Case of the Zeros and Ones

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 length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions 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

41100

Output

0

Input

501010

Output

1

Input

811101111

Output

6

程序分析:此题的意思就是找出0比1多多少个,或者是1比0多多少个。也就是0和1的差。所以我需要分别统计出0和1的个数就好,然后在做差运算即可。

程序代码:

#include<iostream>

using namespace std;
char a[200005];
int main()
{
    int n,l,b=0,t,c=0;
    cin>>n;
    for(l=0;l<n;l++)
    {    cin>>a[l];

        if(a[l]==‘1‘)
        c++;
        else b++;
    }
        if(b>c)
        t=b-c;
        else t=c-b;
    cout<<t<<endl;
        return 0;
}
时间: 2024-10-12 22:18:04

Case of the Zeros and Ones的相关文章

A. Case of the Zeros and Ones----解题报告

A. Case of the Zeros and Ones 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. Conside

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

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 556A Case of the Zeros and Ones 消除01串

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

Codeforces Round #310 (Div. 2)

A. Case of the Zeros and Ones 题目大意: 给出一个只含0和1的字符串,当是0和1相邻的话,这两个字符就可以删除,问最后不能删除的字符有多少个? 解题思路: 只需要分别统计出来0和1的个数,然后相减之后的绝对值就是答案. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdl

ZOJ3582---Back to the Past

Back to the Past Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Recently poet Mr. po encountered a serious problem, rumor said some of his early poems are written by others. This brought a lot of trouble to Mr. po, so one day he

Little Bishops uva861

Little Bishops A bishop is a piece used in the game of chess which is played on a board of square grids. A bishop can only move diagonally from its current position and two bishops attack each other if one is on the path of the other. In the followin

matlab公共函数之保存YUV数据

matlab保存图像为YUV格式的脚本函数 % function flag = saveYUVData(filename,Y,U,V,format) % input params. % filename: saving data path % Y: Y data, res. width x height % U: U data, res. width/2 x height/2 for NV12, NV21 and YUV420P, width x height for YUV444 % V: V