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;}
- CodeForces 556A Case of the Zeros and Ones
- CodeForces-556A Case of the Zeros and Ones
- codeforces 556A Case of the Zeros and Ones
- Codeforces 556A:Case of the Zeros and Ones
- CodeForces 556A Case of the Zeros and Ones
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- Codeforces 556A Case of the Zeros and Ones 消除01串
- CodeForces NO.556A Case of the Zeros and Ones(stack,110101010)
- A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
- Codeforces Round #310 (Div. 2)-A. Case of the Zeros and Ones
- coderforce A. Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- CF#310-A-Case of the Zeros and Ones-链表模拟
- the Zeros and Ones
- hdu--2191
- MP4 文件格式
- Android 使用AsyncTaskLoader获取手机联系人
- codeforces 556B Case of Fake Numbers
- 全排列hash-康拓展开
- codeforces 556A Case of the Zeros and Ones
- LeetCode----Permutation Sequence
- java中form以post、get方式提交数据中文乱码问题总结
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- ubuntu下手动安装python源码
- Bellman-Ford||SPFA-POJ-3259-Wormholes
- Javascript--工厂模式、构造函数、原型
- Java中throws和throw的区别讲解
- android学习之1:拍照保存图片