可以无限交换,那视为把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
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.