CF873B Balanced Substring (前缀和)
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 (前缀和)的更多相关文章
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- 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 ...
- Codeforces 873 B. Balanced Substring(前缀和 思维)
题目链接: Balanced Substring 题意: 求一个只有1和0的字符串中1与0个数相同的子串的最大长度. 题解: 我的解法是设1的权值是1,设0的权值是-1,求整个字符串的前缀和并记录每个 ...
- [Codeforces 873B]Balanced Substring
Description You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s ...
- 837B. Balanced Substring
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Balanced Substring
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string ...
- 【Cf edu 30 B. Balanced Substring】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CodeForces - 873B Balanced Substring(思维)
inputstandard input outputstandard output You are given a string s consisting only of characters 0 a ...
- Codeforces 873B - Balanced Substring(思维)
题目链接:http://codeforces.com/problemset/problem/873/B 题目大意:一个字符串全部由‘0’和‘1’组成,当一段区间[l,r]内的‘0’和‘1’个数相等,则 ...
随机推荐
- js千分位处理
一.去掉千分位 function removeThousands(num) { var x = num.split(','); return parseFloat(x.join("" ...
- P1067 多项式输出(模拟水题)
题目描述 一元nn次多项式可用如下的表达式表示: 其中,a_ix^iaixi称为ii次项,a_iai 称为ii次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: ...
- Codeforces Round #365 (Div. 2) A
Description Mishka is a little polar bear. As known, little bears loves spending their free time pla ...
- Java面向对象_Object类
Object类 是类层次结构的根类,每个类都是用Object类作为超类,所有对象(包括数组)都实现这个类的方法.所有类都是Object类的子类. 下面先说两个方法toString()和equals(O ...
- java Smaphore 控制并发线程数
概念: Semaphore(信号量)是用来控制同事访问特定资源的线程数量,它通过协调各个线程,已保证合理的使用公共资源. 应用场景: Semaphore 可以用于做流量控制,特别是共用资源有限的应用场 ...
- win10命令行压缩zip文件
1.下载winzip,下载winzip command line(官方) 2.使用命令 "C:\Program Files\WinZip\WZZIP.exe" C:\test\a. ...
- jQuery获取url
JS获取当前页面的URL信息 设置或获取对象指定的文件名或路径. <script> alert(window.location.pathname) </script> 设置或获 ...
- 从零开始的全栈工程师——js篇2.14(表单与计时器)
一.表单 Form input select textarea type=”radio/checkbox/password/button/text/submit/reset/” 表单的事件 oncha ...
- 《C++面向对象程序设计》之变量的生存期
<C++面向对象程序设计>之变量的生存期 静态生存期 (1)全局静态生存期:在函数体外声明,作用域从声明处开始到文件结尾处结束,称其有文件作用域,相当于全局变量 . (2)局部静态生存期: ...
- 构建第一个Spring Boot2.0应用之集成mybatis(六)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.步骤 方式一:利用配置文件配 ...