Educational Codeforces Round 30 Balanced Substring 前缀和
来源:互联网 发布:linux文件内容复制 编辑:程序博客网 时间:2024/05/01 20:59
我是傻逼啊
没想一下,直接按 二分做的
其实就是 0 1 记录前缀和问题
我代码中的 mp 作用就是 映射 i 位置 和 前 i 个元素的和,,,
当有两个前缀和想同时,说明他们之间的和是0,也就是我们要找的长度
----------------------------------------------------
更新一下,这个 “ 他们之间的和是0 ” ,意思是 0 1 数量相同,我们记录的 x y 的差值是零,
对于每一个差值,我们记录最小的那个 下标 这样最后我们找到 相同差值的时候,一定值最长的区间
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;typedef long long ll;const int maxn = 100000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;int n, x = 0, y = 0;char s[maxn];map<int,int> mp;int main() { scanf("%d%s", &n, s+1); int ans = 0; mp[0] = 0; for(int i = 1; i <= n; ++i) { if(s[i] == '1') { x++; } else y++; if(mp.count(y-x)) { ans = max(ans, i-mp[y-x]); } else { mp[y-x] = i; } } printf("%d", ans); return 0;}
阅读全文
1 0
- Educational Codeforces Round 30 Balanced Substring 前缀和
- Educational Codeforces Round 30 B.Balanced Substring
- Educational Codeforces Round 30 B. Balanced Substring
- Educational Codeforces Round 30 Balanced Substring
- Educational Codeforces Round 30 B. Balanced Substring
- Educational Codeforces Round 30-B-Balanced Substring
- (Codeforces 873B)B. Balanced Substring [前缀和]+思维
- CodeForces 873B Balanced Substring(前缀和+思维)
- Educational Codeforces Round 11(C)尺取+前缀和
- Educational Codeforces Round 21 D. Array Division(前缀和,二分)
- Educational Codeforces Round 28 Four Segments 前缀和 + 思维
- Educational Codeforces Round 30
- Educational Codeforces Round 30
- Educational Codeforces Round 30
- Educational Codeforces Round 30
- codeforces873B Balanced Substring(前缀和)
- cf873B-Balanced Substring(前缀和)
- Codeforces 873B Balanced Substring(利用前缀和找和为0的子串)
- [Score, ACM/ICPC Seoul 2005, UVA1585]
- Windows7以上使用WFP驱动框架实现IP数据包截取(一)
- 【Openjudge】出栈序列统计
- 阅读文章:Incorporating Knowledge Graph Embeddings into Topic Modeling
- shiro自定义realm无法使用注解
- Educational Codeforces Round 30 Balanced Substring 前缀和
- [Molar Mass, ACM/ICPC Seoul 2007, UVA1586]
- 矩阵乘法(Strassen 算法实现)
- 设计全局ER模型 数据库系统原理(2007版) 课程代码4735 笔记
- UVa 1471 Defense Line 防线
- 二叉树-中序遍历
- Spring框架的注释问题
- Java基础之逐步击破----九九乘法表
- 网站图片应如何SEO优化?