codeforces 556A Case of the Zeros and Ones

来源:互联网 发布:大型无人机价格 知乎 编辑:程序博客网 时间:2024/06/05 04:57

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 test(s)
Input

4
1100

Output

0

Input

5
01010

Output

1

Input

8
11101111

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: 这里写图片描述.

题目大意:给你一串二进制,相邻的0和1可以抵消,问抵消之后最后会剩下多少位数字。

解题思路:统计有几个1有几个0,大的减去小的就是答案。

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;typedef long long ll;const int N = 2e5 + 5;int main() {    int n;    scanf("%d\n", &n);    int cnt1 = 0, cnt2 = 0;    char a;    for (int i = 0; i < n; i++) {        scanf("%c", &a);            if (a == '0') cnt1++;        else cnt2++;    }    if (cnt1 < cnt2) swap(cnt1, cnt2);    printf("%d\n", cnt1 - cnt2);    return 0;}
0 0