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 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.

Examples

input

41100

output

0

input

501010

output

1

input

811101111

output

6

Note

In the first sample test it is possible to change the string like the following: .

In the second sample test it is possible to change the string like the following: .

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

题意:大致意思是给出一个01串,可删去01或10直到不能再删,输出最终01串的长度

思路:这是一道贪心,顺序什么的并不重要。因此本着贪到贪无可贪的原则,我们只需输出所有的0和1的个数再减一减就可以A掉了

代码:

var
  s:array[1..200005] of char;
  i,j,n,k,m1,m2:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
    read(s[i]);
    if s[i]=‘1‘ then inc(m1);
    if s[i]=‘0‘ then inc(m2);
  end;
  writeln(abs(m1-m2));
end.
时间: 2024-10-19 14:26:56

CodeForces - 556A Case of the Zeros and Ones的相关文章

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

题目传送门 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 =

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 555B Case of Fugitive

题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个桥,给出每个岛屿的位置和桥的长度,问是否可以把n个岛屿连起来? 解题思路: 排序+贪心,对于n个岛屿,相邻的两个之间起点和端点可以转化为n-1个连接桥的长度区间,把区间升序排列. 对于m个桥升序排列,对于每一个桥枚举每个可以合法覆盖的区间,选取最优的,选取的时候满足L<bridge_length<

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

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

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 - 556C Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls