Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

链接:

https://codeforces.com/contest/1251/problem/B

题意:

A palindrome is a string t which reads the same backward as forward (formally, t[i]=t[|t|+1?i] for all i∈[1,|t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and 0 are palindromes.

You have n binary strings s1,s2,…,sn (each si 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 x,a,y,b such that 1≤x,y≤n and 1≤a≤|sx| and 1≤b≤|sy| (where x and y are string indices and a and b are positions in strings sx and sy respectively),
swap (exchange) the characters sx[a] and sy[b].
What is the maximum number of strings you can make palindromic simultaneously?

思路:

考虑所有串的长度,当有奇数长度存在时,保证可以交换出全部,当没有时, 可能存在0或1为奇数,不能配对,有奇数可以将奇数减位,同时保证剩下的偶数相等。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

int n;
string s;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int one = 0, zer = 0;
        int q = 0;
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
        {
            cin >> s;
            int len = (int)s.length();
            if (len%2)
                q++;
            for (int j = 0;j < len;j++)
            {
                if (s[j] == '0')
                    zer++;
                else
                    one++;
            }
        }
        if (q > 0)
            printf("%d\n", n);
        else
        {
            if (one%2 || zer%2)
                printf("%d\n", n-1);
            else
                printf("%d\n", n);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11779066.html

时间: 2024-10-07 23:45:41

Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes的相关文章

Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer

链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n digits (n is between 1 and 3?105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swappi

Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard

链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains 26 buttons (one for each letter of the Latin alph

Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

链接: https://codeforces.com/contest/1251/problem/D 题意: You are the head of a large enterprise. n people work at you, and n is odd (i.?e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it,

Educational Codeforces Round 75 (Rated for Div. 2)

(体验到了胡出一道题但是写锅的绝望呢) A: 送分题. #include<bits/stdc++.h> #define maxn 100005 #define maxm 500005 #define inf 0x7fffffff #define ll long long using namespace std; bool may[30]; char str[maxn]; inline int read(){ int x=0,f=1; char c=getchar(); for(;!isdigi

Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[200007];int n;long long s;bool check(int x){ int num=0; long long sum=s; for(int i=n;i;--i){ if(a[i].first>=x) ++num; else if(a[i].second>=x&&s

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.