原题链接: https://codeforces.com/contest/1407/problems
测试样例
input
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
output
1
0
1
0
2
1 1
4
1 1 0 0
Note
In the first and second cases, alternating sum of the array, obviously, equals 0.
In the third case, alternating sum of the array equals 1−1=0.
In the fourth case, alternating sum already equals 1−1+0−0=0, so we don’t have to remove anything.
题意: 给你一个01数组,你可以通过删除最多的元素。你要使得最后的数组定义的数组和(即奇位加,偶位减)为
,请输出该数组。
解题思路: 这道题放在签到题,肯定是简单的。我们看:数组中只有和
两种元素,那么是不是说明
的数量或者
的数量有一个
。 那么我们删除另一个数量小的是不是可以达到删除数量的限制。也就是说我们最后的数组要么只保留
,要么只保留
,这样达到的效果显而易见,我们数组定义的和为
。当然,特殊情况在于只保留
的时候,我们要使得奇偶位置上的数目相同,那么我们只要判断
数量的奇偶性,如果为奇,就少输出一个。 (由于这个的限制,故如果
和
数量相等时,就应该以0为主导。)OK,具体看代码。
AC代码
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n,a[maxn];
int cnt[2];//统计0和1的数目。
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
cnt[0]=cnt[1]=0;//初始化。
rep(i,1,n){
cin>>a[i];
if(a[i])
cnt[1]++;
else
cnt[0]++;
}
//判断哪个大,相等时以0为主导。
if(cnt[0]>=cnt[1]){
cout<<cnt[0]<<endl;
rep(i,1,cnt[0])
cout<<0<<" ";
cout<<endl;
}
else{
if(cnt[1]%2)cnt[1]--;//由于我们要让数组规定和为0,故要确保奇偶位置数目相同。
cout<<cnt[1]<<endl;
rep(i,1,cnt[1])
cout<<1<<" ";
cout<<endl;
}
}
}
return 0;
}