Alexandra has an even-length array 𝑎, consisting of 0s and 1s. The elements of the array are enumerated from 1 to 𝑛. She wants to remove at most 𝑛2 elements (where 𝑛 — length of array) in the way that alternating sum of the array will be equal 0 (i.e. 𝑎1−𝑎2+𝑎3−𝑎4+…=0). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don’t have to be consecutive.
For example, if she has 𝑎=[1,0,1,0,0,0] and she removes 2nd and 4th elements, 𝑎 will become equal [1,1,0,0] and its alternating sum is 1−1+0−0=0.
Help her!
Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤103). Description of the test cases follows.
The first line of each test case contains a single integer 𝑛 (2≤𝑛≤103, 𝑛 is even) — length of the array.
The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (0≤𝑎𝑖≤1) — elements of the array.
It is guaranteed that the sum of 𝑛 over all test cases does not exceed 103.
Output
For each test case, firstly, print 𝑘 (𝑛2≤𝑘≤𝑛) — number of elements that will remain after removing in the order they appear in 𝑎. Then, print this 𝑘 numbers. Note that you should print the numbers themselves, not their indices.
We can show that an answer always exists. If there are several answers, you can output any of them.
Example
inputCopy
4
2
1 0
2
0 0
4
0 1 1 1
4
1 1 0 0
outputCopy
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.
思路:
最多删n/2个数,所以可以把0或者1(取决于数量)全部删掉。如果删掉的是0,剩下的1为奇数就再删一个。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
int a[maxn];
int main() {
int T;scanf("%d",&T);
while(T--) {
int n;scanf("%d",&n);
int zero = 0,one = 0;
for(int i = 1;i <= n;i++) {
scanf("%d",&a[i]);
if(a[i]) one++;
else zero++;
}
if(one <= n / 2) {
printf("%d\n",zero);
for(int i = 1;i <= zero;i++) {
printf("0 ");
}
printf("\n");
} else {
printf("%d\n",one / 2 * 2);
for(int i = 1;i <= one / 2 * 2;i++) {
printf("1 ");
}
printf("\n");
}
}
return 0;
}