time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Alexandra has an even-length array aa, consisting of 00s and 11s. The elements of the array are enumerated from 11 to nn. She wants to remove at most n2n2 elements (where nn — length of array) in the way that alternating sum of the array will be equal 00 (i.e. a1−a2+a3−a4+…=0a1−a2+a3−a4+…=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 a=[1,0,1,0,0,0]a=[1,0,1,0,0,0] and she removes 22nd and 44th elements, aa will become equal [1,1,0,0][1,1,0,0] and its alternating sum is 1−1+0−0=01−1+0−0=0.
Help her!
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031≤t≤103). Description of the test cases follows.
The first line of each test case contains a single integer nn (2≤n≤1032≤n≤103, nn is even) — length of the array.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1) — elements of the array.
It is guaranteed that the sum of nn over all test cases does not exceed 103103.
Output
For each test case, firstly, print kk (n2≤k≤nn2≤k≤n) — number of elements that will remain after removing in the order they appear in aa. Then, print this kk 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
input
Copy
4 2 1 0 2 0 0 4 0 1 1 1 4 1 1 0 0output
Copy
1 0 1 0 2 1 1 4 1 1 0 0Note
In the first and second cases, alternating sum of the array, obviously, equals 00.
In the third case, alternating sum of the array equals 1−1=01−1=0.
In the fourth case, alternating sum already equals 1−1+0−0=01−1+0−0=0, so we don't have to remove anything.
解题说明:此题是一道模拟题,最容易想到的就是1的个数少于n/2,那就把1全部删掉就行了。当1的个数较多时,删除0,然后保留偶数个1即可。
#include <stdio.h> int main() { int t, n, i; int a[1005]; int zero, one; scanf("%d", &t); while (t--) { scanf("%d", &n); zero = 0; one = 0; for (i = 0; i<n; i++) { scanf("%d", &a[i]); if (a[i] == 1) { one++; } else { zero++; } } if (one <= n / 2) { printf("%d\n", zero); for (i = 0; i < zero; i++) { printf("0 "); } printf("\n"); } else { printf("%d\n", one / 2 * 2); for (i = 0; i < one / 2 * 2; i++) { printf("1 "); } printf("\n"); } } return 0; }
