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. code和pre竟然有区别!!!!

    坑爹的,今天复习基础,本来写了个获取样式,为了显示出我的代码,才发现了 code和pre标签竟然是不同的. 1.用 JS 的代码注释风格来说 code 就是 // , <pre> 就是 / ...

  2. nginx大量TIME_WAIT的解决办法

    1.netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' 查看time_wait 很大 2.解决此问题需要对sysc ...

  3. Caused by: MetaException(message:Hive Schema version 2.1.0 does not match metastore's schema version 1.2.0 Metastore is not upgraded or corrupt)

    解决方案汇总: ()删除HDFS上的hive数据与hive数据库 hadoop fs -rm -r -f /tmp/hive hadoop fs -rm -r -f /user/hive ()删除My ...

  4. java——模拟新浪微博用户注册

    1.创建用户类,重写HashCode()和equals()方法: import java.util.*; public class User{ private String name; private ...

  5. B - Reverse and Compare 小小思维题

    http://agc019.contest.atcoder.jp/tasks/agc019_b 一开始的做法是, 用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案. 然后,如果翻转afucka ...

  6. C# 读写XML文件的方法

    C# 读写XML文件的方法 一.写XML文件 XmlDocument xmlDocument = new XmlDocument();xmlDocument.AppendChild(xmlDocume ...

  7. linux 修改myql 编码配置等信息参考

    mysql 配置: 1:编辑配置文件: vi /etc/my.cnf 2:修改配置如下: [client] password        = 123456 port            = 330 ...

  8. html便民查询各个工具类实例代码分享(支持pc和移动端)

    1.手机号码查询 <iframe id="api_iframe_51240" name="api_iframe_51240" src="&quo ...

  9. javascript的常用操作(二)

    Undefined 不是 Null 在 JavaScript 中, null 用于对象, undefined 用于变量,属性和方法. 对象只有被定义才有可能为 null,否则为 undefined. ...

  10. oracle最高账号sys的密码认证模式

    CONNECT USERNAME/PASSWORD@SERVERNAME AS SYSDBAconnect 是指连接到username是指用户名password是指密码servername是指服务名a ...