Codeforces Round # 604 (Div. 2) (explanations)

A. Beautiful String (violence)

Topic Link

Subject to the effect:

Given a string, only \ (? A \ b \ c \? \) , And asked whether there is an all \ (? \) Replaced by \ (A \ b \ c \) , so that any adjacent characters in different ways.

The general idea:

In fact, the question mark can be found by enumerating make it legal, if there is not legitimate to say hello necessarily have to remove illegal, violent enumeration can be.

Code:

Click to expand the code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char s[N];
int T;
char c[3]={'a','b','c'};
int main()
{
    //freopen("H:\\c++1\\in.txt","r",stdin);
    //freopen("H:\\c++1\\out.txt","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%s",s+1);
        int len=strlen(s+1);
        for(int i=1;i<=len;i++){
            if(s[i]=='?'){
                    for(int j=0;j<3;j++){
                        if(c[j]!=s[i-1]&&c[j]!=s[i+1]){
                            s[i]=c[j];break;
                        }
                    }
                
            }
        }
        int flag=0;
        for(int i=1;i<len&&flag==0;i++){
            if(s[i]==s[i+1])flag=1;
        }
        if(flag){
            puts("-1");
        }
        else printf("%s\n",s+1);
    }
    return 0;
}

B. Beautiful Numbers (thinking)

Topic Link

Subject to the effect:

To a permutation, define \ (m \) there is a continuous stretch \ (1 - m \) are arranged, such as \ (\ {4,5,1,3,2,6 \} \) , there \ ( {1,3,2} \) , for the \ (3 \) arrangement, which does not exist \ (m = 2 \) arrangement. Q. For each \ (m \) determines whether there is \ (1-m \) arrangement.

The general idea:

As long as the position of each number is recorded \ (POS \) array, for example, \ (\ {4,5,1,3,2,6 \} \) a \ (POS \) array is \ (\ {3 , 5,4,1,2,6 \} \) then we just record the maximum front to back \ (max \) and the minimum value \ (min \) , then \ (max-min + 1 == i \) is present \ (1-m \) arrangement.

Code:

Click to expand the code

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
set<int>s; // 用set维护最大值最小值,其实不用那么麻烦
int T;
int a[N];
int pos[N];
int n;
int ans[N];
int main()
{
    //freopen("H:\\c++1\\in.txt","r",stdin);
    //freopen("H:\\c++1\\out.txt","w",stdout);
    scanf("%d",&T);
    while(T--){
        s.clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            pos[a[i]]=i;
        }
        for(int i=1;i<=n;i++){
            s.insert(pos[i]);
            auto it=s.begin();
            int v1=*it;
            it=s.end();
            it--;
            int v2=*it;
            if(v2-v1+1==i)ans[i]=1;
            else ans[i]=0;
        }

        for(int i=1;i<=n;i++)printf("%d",ans[i]);
        puts("");
    }
    return 0;
}

C. Beautiful Regional Contest (greedy)

Topic Link

Subject to the effect:

For \ (n-\) number of individual problem-solving, now issued medals, gold, silver and bronze each \ (g, s, b \ ) each, requires solving a number greater than gold silver, \ (... \) , bronze solution unlicensed greater than the number of questions, and \ (G, S, B> 0 \ \ & \ & \ G <S, G <B \) , the required number of medals obtained legally, and maximum.

The general idea:

Start thinking too much, resulting in the card to the end of the game. . . In fact, the number of gold medals is determined, as soon as a silver g + 1, and then look through the same number of problem-solving to take a minimum number of silver, after looking up to take the bronze medal calculate how many. To determine the legality.

Code:

Click to expand the code

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+10;
int T;
int n;
int p[N];
int id[N],l[N],r[N],sum[N],cnt=0;
int qh(int L,int R){
    return sum[R]-sum[L-1];
}
int main()
{
    //freopen("H:\\c++1\\in.txt","r",stdin);
    //freopen("H:\\c++1\\out.txt","w",stdout);
    scanf("%d",&T);
    while(T--){
        cnt=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&p[i]);
        }
        reverse(p+1,p+n+1);
        int temp=p[1];cnt++;l[cnt]=1;
        for(int i=1;i<=n;i++){
            if(p[i]==temp)id[i]=cnt;
            else{
                temp=p[i];
                r[cnt]=i-1;
                cnt++;
                l[cnt]=i;id[i]=cnt;
            }
        }
        r[cnt]=n;
        for(int i=1;i<=cnt;i++)sum[i]=r[i]-l[i]+1,sum[i]=sum[i-1]+sum[i];
        int mx=n/2;
        if(mx<5){
            puts("0 0 0");continue;
        }
        int flag=1;
        int pp=id[n];
        int g=sum[pp]-sum[pp-1];
        int pos=l[pp];
        int s=g+1;
        temp=pos-s;
        if(s+g>=n/2)flag=0;
        int pp1=id[temp];
        int pos1=l[pp1];
        s=pos-pos1;
        int sy=n-mx;
        int pp2=id[sy];
        int pos2=r[pp2]+1;
        pp2++;
        int b=pos1-pos2;
        if(b<=0||s<=0||b<=g||s<=g)flag=0;
        if(flag)printf("%d %d %d\n",g,s,b);
        else puts("0 0 0");
    }
    return 0;
}

D. Beautiful Sequence (enumeration)

Topic Link

Subject to the effect:

Number to four, \ (A, B, C, D \) , represents \ (A \) a \ (0 \) , \ (B \) th \ (. 1 \) , \ (C \) a \ ( 2 \) , \ (D \) a \ (3 \) , require these numbers in a row, so that the number is a difference of two adjacent, output scheme.

The general idea:

Each number can be enumerated as the beginning, and then select adjacent digital back to fill in, if you can fill out, then the program can be output. Because if there is an answer, beginning with four kinds of digital one is bound to get the solution.

Code:

Click to expand the code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int ans[N];
int flag=0;
vector<int>temp;
void dfs(int a,int b,int c,int d,int pos){
    if(a+b+c+d==0){
        flag=1;
        puts("YES");
        for(int v:temp)printf("%d ",v);
            puts("");exit(0);
    }
    if(pos==0&&b)temp.push_back(1),dfs(a,b-1,c,d,pos+1);
    if(pos==1){if(c)temp.push_back(2),dfs(a,b,c-1,d,pos+1);
              else if(a)temp.push_back(0),dfs(a-1,b,c,d,pos-1);}
    if(pos==2){if(d)temp.push_back(3),dfs(a,b,c,d-1,pos+1);
              else if(b)temp.push_back(1),dfs(a,b-1,c,d,pos-1);}
    if(pos==3&&c)temp.push_back(2),dfs(a,b,c-1,d,pos-1);

}
int a,b,c,d;

int main()
{
    //freopen("H:\\c++1\\in.txt","r",stdin);
    //freopen("H:\\c++1\\out.txt","w",stdout);
    scanf("%d%d%d%d",&a,&b,&c,&d);
    flag=0;
    if(flag==0&&a){
        temp.clear();
        temp.push_back(0);
        dfs(a-1,b,c,d,0);
    }
    if(flag==0&&b){
        temp.clear();
        temp.push_back(1);
        dfs(a,b-1,c,d,1);   
    }
    if(flag==0&&c){
        temp.clear();
        temp.push_back(2);
        dfs(a,b,c-1,d,2);
    }
    if(flag==0&&d){
        temp.clear();
        temp.push_back(3);
        dfs(a,b,c,d-1,3);
    }
    if(flag==0)puts("NO");
    return 0;
}

E. Beautiful Mirrors (probability DP)

Topic Link

Subject to the effect:

There \ (n \) mirrors, each mirror has \ (p_i \) of probability say beautiful, I began to ask from a mirror, say if it beautiful, then I would ask the following day at a mirror, if just to the first \ (n \) mirrors, then I will be happy. Otherwise, I began to ask again the next day from a mirror. I asked the expected number of days needed to be happy.

The general idea:

Suppose \ (I \) mirrors, the number of days required for the happy \ (F_i \) , then the \ (F_i = P_i * (F_ {i-1} +1) + (1-P_i) * (F_ {i- 1} + F_i + 1) \ )

Is the first \ (F_ {i-1} \) days must be beautiful, there \ (P_i \) probability is beautiful, so I'll just be happy, that is, \ (F_ {i-1} +1 \) day, as well as the rest of probability, I need to start again you will need to \ (F_ {i-1} + F_i + 1 \) days. The expression profile of what is the \ (F_i = (F_ {i -1} +1) / P_i \) cycle again on the line. (Probability will be in addition to 100).

Code:

Click to expand the code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
const int mod=998244353;
ll ksm(ll a,ll b){
    ll res=1,t=a;
    while(b){
        if(b&1)res=(res*t)%mod;
        t=(t*t)%mod;
        b>>=1;
    }
    return res;
}
int n;
ll p[N],f[N];
int main()
{
    //freopen("H:\\c++1\\in.txt","r",stdin);
    //freopen("H:\\c++1\\out.txt","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lld",&p[i]);
    f[0]=0;
    for(int i=1;i<=n;i++){
        f[i]=((100*(f[i-1]+1)%mod)*ksm(p[i],mod-2)%mod)%mod; //注意爆ll
    }
    printf("%lld\n",f[n]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/C-W-K/p/12002523.html