Codeforces Round #629 (Div. 3) C, D problem solution

PS: During the game, I don’t know why the cf website either crashes or the display speed is very slow. It takes several minutes to hand in the code. .
Question C:
Rough meaning: Give us a ternary number X, require the construction of ternary numbers a, b, and define the XOR value of the ternary system as xi=(ai+bi)%3. The constructed a and b must meet the following conditions. 1.a^b=x 2.max(a,b) is the smallest.
Idea: At first, a1=b1=1, and then traverse from 2-n. When the first 1 is encountered, a string is assigned 1, and b string is assigned 0, and then all numbers are placed in b. We can find that 10000>02222, so the choice is one-sided. Therefore, with this structure, we can find that b must be less than or equal to a, and the maximum and minimum
code of a:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=5e4+50;
char x[MAXN];
char a[MAXN],b[MAXN];
int main()
{
    
    
    int t;int n;
    scanf("%d",&t);
    while(t--)
    {
    
    
        scanf("%d",&n);
        scanf("%s",x+1);
        int flag=0;
        a[1]='1';b[1]='1';
        for(int i=2;i<=n;i++)
        {
    
    
            if(flag==0)
            {
    
    
                if(x[i]=='0')
                {
    
    
                    a[i]='0';
                    b[i]='0';
                }
                else if(x[i]=='2')
                {
    
    
                    a[i]='1';
                    b[i]='1';
                }
                else if(x[i]=='1')
                {
    
    
                    a[i]='1';
                    b[i]='0';
                    flag=1;
                }
            }
            else
            {
    
    
                a[i]='0';
                b[i]=x[i];
            }
        }
        for(int i=1;i<=n;i++)
        {
    
    
            printf("%c",a[i]);
        }
        printf("\n");
        for(int i=1;i<=n;i++)
        {
    
    
            printf("%c",b[i]);
        }
        printf("\n");
    }
    return 0;
}

Question D:
General meaning of the question: n animals form a ring. Now we need to color the ring. The number of colors used is as small as possible, and adjacent animals cannot use the same color.
Idea: It can be divided into three situations:
1. If there is only one animal, all of them have the same color.
2. If the adjacent animals are not the same, three colors are required, the first is 1212, and the last animal is 3.
3. If the above two conditions are not met, if n is an even number, then 12121212..., if n is an odd number, because the second type is not satisfied, there must be adjacent animals that are the same, so you can first 12 and encounter the same , The flip becomes 21.
Note: Because this is a ring, you must not forget to judge 1 and n.
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2e5+50;
int t[MAXN],num[MAXN];
int main()
{
    
    
    int q,n;
    scanf("%d",&q);
    while(q--)
    {
    
    
        memset(num,0,sizeof num);
        scanf("%d",&n);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%d",&t[i]);
            if(num[t[i]]==0)
            {
    
    
                cnt++;
                num[t[i]]=1;
            }
        }
        if(cnt==1)
        {
    
    
            printf("1\n");
            for(int i=1;i<=n;i++)
            {
    
    
                printf("1 ");
            }
            printf("\n");
            continue;
        }
        t[0]=t[n];t[n+1]=t[1];
        int flag=0;int pos=0;
        for(int i=1;i<=n;i++)
        {
    
    
            if(t[i]==t[i-1])
            {
    
    
                pos=i;
                flag=1;
                break;
            }
        }
        if(flag==0&&(n&1))
        {
    
    
            printf("3\n");
            for(int i=1;i<=(n-1)/2;i++)
            {
    
    
                printf("1 2 ");
            }
            printf("3\n");
        }
        else if(!(n&1))
        {
    
    
            printf("2\n");
            for(int i=1;i<=n/2;i++)
            {
    
    
                printf("1 2 ");
            }
            printf("\n");
        }
        else if(flag&&(n&1))
        {
    
    
            printf("2\n");
            for(int i=1;i<pos;i++)
            {
    
    
                printf("%d ",i%2+1);
            }
            for(int i=pos;i<=n;i++)
            {
    
    
                printf("%d ",2-i%2);
            }
            printf("\n");
        }
    }
    return 0;
}

Maybe we are not as smart as others, but we can work hard and we will pay off in the end. Since we have chosen the path of ACM, we must go down with love.

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/105147832