CodeForces 556A

Description

Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.

Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacentpositions
in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n?-?2 as
a result.

Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.

Input

First line of the input contains a single integer n (1?≤?n?≤?2·105),
the length of the string that Andreid has.

The second line contains the string of length n consisting only from zeros and ones.

Output

Output the minimum length of the string that may remain after applying the described operations several times.

Sample Input

Input

4
1100

Output

0

Input

5
01010

Output

1

Input

8
11101111

Output

6

Hint

In the first sample test it is possible to change the string like the following: 1100→10→(empty).

In the second sample test it is possible to change the string like the following: 01010→010→0.

In the third sample test it is possible to change the string like the following:11101111→111111.

题意:

给你一行整数,相邻两个数如果是01或者是10则去掉,如此下去,直到不可以操作为止,求出最后剩下的最短长度。

思路:

看1或0的个数,求出其最小的,用总长度减去这个最小长度的两倍。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
char  a[200010];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%s",&a);
        int a0=0,a1=0,ans=0;
        for(int i=0;i<n;i++)
        {
            if(a[i]=='0')
                a0++;

        }
        a1=n-a0;
        if(a0<=a1)
        ans=n-2*a0;
        else   if(a1<a0)
            ans=n-2*a1;

        printf("%d\n",ans);

    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 15:10:33

CodeForces 556A的相关文章

Problem A CodeForces 556A

Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we

CodeForces 556A 解题心得

原题: Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation:

Codeforces 556A Case of the Zeros and Ones 消除01串

题意:给出一个只由0和1组成的串,每次操作可以选择两个相邻的位置且这两个位置一个为0,一个为1(顺序无所谓)并删去这两个位置得到一个新串,然后可以对新串继续操作.操作次数无限制,令原串的长度不断缩短直至不能再短.求这个最短的长度为多少. 虽然题目不难,不过还是证明一下自己思路的正确性.这里我主要是想说一下证明的思路.直观上看,只要发现两个相邻的是0和1就把它删去,不用考虑不同的删法会不会对后面造成影响.比如有如下串:.......101........ 删掉10和删掉01都无所谓.这里我们证明一

CodeForces - 556A Case of the Zeros and Ones

Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any t

Codeforces 556A Case of the Zeros and Ones(消除01)

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones. Once he thought about a string of

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st