Alexandra has an even-length array a, consisting of 0s and 1s. The elements of the array are enumerated from 1 to n. She wants to remove at most n2 elements (where n — length of array) in the way that alternating sum of the array will be equal 0 (i.e. a1−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] and she removes 2nd and 4th elements, a 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 t (1≤t≤103). Description of the test cases follows.
The first line of each test case contains a single integer n (2≤n≤103, n is even) — length of the array.
The second line contains n integers a1,a2,…,an (0≤ai≤1) — elements of the array.
It is guaranteed that the sum of n over all test cases does not exceed 103.
Output
For each test case, firstly, print k (n2≤k≤n) — number of elements that will remain after removing in the order they appear in a. Then, print this k 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.
一开始想着通过匹配奇偶位置上的1的个数来达到平衡 ,后来发现如果去掉1中间的0也可以改变其奇偶关系就放弃了。其实A大多都是思维题,这才发现如果一串数字是0占比大于n/2,我只要输出0就够了。那如果1>n/2呢,我需要去掉所有的0然后根据1的奇偶个数来输出,只输出偶数个1就可以了
#include<map>
#include<stack>
#include<queue>