ACM----CodeForces - Ahahahahahahahaha

Problem Description:

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. a1a2+a3a4+=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 11+00=01−1+0−0=0.

Help her!

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1031≤t≤103). Description of the test cases follows.

The first line of each test case contains a single integer nn (2n1032≤n≤103, nis even)  — length of the array.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai10≤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 (n2knn2≤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
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 00.

In the third case, alternating sum of the array equals 11=01−1=0.

In the fourth case, alternating sum already equals 11+00=01−1+0−0=0, so we don't have to remove anything.

题目理解:给定一个偶数长度,由0和1组成的数组a。数组元素的编号从1到n。每个数组最多能够去除n/2个数字使得数组的交替和等于0(a1-a2+a3-a4+…=0)。也就是说编号为奇数的数字和等于编号为偶数的数字和。你可以去除任意位置的数字。
比如,如果你有一个数组a = [1,0,1,0,0,0],你可以去除编号为2和4的数字,这样a会变成[1,1,0,0],其交替和为1-1+0-0=0。

题目分析:首先数组是由0和1两种元素组成,且长度为偶数,所以0和1两种元素至少有一种元素的个数大于等于n/2,而题目中要求去掉的元素个数小于等于n/2,所以我们可以将个数少于n/2的元素去掉。可分为以下几种情况:

  1.0的个数大于等于1的个数时,去掉所有元素1,数组中剩下的元素都为0,交替相加后即为0

  2.0的个数小于1的个数时,当0和1的个数都为偶数时,去掉所有的元素0,偶数个1交替相加最后结果为0;当0和1的个数都为奇数时,由于1的个数-0的个数大于等于2(因为n是偶数,0和1的个数都是奇数,所以在0和1的个数不相等的情况下,差会大于等于2),所以去除一个1之后,1的数量仍然大于等于n/2。将所有的元素0去掉后,去掉一个元素1使数组中剩余的元素1为偶数个。交替相加后结果为0。

AC代码:

 1 #include<stdio.h>
 2 int main(){
 3     int num;
 4     int n;
 5     scanf("%d",&n);
 6     while(n--){
 7         int count0=0,count1=0;
 8         int a[10000];
 9         scanf("%d",&num);
10         for(int i=0;i<num;i++){
11             scanf("%d",&a[i]);
12             if(0==a[i]) count0++;
13             else if(1==a[i]) count1++;
14         }
15         if(count0>=count1){
16             printf("%d\n",count0);
17             for(int i=0;i<count0;i++)
18                 printf("0 ");
19             printf("\n");
20         }
21         if(count1>count0){
22             if(count1%2==0){
23                 printf("%d\n",count1);
24                 for(int i=0;i<count1;i++)
25                     printf("1 ");
26                 printf("\n");
27             }
28             if(count1%2!=0){
29                 printf("%d\n",count1-1);
30                 for(int i=0;i<count1-1;i++)
31                     printf("1 ");
32                 printf("\n");
33             }
34         }
35     } 
36     return 0;   
37 }

 

posted @ 2021-01-13 21:19  AA、  阅读(119)  评论(0编辑  收藏  举报