Codeforces Round #827 (Div. 4)A~G (Analog, Brute Force, Prefix Max, Binary, Binary)

Check-in questions A~C

A

#include<bits/stdc++.h>
using namespace std;
int n,w,x,y;int a[100005],b[100005];
int main()
{
    int T=1;cin>>T;
    while(T--){
     cin>>w>>x>>y;
     if(w==x+y)cout<<"YES\n";
     else if(x==w+y)cout<<"YES\n";
     else if(y==x+w)cout<<"YES\n";
     else cout<<"NO\n";
    }
    return 0;
}

B

#include<bits/stdc++.h>
using namespace std;
int n,w,x,y;int a[100005],b[100005];
int main()
{
    int T=1;cin>>T;
    while(T--){
     cin>>n;
     for(int i=1;i<=n;i++)cin>>a[i];
     sort(a+1,a+1+n);bool flag=0;
     for(int i=1;i<n;i++)
        if(a[i]==a[i+1])flag=1;
     if(flag)cout<<"NO\n";
     else cout<<"YES\n";
    }
    return 0;
}

C

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T=1;cin>>T;
    while(T--){
int n,w,x,y;int a[10]={0},b[10]={0};char mp[10][10];
        for(int j=1;j<=8;j++)
        for(int i=1;i<=8;i++)cin>>mp[j][i];
        for(int ii=1;ii<=8;ii++){
            bool flag=0;
            for(int i=1;i<=8;i++){
                if(mp[ii][i]!='R')flag=1;
            }
            if(!flag)a[ii]=1;
        }
        for(int ii=1;ii<=8;ii++){
            bool flag=0;
            for(int i=1;i<=8;i++){
                if(mp[i][ii]!='B')flag=1;
            }
            if(!flag)b[ii]=1;
        }
        for(int i=1;i<=8;i++){
            if(a[i]){cout<<"R\n";break;}
            if(b[i]){cout<<"B\n";break;}
        }

    }
    return 0;
}

D.Coprime

Question meaning: Given a sequence, if a[i] and a[j] are relatively prime, then i+j. Find the largest i+j

Mistake: During the competition, I stored all the answers in the vector, and it turned out to be MLE. When I came back to my senses, I found that as long as max was enough, it turned out to be TLE because of the complexity of n squares.

Idea: After the game, it is found that although n has 2e5, the range of a is only 1~1000, so you can record the maximum serial number of each a[i], and you can finish it in 1e6 cycles

code:

#include<bits/stdc++.h>
using namespace std;
int a[200005]={0};int n;int vis[1003];
signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    int T=1;cin>>T;
    while(T--){
        memset(vis,0,sizeof(vis)); int maxx=-1;
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++)vis[a[i]]=i;
        for(int i=1;i<=1000;i++){
            for(int j=i;j<=1000;j++){
                if(!vis[i]||!vis[j])continue;
                if(__gcd(i,j)==1)maxx=max(maxx,vis[i]+vis[j]);
            }
        }
        cout<<maxx<<'\n';
    }
    return 0;
}

E. Sorry

Meaning of the question: Given the length of the legs, walk the stairs until the height of the stairs is greater than the length of the legs, and stop walking. Find the height that has been walked

Mistake: I wrote a double loop during the game, TLE; I thought of the prefix sum, but I didn’t think about the maximum value of the prefix; I thought about the binary search, but I didn’t think about it because there was no order

Idea: prefix maximum value + binary search

The lower_bound() function finds the first element in the range [first, last) that is not less than val.

upper_bound() Finds the first element in the range [first, last) greater than val.

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,w,x,y;int a[200005],k,sum[200005],maxx[200005]={0};
signed main()
{
    int T=1;cin>>T;
    while(T--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>a[i];sum[i]=sum[i-1]+a[i];maxx[i]=max(maxx[i-1],a[i]);
        }
        for(int i=1;i<=m;i++){
            cin>>k;
            int x=upper_bound(maxx+1,maxx+n+1,k)-maxx;
            cout<<sum[x-1]<<' ';
        }
        cout<<'\n';
    }
    return 0;
}

F. Smaller

I misread the question orz, anyway, it is a very simple question      

Remember to open long long

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,w,x,y;
signed main()
{
    int T=1;cin>>T;
    while(T--){
      cin>>n;int suma=1,sumb=1;int a=1,b=1;
      for(int i=1;i<=n;i++){
        int d,k;cin>>d>>k;string ss;cin>>ss;
        if(d==1)for(int p=0;p<ss.size();p++)if(ss[p]=='a')a+=k,suma+=k;
        if(d==2)for(int p=0;p<ss.size();p++)if(ss[p]=='a')b+=k,sumb+=k;
        if(b!=sumb)cout<<"YES\n";
        else if(a==suma){
               if(suma<sumb)cout<<"YES\n";
               else cout<<"NO\n";
        }
        else cout<<"NO\n";
        //a和b都是a,但是a的个数比b少
        //a不都是a,但b都是a,失败
        //b不都是a,成功
      }
    }
    return 0;
}

G. Orray

When doing it, I always get used to splitting the numbers to do it, but it seems that this kind of question is to find a number XOR or or, and then do it according to the situation

Idea: traverse all the numbers, and finally find the optimal result output

Note: If you put a|b in the if condition to judge the size, remember to add parentheses

code:

#include<bits/stdc++.h>
using namespace std;
int a[200005];
bool vis[200005];
int main()
{
    int T;cin>>T;
    while(T--){
        memset(vis,0,sizeof(vis));
        int n;cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        int now=0,mx=0;
        for(int i=1;i<=min(31,n);i++){
            int id=0;
            for(int j=1;j<=n;j++){
                if((now|a[j])>mx){
                   mx=now|a[j];id=j;
                }
            }
            if(id==0)break;
            now=mx;vis[id]=1;cout<<a[id]<<' ';
        }
        for(int i=1;i<=n;i++){
            if(vis[i]==0)cout<<a[i]<<' ';
        }
        cout<<'\n';
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/zy98zy998/article/details/127320378