题意:
给你一个长度为n(n是偶数)只包含0和1的串,让你删除不超过n/2个元素,使得原来的串,奇数位置的值的和等于偶数位置的和,输出删除后的串。
题解:
记录1的数目和0的数目,如果0的数目大于n/2,直接输出0的个数,否则就输出n/2个1。
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while(T--) {
int n;
cin >> n;
int x;
int cnt0 = 0,cnt1 = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
if(x == 0)cnt0++;
else cnt1++;
}
if(cnt0 >= cnt1) {
cout<<n/2<<'\n';
for(int i = 0; i < n/2; i++) {
printf("%d ",0);
}
} else {
if(cnt1 & 1)cnt1--;
cout<<cnt1<<'\n';
for(int i = 0; i < cnt1; i++) {
printf("%d ",1);
}
}
puts("");
}
return 0;
}