返回顶部

Codeforces Round #669 (Div. 2) A. Ahahahahahahahaha (构造)

  • 题意:有一个长度为偶数只含\(0\)\(1\)的序列,你可以移除最多\(\frac{n}{2}\)个位置的元素,使得操作后奇数位置的元素和等于偶数位置的元素和,求新序列.

  • 题解:统计\(0\)\(1\)的个数,如果\(0\)的个数大于\(\frac{n}{2}\),那么直接输出\(n/2\)\(0\),否则输出所有\(1\)(个数必须为偶).

  • 代码:

    int t;
    int n;
    int a[N];
     
    int main() {
        //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    	t=read();
    	while(t--){
    		n=read();
    		int cnt0=0;
    		int cnt1=0;
    		for(int i=1;i<=n;++i){
    			a[i]=read();
    			if(a[i]%2==0) cnt0++;
    			else cnt1++;
    		}
     
    		if(cnt0>=n/2){
    			printf("%d\n",n/2);
    			for(int i=1;i<=n/2;++i) printf("0 ");
    		}
    		else{
    			if(cnt1%2==1) cnt1--;
    			printf("%d\n",cnt1);
    			for(int i=1;i<=cnt1;++i) printf("1 ");
    		}
    		puts("");
    	}
     
        return 0;
    }
    
posted @ 2020-09-12 20:10  _Kolibri  阅读(194)  评论(0)    收藏  举报