Educational Codeforces Round 101 A~D

Because I was preparing for the final exam, I missed the cf for more than half a month. Now I come back to make up and find out how it feels.

A. Regular Bracket Sequence

Question portal:

A. Regular Bracket Sequence

Ideas:

There is only a pair of brackets'()', and all the rest are question marks. Each question mark may be'(' or')'. Ask if you can match all the brackets.
At first, I didn't see the condition with only a pair of parentheses, and it was complicated.

AC Code

#include <bits/stdc++.h>
using namespace std;
string s;
int main()
{
    
    
   int t;
   cin>>t;
   while(t--)
   {
    
    
       cin>>s;
       int n=s.size();
       if(n&1||s[0]==')'||s[n-1]=='(')
        cout<<"NO"<<endl;
        else 
        cout<<"YES"<<endl;
   }
   return 0;
}

B. Red and Blue

Question portal:

B. Red and Blue

Ideas:

Because the relative positions of the elements in the r array and the b array in the a array have not changed, the maximum prefix sum of the r array is X, and the maximum prefix sum of the b array is Y. Then the answer is max( X, Y, X + Y).

AC Code

#include<bits/stdc++.h>
using namespace std;
int n,m;
int r[200],b[200];
int f1[200],f2[200];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        memset(f1,0,sizeof(f1));
        memset(f2,0,sizeof(f2));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&r[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
            scanf("%d",&b[i]);
        int maxr=-1e5,maxb=-1e5;
        for(int i=1;i<=n;i++)
        {
    
    
            f1[i]=f1[i-1]+r[i];
            maxr=max(maxr,f1[i]);
        }
        for(int i=1;i<=m;i++)
        {
    
    
            f2[i]=f2[i-1]+b[i];
            maxb=max(maxb,f2[i]);
        }
        int res=0;
        res=max(maxr,maxb);
        res=max(res,maxr+maxb);
        res=max(res,0);
        printf("%d\n",res);
    }
    //system("pause");
    return 0;
}

C. Building a Fence

Question portal:

C. Building a Fence

Ideas:

The positions of the first fence and the last fence are certain. Find the starting interval of each fence, and then compare with the previous fence whether there is a common edge. If 2~n-1 fences are satisfied, don't forget to compare with the last determined fence.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int h[N];
int top[N],low[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&h[i]);
        top[1]=h[1],low[1]=h[1];
        top[n]=h[n],low[n]=h[n];
        int flag=0;
        for(int i=2;i<n;i++)
        {
    
    
            top[i]=h[i]+k-1;//最高的起点
            low[i]=h[i];   //最低的起点
            top[i]=min(top[i],top[i-1]+k-1);
            low[i]=max(low[i],low[i-1]-k+1);
            if(low[i]>=top[i-1]+k||top[i]<=low[i-1]-k)
            {
    
    
                flag=1;
                break;
            }
        }
        if(low[n]>=top[n-1]+k||top[n]<=low[n-1]-k) flag=1;
        if(flag==1) printf("NO\n");
        else printf("YES\n");
    }
    //system("pause");
    return 0;
}

D. Ceil Divisions (structure)

Question portal:

D. Ceil Divisions

Ideas:

We found that a number smaller than n divided by n is rounded up to 1. But this will leave an unprocessed n at the end. So we thought about what to do with n. My first thought was to keep dividing by 2, but seeing the data range and the largest operand, it is obviously wrong to think this way. Seeing this operand, the most likely thing is the square root. Then we observe (n^1/2,n), we find that n divided by twice is the former one. Moreover, the square of the continuous 2e5 is 200,000 448 22 5 3 2. Obviously the number of operations meets the requirements of the problem.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int h[N];
int top[N],low[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&h[i]);
        top[1]=h[1],low[1]=h[1];
        top[n]=h[n],low[n]=h[n];
        int flag=0;
        for(int i=2;i<n;i++)
        {
    
    
            top[i]=h[i]+k-1;//最高的起点
            low[i]=h[i];   //最低的起点
            top[i]=min(top[i],top[i-1]+k-1);
            low[i]=max(low[i],low[i-1]-k+1);
            if(low[i]>=top[i-1]+k||top[i]<=low[i-1]-k)
            {
    
    
                flag=1;
                break;
            }
        }
        if(low[n]>=top[n-1]+k||top[n]<=low[n-1]-k) flag=1;
        if(flag==1) printf("NO\n");
        else printf("YES\n");
    }
    //system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/Stevenwuxu/article/details/112722802
Recommended