CF873B Balanced Substring (前缀和)

蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过.

显然求\(\sum[i][0] - \sum[l][0] = \sum[i][1] - \sum[l][1]\)

\(\sum[i][0] - \sum[l][1] = \sum[i][0] - \sum[l][0]\)

然后hash一下DP即可.

#include <iostream>
#include <cstdio>
using namespace std;
#define rep(i,x,p) for(int i = x;i <= p;++ i)
const int maxN = 100000 + 7;
/*
设状态sum[i]
sum[i][0]表示前i位0数的个数
sum[i][1]表示前i位1数的个数
*/
int f[maxN][2]; // f[i][0]表示 sum[i][0] - sum[i][1]最远位置.
// f[i][1]表示 sum[i][1] - sum[i][0]最远位置.
int a[maxN];
char c[maxN];
int sum[maxN][2]; inline int max(int a,int b) {return a > b ? a : b;}
inline int min(int a,int b) {return a > b ? b : a;} inline int read() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
} int main() {
int n = 0,ans = 0;
n = read();
rep(i,1,n) scanf("%1d",&a[i]);
rep(i,1,n) {
sum[i][0] = sum[i - 1][0];
sum[i][1] = sum[i - 1][1];
a[i] == 0 ? sum[i][0] ++ : sum[i][1] ++;
if(sum[i][0] > sum[i][1]) {
int tmp = sum[i][0] - sum[i][1];
if(f[tmp][0]) ans = max(ans,i - f[tmp][0]);
else f[tmp][0] = i;
}
else {
int tmp = sum[i][1] - sum[i][0];
if(f[tmp][1]) ans = max(ans,i - f[tmp][1]);
else f[tmp][1] = i;
}
}
printf("%d\n", ans);
return 0;
}

CF873B Balanced Substring (前缀和)的更多相关文章

  1. CF873B Balanced Substring

    1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...

  2. codefroces 873 B. Balanced Substring && X73(前缀和思想)

    B. Balanced Substring You are given a string s consisting only of characters 0 and 1. A substring [l ...

  3. Codeforces 873 B. Balanced Substring(前缀和 思维)

    题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...

  4. [Codeforces 873B]Balanced Substring

    Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s  ...

  5. 837B. Balanced Substring

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Balanced Substring

    You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...

  7. 【Cf edu 30 B. Balanced Substring】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. CodeForces - 873B Balanced Substring(思维)

    inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...

  9. Codeforces 873B - Balanced Substring(思维)

    题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...

随机推荐

  1. maven POM总结

    可继承的字段 version property 其他占坑: parent import scope   Dependency_Management中的scope是可以被继承的,http://maven ...

  2. UiAutomator环境配置

    前提:下载jdk以及adt 一.配置java开发环境 1.新建系统变量 -——变量名:JAVA_HOME      变量值:C:\Program Files\Java\jdk1.7.0_80 2.编辑 ...

  3. Spark编程模型(中)

    创建RDD 方式一:从集合创建RDD makeRDD Parallelize 注意:makeRDD可以指定每个分区perferredLocations参数parallelize则没有. 方式二:读取外 ...

  4. css盒子模型 css3盒子相关样式

    1.内边距(内边距在content外,边框内) 内边距属性: padding          设置所有边距 padding-bottom     底边距 padding-left           ...

  5. String在方法中的传递方式(调用外部方法给String变量赋值时,未得到预期结果)

    示例: public class StringTraining { public static void changeStr(String str){ str = "137878" ...

  6. springMvc 添加定时任务

    1.创建定时类 import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stere ...

  7. A promise tomorrow is worth a lot less than trying today.

    A promise tomorrow is worth a lot less than trying today.明日的承诺远不及今日的行动.

  8. https 双向验证

    服务器配置 服务器秘钥   服务器公钥证书  ,客户端公钥证书 客户端配置  客户端秘钥+密码 服务器公钥证书 目前android验证ok,pc浏览器添加客户端秘钥证书  ,访问还是失败,待继续查找资 ...

  9. iOS 当使用FD_FullscreenPopViewController的时候遇到scrollView右滑手势无法使用的解决

    当我们在ViewController中有scrollView的时候, 可能会遇到右滑无法响应返回手势, 有以下解决办法: 自定义scrollView, 实现该scrollView的以下方法即可: @i ...

  10. 服网LNMP集群 w/ MySQL PaaS-1.0

    平台: arm 类型: ARM 模板 软件包: haproxy linux mysql nginx application server arm basic software fuwang infra ...