Codeforces Round # 499 (Div. 1) solution to a problem portion (B, C, D)

Codeforces Round #499 (Div. 1)


This classmates had wanted to play with \ (\ rm virtual \ contest \ ) , resulting in something delayed, and later after another and writing a bit, then together we made a solution to a problem.


B.Rocket

An extremely simple interactive questions, some would say that anti position, and then choose a number to ask all of this directly elected position

Obviously, the choice \ (\ rm 1 \) and \ (\ rm m \) can be, directly after selecting End-half the line

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=41;
int n,m,now,flag;bool vis[maxn];
int check(int x,int now){
    int g;
    printf("%d\n",x),fflush(stdout),read(g);
    if(vis[now])g=-g;return g;
}
int main(){
    read(m),read(n);
    for(rg int i=1;i<=n;i++){
        printf("%d\n",m),fflush(stdout);read(now);
        if(now==0)return 0;
        else if(now==-1)vis[i]=1;
    }
    int l=1,r=m;now=1;
    while(l<=r){
        int mid=(l+r)>>1,f=check(mid,now);
        if(!f)return 0;
        if(f==-1)l=mid+1;else r=mid-1;
        now++;if(now==n+1)now=1;
    }
}

C.Border

This question is actually quiteObviouslyFirst we certainly only a few tubes each with \ (\ rm k \) value in hexadecimal last one, think about the combination of process, found that for \ (\ rm the X-, the y-\) , it seems \ (\ rm gcd (k, x, y) \) multiples can be combined out, and then venture a guess this is right, then all the number and \ (\ rm k \) is the common denominator of the answer is the ability to make up numbers

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m,a[maxn],now;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int main(){
    read(n),read(m),now=m;
    for(rg int i=1;i<=n;i++)read(a[i]),a[i]=a[i]%m,now=gcd(now,a[i]);
    if(!now)return printf("%d\n%d\n",1,0),0;
    printf("%d\n",m/now);
    for(rg int i=0;i<m/now;i++)printf("%d ",i*now);
}

D.Mars rover

Indeed it is not difficult, almost one second title

As each modification only one point, and then we found that for this amendment, as long as long as there is a point to it from the impact of changes on the path to the root, so the root will not be affected.

So we can see that this process can be done from the top down, for each of the two sons of a point, if it's left son negated its value is not affected, so the root will not be affected, that is to say take root against its son left this all leaf nodes in the subtree are not affected, the right son versa

So that we can \ (O (n) \) sweep over first calculate the value of the initial tree node weight, then \ (O (n) \) sweep again draw each leaf node to root any impact on the line

The total time complexity \ (O (n) \)

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
    char ch; bool ok;
    for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
    for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e6+10;
int n,m,f[maxn],ans,a[maxn],l[maxn][3];
char s[maxn][5];
bool vis[maxn];
void dfs(int x){
    if(l[x][0]==1){
        dfs(l[x][1]);
        f[x]=!f[l[x][1]];
    }
    else if(l[x][0]==2){
        dfs(l[x][1]),dfs(l[x][2]);
        if(s[x][1]=='A')f[x]=f[l[x][1]]&f[l[x][2]];
        if(s[x][1]=='O')f[x]=f[l[x][1]]|f[l[x][2]];
        if(s[x][1]=='X')f[x]=f[l[x][1]]^f[l[x][2]];
    }
}
void solve(int x,int now){
    vis[x]=now;
    if(l[x][0]==1)solve(l[x][1],now);
    else if(l[x][0]==2){
        if(s[x][1]=='A'){
            int now1=(!f[l[x][1]])&f[l[x][2]],now2=(!f[l[x][2]])&f[l[x][1]];
            if(now1!=f[x])solve(l[x][1],now);
            else solve(l[x][1],0);
            if(now2!=f[x])solve(l[x][2],now);
            else solve(l[x][2],0);
        }
        if(s[x][1]=='O'){
            int now1=(!f[l[x][1]])|f[l[x][2]],now2=(!f[l[x][2]])|f[l[x][1]];
            if(now1!=f[x])solve(l[x][1],now);
            else solve(l[x][1],0);
            if(now2!=f[x])solve(l[x][2],now);
            else solve(l[x][2],0);
        }
        if(s[x][1]=='X'){
            int now1=(!f[l[x][1]])^f[l[x][2]],now2=(!f[l[x][2]])^f[l[x][1]];
            if(now1!=f[x])solve(l[x][1],now);
            else solve(l[x][1],0);
            if(now2!=f[x])solve(l[x][2],now);
            else solve(l[x][2],0);
        }
    }
}
int main(){
    read(n);
    for(rg int i=1;i<=n;i++){
        scanf("%s",s[i]+1);
        if(s[i][1]=='A'||s[i][1]=='O'||s[i][1]=='X')
            l[i][0]=2,read(l[i][1]),read(l[i][2]);
        else if(s[i][1]=='N')l[i][0]=1,read(l[i][1]);
        else l[i][0]=0,read(f[i]);
    }
    dfs(1),solve(1,1);
    for(rg int i=1;i<=n;i++)
        if(s[i][1]=='I'){
            if(vis[i])printf("%d",!f[1]);
            else printf("%d",f[1]);
        }
}

Guess you like

Origin www.cnblogs.com/lcxer/p/11059023.html