Codeforces Round #669 (Div. 2) ------ Ahahahahahahahaha

46 篇文章 0 订阅
31 篇文章 0 订阅

题目

在这里插入图片描述

题意

给你一个长度为n的数组,数组元素由0或1构成,你可以进行随意的删除操作,一次只能删除一个元素,最多操作次数不能超过n / 2次。操作后的结果就是:奇数位数字之和等于偶数位数字之和。最后输出剩余的数组元素

题解

由于题目没有规定说删除次数最少,所以直接找出特殊情况。
如果题目里面0的个数大于等于n / 2, 那么直接保留n / 2个0即可,其余的都删除
如果题目里面1的个数大于等于n / 2且数目为偶数,那么直接保留所有1即可,其余0删除;如果是奇数,则保留num(1的数目) - 1个1,这样就是偶数个1,符合条件

AC代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
const int N = 1e3 + 15;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		int num1 = 0;
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			if (x == 1)
				num1++;
		}
		if (num1 <= n / 2) {
			cout << n / 2 << endl;
			for (int i = 0; i < n / 2; i++)
				cout << 0 << " ";
			cout << endl;
		}
		else {
			if (num1 % 2 == 0) {
				cout << num1 << endl;
				for(int i = 0; i < num1; i++)
					cout << 1 << " ";
				cout << endl;
			}
			else {
				cout << num1 - 1 << endl;
				for (int i = 0; i < num1 - 1; i++)
					cout << 1 << " ";
				cout << endl;
			}
		}
	}
	return 0;
}

Python 版ac代码

t = int(input())
while t:
    n = int(input())
    a = list(map(int, input().split()))
    num = 0
    for i in a:
        if i == 1:
            num += 1
    if num <= n / 2:
        n = int(n / 2)
        print(n)
        for i in range(n):
            print(0, end= " ")
        print()
    else:
        if num % 2 == 0:
            print(num)
            for i in range(num):
                print(1, end = " ")
            print()
        else:
            num = num - 1
            print(num)
            for i in range(num):
                print(1, end = " ")
            print()
    t -= 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值