0
点赞
收藏
分享

微信扫一扫

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

boomwu 2022-06-24 阅读 56

原题链接: ​​https://codeforces.com/contest/1407/problems​​

A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define
测试样例

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数组,你可以通过删除最多A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define_02的元素。你要使得最后的数组定义的数组和(即奇位加,偶位减)为A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_ios_03,请输出该数组。

解题思路: 这道题放在签到题,肯定是简单的。我们看:数组中只有A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_数组_04A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define_05两种元素,那么是不是说明A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_数组_04的数量或者A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define_05的数量有一个A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_ios_08 那么我们删除另一个数量小的是不是可以达到删除数量的限制。也就是说我们最后的数组要么只保留A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_思维_09,要么只保留A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_ios_03,这样达到的效果显而易见,我们数组定义的和为A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_ios_03当然,特殊情况在于只保留A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define_05的时候,我们要使得奇偶位置上的数目相同,那么我们只要判断A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_#define_05数量的奇偶性,如果为奇,就少输出一个。 (由于这个的限制,故如果A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_ios_03A. Ahahahahahahahaha (构造+思维) Codeforces Round #669 (Div. 2)_思维_09数量相等时,就应该以0为主导。)OK,具体看代码。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

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;
}


举报

相关推荐

0 条评论