Educational Codeforces Round 101 (Rated for Div. 2)

A. Regular Bracket Sequence

题意:题目中给(和)还有?,其中?可以转换成为()中的任何一个,并且所给样例中只出现一次(),问能不能括号匹配

思路:直接看第一个和最后一个能不能匹配上,然后再看一下全部的长度是不是偶数个字符

想错:1)我一开始读错题,没有看到只出现一次的括号;

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const long long int maxx=2e5+10;
 9 int aa[maxx];
10 int bb[maxx];
11  
12 int main(){
13     int t;
14     scanf("%d",&t);
15     getchar();
16     while(t--){
17         char s[200];
18         scanf("%s",&s);
19         int n=strlen(s);
20  
21         if(n%2==1){
22             printf("NO\n");
23         }else{
24             int flag=0;
25             int i=0,j=n-1;
26             if((s[i]=='('&&s[j]==')')||(s[i]=='('&&s[j]=='?')||(s[i]=='?'&&s[j]=='?')||(s[i]=='?'&&s[j]==')')){
27  
28             }else{
29                 flag=1;
30             }
31             if(flag==1){
32                 printf("NO\n");
33             }else{
34                 printf("YES\n");
35             }
36         }
37     }
38 }
View Code

B. Red and Blue

题意:就是一组数a,然后染色成为红色r和蓝色b,记不清相对位置,给定红色中红色数出现的相对位置,蓝色也是,问f(a)=max(0,a1,(a1+a2),(a1+a2+a3),,(a1+a2+a3++an+m)),这个数组a的最大值是多少

思路:就是找数组r和数组b已经确定,这两个构成的数组,最大的前缀和是多少,直接找到r和b分别最大的前缀和相加即可

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const long long int maxx=2e5+10;
 9 int aa[maxx];
10 int bb[maxx];
11  
12 int main(){
13     int t;
14     scanf("%d",&t);
15     while(t--){
16         int a[250],b[250];
17         int n,m;
18         scanf("%d",&n);
19         int sum1=0,sum2=0;
20         int max1=0,max2=0;
21         for(int i=0;i<n;i++){
22             int num;
23             scanf("%d",&num);
24             sum1+=num;
25             max1=max(max1,sum1);
26         }
27         scanf("%d",&m);
28         for(int i=0;i<m;i++){
29             int num;
30             scanf("%d",&num);
31             sum2+=num;
32             max2=max(max2,sum2);
33         }
34         printf("%d\n",max1+max2);
35         printf("\n");
36     }
37 }
View Code

C. Building a Fence

题意: 给定一堆高度,然后去围栅栏,第一个和最后一个应该是必须挨着地面,其他围栏可以浮空,但是不能超过k-1

思路:依次处理每个围栏的区间,假设围栏所在区间最低为low,最高为high,则第一个围栏low = h[1], high = h[1] + k,第二个围栏low = max(low - k + 1, h[i]),因为每个区间需要有公共边,所以max里第二个值还需 + 1,high = min(high + k - 1, h[i] + 2 * k - 1),第一个区间最大值 + 围栏高度 - 1),因为围栏最多浮空k - 1,所以max里第一个值为地面高度 + k - 1 + k,因为每个区间需要有公共边,所以max里第二个值还需 - 1,以此类推见https://www.cnblogs.com/xiaopangpangdehome/p/14205266.html

想错:1)这个是dp区间的两边都要dp,这样一来就不是很好确定;2)不管是最大最小,一个是依赖与前面有公共边,另一个就是在高度为h[i]的高度上进行建筑;3)最低选最大值的时候肯定不浮空

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<queue>
 7 using namespace std;
 8 const long long int maxx=2e5+10;
 9 int main(){
10     int t;
11     scanf("%d",&t);
12     while(t--){
13         int a[maxx]={0},b[maxx]={0};
14         int n,k;
15         scanf("%d %d",&n,&k);
16         int flag=0;
17         for(int i=0;i<n;i++){
18             scanf("%d",&a[i]);
19         }
20         int low=0,high=0;
21         for(int i=0;i<n;i++){
22             if(i==0){
23                 low=a[i];
24                 high=a[i]+k;
25             }else{
26                 low=max(low-k+1,a[i]);
27                 high=min(high+k-1,a[i]+2*k-1);
28             }
29             if(high-k<a[i]||low>a[i]+k-1||high<low){
30                 flag=1;
31                 break;
32             }
33         }
34        if(low!=a[n-1]){
35             flag=1;
36        }
37         if(flag==0){
38             printf("YES\n");
39         }else{
40             printf("NO\n");
41         }
42  
43     }
44 }
View Code

 

posted @ 2021-01-28 15:11  bonel  阅读(48)  评论(0)    收藏  举报