[Codefroce 1251B] Binary Palindromes

可以无限交换,那视为把x个1,y个0分配就行,贪心算法,优先满足短的。需要奇数个数,尽量破坏x,y中的的奇数,因为偶数可以随意分成奇数,但剩下一个1,一个0不能凑偶数

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

int len[1001];

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,x = 0,y = 0; scanf("%d",&n);
		for (int i = 1; i <= n; i++) {
			char s[100];
			cin >> s;
			len[i] = strlen(s);
			for (int k = 0; k < len[i]; k++)
			  if (s[k] == '0') x++;
			  else y++;
		}
		sort(len+1,len+n+1);
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			bool ok = 1;
			if (len[i]&1) {
				if(x&1) x--;
				else {
					if(y) y--;
					else if(x) x--;
					else ok = 0;
				}
				len[i]--;
			}
			int t1 = min(x/2,len[i]/2);
			x -= t1*2;
			len[i] -= t1*2;
			int t2 = min(y/2,len[i]/2);
			y -= t2*2;
			len[i] -= t2*2;
			if (len[i]) ok = 0;
			if (!ok) break;
			ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
} 

A palindrome is a string which reads the same backward as forward (formally, for all ). Here denotes the length of a string
. For example, the strings 010, 1001 and 0 are palindromes.
You have
binary strings (each
consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings — there are no restrictions.
Formally, in one move you:
choose four integer numbers
such that and and (where and are string indices and and are positions in strings and
respectively),
swap (exchange) the characters
and
.
What is the maximum number of strings you can make palindromic simultaneously?
Input
The first line contains single integer
(
) — the number of test cases.
The first line on each test case contains single integer
(
) — the number of binary strings you have.
Next
lines contains binary strings — one per line. It’s guaranteed that
and all strings constist of zeroes and/or ones.
Output
Print
integers — one per test case. The -th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the -th test case.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值