zoukankan      html  css  js  c++  java
  • Codeforces Round #732 (Div. 2)

    绝赞自闭 China Round

    A

    模拟。
    无解的情况是前后总和不等。
    然后当 (a_i>b_i) 时就向后填,(a_i<b_i) 时就把后面的搬过来,这样操作就能保证 (a=b) 了。

    #pragma GCC optimize("O3")
    #include<bits/stdc++.h>
    using namespace std;
    #define endl '
    '
    #define debug(x) cerr << #x << ": " << x << endl
    #define pb(a) push_back(a)
    #define set0(a) memset(a,0,sizeof(a))
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define dwn(i,a,b) for(int i=(a);i>=(b);i--)
    #define ceil(a,b) (a+(b-1))/b
    #define INF 0x3f3f3f3f
    #define ll_INF 0x7f7f7f7f7f7f7f7f
    typedef long long ll;
    typedef pair<int,int> PII;
    typedef pair<double,double> PDD;
    
    inline void read(int &x) {
        int s=0;x=1;
        char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
        x*=s;
    }
    
    const int N=105;
    
    int a[N], b[N];
    
    int main(){
    	int T; cin>>T;
    	while(T--){
    		int n; cin>>n;
    		
    		int s[2]={0};
    		rep(i,1,n) cin>>a[i], s[0]+=a[i];
    		rep(i,1,n) cin>>b[i], s[1]+=b[i];
    		
    		if(s[0]!=s[1]){
    			puts("-1");
    			continue;
    		}
    		
    		vector<PII> res;
    		
    		rep(i,1,n) if(a[i]!=b[i]){
    			while(a[i]>b[i]){
    				rep(j,i+1,n) while(a[j]<b[j] && a[i]>b[i]){
    					res.push_back({i, j});
    					a[i]--, a[j]++;
    				}
    			}
    			
    			while(a[i]<b[i]){
    				rep(j,i+1,n) while(a[j]>b[j] && a[i]<b[i]){
    					res.push_back({j, i});
    					a[i]++, a[j]--;
    				}
    			}
    		}
    		
    		cout<<res.size()<<endl;
    		for(auto i: res) cout<<i.first<<' '<<i.second<<endl;
    	}	
        return 0;
    }
    

    B

    思维题。
    异或一下,落单的就是答案,每位都这样操作一下即可。
    和这题的思想一模一样:https://www.luogu.com.cn/problem/P1469

    #pragma GCC optimize("O3")
    #include<bits/stdc++.h>
    using namespace std;
    #define endl '
    '
    #define debug(x) cerr << #x << ": " << x << endl
    #define pb(a) push_back(a)
    #define set0(a) memset(a,0,sizeof(a))
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define dwn(i,a,b) for(int i=(a);i>=(b);i--)
    #define ceil(a,b) (a+(b-1))/b
    #define INF 0x3f3f3f3f
    #define ll_INF 0x7f7f7f7f7f7f7f7f
    typedef long long ll;
    typedef pair<int,int> PII;
    typedef pair<double,double> PDD;
    
    inline void read(int &x) {
        int s=0;x=1;
        char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
        x*=s;
    }
    
    int cnt[100005][26];
    
    int main(){
    	int T; cin>>T;
    	while(T--){
    		string res=""; 
    		int n, m; cin>>n>>m;
    		
    		rep(i,0,m-1) rep(j,0,25) cnt[i][j]=0;
    		
    		rep(i,1,n+n-1){
    			string t; cin>>t;
    			rep(j,0,m-1) cnt[j][t[j]-'a']++;
    		}
    		rep(i,0,m-1) rep(j,0,25) if(cnt[i][j]&1){
    			cout<<(char)(j+'a');
    		}
    		cout<<endl;
    	}	
        return 0;
    }
    

    C

    思维题。
    注意到每个数都必须要操作偶数次,所以我们考察一下数列 (a) 排序前后每个数的奇偶位数情况,如果不相等了就是 NO ,都相等那就 YES

    #pragma GCC optimize("O3")
    #include<bits/stdc++.h>
    using namespace std;
    #define endl '
    '
    #define debug(x) cerr << #x << ": " << x << endl
    #define pb(a) push_back(a)
    #define set0(a) memset(a,0,sizeof(a))
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define dwn(i,a,b) for(int i=(a);i>=(b);i--)
    #define ceil(a,b) (a+(b-1))/b
    #define INF 0x3f3f3f3f
    #define ll_INF 0x7f7f7f7f7f7f7f7f
    typedef long long ll;
    typedef pair<int,int> PII;
    typedef pair<double,double> PDD;
    
    #define int long long
    
    inline void read(int &x) {
        int s=0;x=1;
        char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
        x*=s;
    }
    
    const int N=1e5+5;
    int buc[N][2];
    int a[N], b[N];
    
    signed main(){
    	int T; cin>>T;
    	while(T--){
    		set0(buc);
    		int n; cin>>n;
    		rep(i,1,n) read(a[i]), b[i]=a[i], buc[a[i]][i&1]++;
    	
    		sort(b+1, b+1+n);	
    		
    		rep(i,1,n) buc[b[i]][i&1]--;
    		
    		bool ok=true;
    		rep(i,1,N-1) if(buc[i][0] || buc[i][1]){
    			puts("NO");
    			ok=false;
    			break;
    		}
    		
    		if(ok) puts("YES");
    	}	
        return 0;
    }
    

    D

    组合计数。
    分组,从左到右分,如果连续两个数都是 (1) ,那就分成一组,注意到(可我赛场上没注意到(悲))在若干次操作之后,组数是不变的(虽然组员会改变),那么我们可以把组看做是小球(设有 (m) 组),把 (0) (设有 (n) 个)和当前的位置看成是盒子,那么组合数为 (C_{m+n}^m)

    #pragma GCC optimize("O3")
    #include<bits/stdc++.h>
    using namespace std;
    #define endl '
    '
    #define debug(x) cerr << #x << ": " << x << endl
    #define pb(a) push_back(a)
    #define set0(a) memset(a,0,sizeof(a))
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define dwn(i,a,b) for(int i=(a);i>=(b);i--)
    #define ceil(a,b) (a+(b-1))/b
    #define INF 0x3f3f3f3f
    #define ll_INF 0x7f7f7f7f7f7f7f7f
    typedef long long ll;
    typedef pair<int,int> PII;
    typedef pair<double,double> PDD;
    
    #define int long long
    
    const int N=1e5+5, mod=998244353;
    
    inline void read(int &x) {
        int s=0;x=1;
        char ch=getchar();
        while(ch<'0'||ch>'9') {if(ch=='-')x=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
        x*=s;
    }
    
    ll fpow(ll x,ll p)
    {
        ll res=1;
        for(;p;p>>=1,x=x*x%mod)
            if(p&1)res=res*x%mod;
        return res%mod;
    }
    
    ll inv(ll x){
    	return fpow(x,mod-2)%mod;
    }
    
    ll fac[N];
    
    void init(){
    	fac[0]=1;
    	for(int i=1; i<N; i++) fac[i]=fac[i-1]*i%mod;
    }
    
    ll C(ll a, ll b){
    	return fac[a]*inv(fac[b])%mod*inv(fac[a-b])%mod;
    }
    
    bool vis[N];
    
    signed main(){
    	init();
    	int T; cin>>T;
    	while(T--){
    		int l; cin>>l;
    		string s; cin>>s;
    		rep(i,0,l-1) vis[i]=false;
    		
    		int n=0, m=0;
    		rep(i,0,l-1){
    			if(s[i]=='0') n++;
    			else if(i && s[i]=='1' && s[i-1]=='1' && !vis[i-1]) m++, vis[i]=true;
    		}
    		cout<<C(m+n, n)<<endl;
    	}	
        return 0;
    }
    
  • 相关阅读:
    LeetCode(287)Find the Duplicate Number
    LeetCode(290) Word Pattern
    LeetCode(205)Isomorphic Strings
    LeetCode(201) Bitwise AND of Numbers Range
    LeetCode(200) Number of Islands
    LeetCode(220) Contains Duplicate III
    LeetCode(219) Contains Duplicate II
    命令行执行Qt程序
    LeetCode(228) Summary Ranges
    redis 的安装和使用记录
  • 原文地址:https://www.cnblogs.com/Tenshi/p/15001595.html
Copyright © 2011-2022 走看看