codeforces 1251B. Binary Palindromes(贪心)

B. Binary Palindromes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A palindrome is a string ? which reads the same backward as forward (formally, ?[?]=?[|?|+1−?] for all ?∈[1,|?|]). Here |?| denotes the length of a string ?. For example, the strings 010, 1001 and 0 are palindromes.

You have ? binary strings ?1,?2,…,?? (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 1≤?,?≤? and 1≤?≤|??| and 1≤?≤|??| (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 ? (1≤?≤50) — the number of test cases.

The first line on each test case contains single integer ? (1≤?≤50) — the number of binary strings you have.

Next ? lines contains binary strings ?1,?2,…,?? — one per line. It’s guaranteed that 1≤|??|≤50 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.

Example
inputCopy
4
1
0
3
1110
100110
010101
2
11111
000001
2
001
11100111
outputCopy
1
2
2
2
Note
In the first test case, ?1 is palindrome, so the answer is 1.

In the second test case you can’t make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let’s make ?1=0110, ?2=111111 and ?3=010000.

In the third test case we can make both strings palindromic. For example, ?1=11011 and ?2=100001.

In the last test case ?2 is palindrome and you can make ?1 palindrome, for example, by swapping ?1[2] and ?1[3].

题意: 给出一些01串,可以交换任意两个串的一个字符。问能得到多少个回文串
思路:
首先奇数不用管,已有的奇数也全部变成偶数(因为奇数串中间放什么都可以)。
统计0和1的数目,每次只要放偶数个0和偶数个1就一定行。
将0和1都变成偶数(奇数减1)之后,实际上二者等价,你大可合并起来。

ACNEW

#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;

const int maxn = 2e5 + 7;

char s[55];

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n;scanf("%d",&n);
        vector<int>now;
        int one = 0,zero = 0;
        for(int i = 1;i <= n;i++) {
            scanf("%s",s + 1);
            int len = strlen(s + 1);
            now.push_back(len / 2 * 2);
            for(int j = 1;j <= len;j++) {
                if(s[j] == '1') {
                    one++;
                } else {
                    zero++;
                }
            }
        }
        
        zero = zero / 2 * 2;
        one = one / 2 * 2;
        
        sort(now.begin(),now.end());
        int ans = n;
        for(int i = 0;i < now.size();i++) {
            if(zero >= now[i]) {
                zero -= now[i];
            }
            else if(one >= now[i]) {
                one -= now[i];
            }
            else if(zero + one >= now[i]){
                if(zero >= one) {
                    zero -= now[i] - one;
                    one = 0;
                }
                else {
                    one -= now[i] - zero;
                    zero = 0;
                }
            }
            else {
                ans--;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

char a[1005];
vector<int>dat;
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        dat.clear();
        int n;scanf("%d",&n);
        int x = 0,y = 0;
        for(int i = 1;i <= n;i++)
        {
            scanf("%s",a);int len = (int)strlen(a);
            if(len % 2 == 1)
            {
                dat.push_back(len);
            }
            else dat.push_back(len);
            
            for(int i = 0;i < len;i++)
            {
                if(a[i] == '0')x++;
                else y++;
            }
        }
        sort(dat.begin(),dat.end());
        int ans = 0;
        for(int i = 0;i < dat.size();i++)
        {
            int now = dat[i];
            if(now % 2 == 1)
            {
                now--;
                if(x % 2 == 1)x--;
                else if(y % 2 == 1)y--;
            }
            if(x >= now)
            {
                x-=now;
                ans++;
            }
            else if(y >= now)
            {
                y-=now;
                ans++;
            }
            else if(x >= y)
            {
                if(y % 2 == 1)
                {
                    if(x >= now - y + 1)
                    {
                        now -= y;now++;y = 1;
                        x -= now;
                        ans++;
                    }
                }
                else
                {
                    if(x >= now - y)
                    {
                        now -= y;y=0;
                        x -= now;
                        ans++;
                    }
                }
            }
            else
            {
                if(x % 2 == 1)
                {
                    if(y >= now - x + 1)
                    {
                        now -= x;now++;x = 1;
                        y -= now;
                        ans++;
                    }
                }
                else
                {
                    
                    if(y >= now - x)
                    {
                        now -= x;x = 0;
                        y -= now;
                        ans++;
                    }
                }
            }
//            printf("%d %d\n",x,y);
        }
        printf("%d\n",ans);
        
    }
    
    return 0;
}

你可能感兴趣的:(#,codeforces)