Ahahahahahahahaha codeforces#669A 思维好题

这篇博客讨论了如何帮助Alexandra从包含0和1的数组中移除元素,使得数组的交替和变为0。作者起初尝试匹配奇偶位置上的1,但发现可以通过移除特定0实现目标。当0的数量超过数组长度的一半时,只需保留0;否则,移除所有0并输出偶数个1。
摘要由CSDN通过智能技术生成

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值