Codeforces Round #604 (Div. 2)(A-E)

A. Beautiful String

题意:把'?'换成'a' or 'b' or 'c'使得相邻的两个字符不相同。

暴力枚举每个'?'前后。

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
string s;
int main(){
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--){
        cin>>s;
        bool ok=true;
        for(int i=0;i<s.size();i++){
            if(i!=0&&s[i]==s[i-1]){
                ok=false;
                break;
            }
            if(s[i]=='?'){
                bool a=false,b=false,c=false;
                if(i!=0){
                    if(s[i-1]=='a')a=true;
                    else if(s[i-1]=='b')b=true;
                    else if(s[i-1]=='c')c=true;
                }
                if(s[i+1]=='a')a=true;
                else if(s[i+1]=='b')b=true;
                else if(s[i+1]=='c')c=true;
                if(!a)s[i]='a';
                else if(!b)s[i]='b';
                else if(!c)s[i]='c';
 
            }
        }
        if(!ok)cout<<"-1"<<endl;
        else cout<<s<<endl;
    }
 
    return 0;
}
View Code

B. Beautiful Numbers

题意:给一个1-n的序列,判断它的子串1-m的长度是否为m。

解:桶排序记录1-n在序列的位置,然后去计算1-m的长度,判断长度是否为m。

#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5+10;
int a[MAXN];
bool vis[MAXN];
int num[MAXN];
int main(){
    int T;
    cin>>T;
    while(T--){
        int n;
        cin>>n;
        int s=1;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            num[a[i]]=i;
        }
        cout<<1;
        int l=num[1],r=num[1];
        for(int i=2;i<=n;i++){
            l=min(l,num[i]);
            r=max(r,num[i]);
            if(r-l+1!=i)cout<<0;
            else cout<<1;
        }
        cout<<endl;
 
    }
 
    return 0;
}
View Code

C. Beautiful Regional Contest

题意:给出n个人的出题数量,让你安排金银铜的人数,满足:金银铜>0,银>金,铜>金,金+银+铜<=n/2

解:模拟。直接安排出题量最多的为金牌人数,第二的为银(如果没有大于金就顺延),剩下都是铜。

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e6+10;
int a[MAXN];
int num[MAXN];
int g,s,b;
int main(){
    int T;
    cin>>T;
    while(T--){
        int n;
        cin>>n;
        int m=n/2;
        g=s=b=0;
        int flag;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            if(i==(n/2)+1)flag=a[i];
        }
        for(int i=1;i<=n;i++){
            if(a[i]==a[1])g++;
            else break;
        }
        s=1;
        for(int i=g+2;i<=n;i++){
            if(a[i]==flag)break;
            if(a[i]==a[i-1]||s<=g)s++;
            else break;
        }
        for(int i=s+g+1;i<=n;i++){
            if(a[i]!=flag)b++;
            else break;
        }
        if((b>g&&s>g&&(s+b+g<=m)))cout<<g<<' '<<s<<' '<<b<<endl;
        else cout<<"0 0 0"<<endl;
 
    }
 
    return 0;
}
View Code

D. Beautiful Sequence

题意:给出0,1,2,3的数量,让你判断是否能排列成一个 任意相邻两个差的绝对值=1,并输出序列。

解:暴力模拟。。。。。。。。。。。。。。。直接按顺序排,0肯定跟1组合,3肯定跟2组合,其他就是1跟2组合。(写的好丑)

#include <bits/stdc++.h>
using namespace std;
 
int main(){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(a==0&&b==0&&abs(c-d)<=1){
        cout<<"YES"<<endl;
        if(c==d){
            while(c--)cout<<"2 3 ";cout<<endl;
        }
        else if(c>d){
            while(d--)cout<<"2 3 ";cout<<2<<endl;
        }else {
            cout<<"3 ";
            while(c--)cout<<"2 3 ";cout<<endl;
        }
    }
    else if(c==0&&d==0&&abs(a-b)<=1){
        cout<<"YES"<<endl;
        if(a==b){
            while(a--)cout<<"0 1 ";cout<<endl;
        }
        else if(a>b){
            while(b--)cout<<"0 1 ";cout<<0<<endl;
        }else {
            cout<<"1 ";
            while(a--)cout<<"0 1 ";cout<<endl;
        }
    }
    else if(a<=b&&d<=c){
        int t=b-a;
        int tt=c-d;
        if(t-tt==1){
            cout<<"YES"<<endl;
            cout<<1<<' ';b--;
            while(a--){
                cout<<"0 1 ";
            }
            while(tt--){
                cout<<"2 1 ";
            }
            while(d--){
                cout<<"2 3 ";
            }cout<<endl;
        }
        else if(tt-t==1){
        cout<<"YES"<<endl;
            while(a--){
                cout<<"0 1 ";
            }
            while(t--){
                cout<<"2 1 ";
            }
            while(d--){
                cout<<"2 3 ";
            }cout<<2<<endl;
        }
        else if(t==tt){
        cout<<"YES"<<endl;
            while(a--){
                cout<<"0 1 ";
            }
            while(t--){
                cout<<"2 1 ";
            }
            while(d--){
                cout<<"2 3 ";
            }cout<<endl;
        }
        else cout<<"NO"<<endl;
 
    }
    else cout<<"NO"<<endl;
 
 
    return 0;
}
View Code

E. Beautiful Mirrors

经典概率题,看了别人的题解有一点点理解吧。

https://www.cnblogs.com/NaVi-Awson/p/11999959.html

#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5+10;
typedef long long ll;
const int mod=998244353;
ll a[MAXN];
ll ksm(ll x,ll y){
    ll res=1LL;
    while(y){
        if(y&1)res=res*x%mod;
        y>>=1;
        x=x*x%mod;
    }
    return res%mod;
}
//a^(p-1)=1modp
//a^(p-2)%p=1/a;
int main(){
    int n;
    cin>>n;
    ll ans=0LL;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        ans=((((ans+1LL)%mod)*100%mod)*ksm(a[i],mod-2)%mod)%mod;
    }
    cout<<ans<<endl;
 
 
 
 
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/lin1874/p/12208528.html