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’个数相等,则 ...
随机推荐
- 我在B站学习 Javascript入门教程 基础
B站av9243452的一系列视频,适合学过其他编程语言的人观看,还挺不错的 共43节,该随笔为1~16节 Js介绍 如需使用外部文件,请在 <script> 标签的 "src& ...
- VSCode makedown增强插件
Markdown Preview Enhanced https://shd101wyy.github.io/markdown-preview-enhanced/#/zh-cn/
- Linux重新挂载磁盘
Linux下磁盘和目录的概念与WIN不同:比如,分了一个系统分区默认挂载了根(/)目录,根下还有其它目录,比如/user /lib等.如果系统分区不够用,可以再分出分支,把根下其它目录分别挂载出来,例 ...
- 转 MYSQL_GTID详解
http://blog.itpub.net/27067062/viewspace-2141906/ 一.GTID概述 GTID是MYSQL5.6新增的特性,GTID(Global Transac ...
- UVALive 7511 L - Multiplication Table 数学模拟题,暴力
给定一副表,问其是否合法. 思路:当全部是?的时候,是合法的. 如果不是,那么,就找到一个数字,把它拆成若干个a*b的形式,去判断其它点是否合法即可. 拆分数字的时候,只需要枚举到sqrt(n),因为 ...
- POI Java 导出数据到Excel
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifac ...
- CentOS Linux解决Device eth0 does not seem to be present【转】
在VMware里克隆出来的CentOS Linux,ifconfig...没有看到eth0,然后重启网卡又报下面错误. 故障现象: service network restartShutting do ...
- java多线程,如何防止脏读数据
多线程容易“非线程安全”的情况,是由于用了全局变量,而又没有很好的控制起情况.所以无论做什么程序,谨慎使用全局变量 "非线程安全"其实会在多个线程对同一个对象中的实例变量进行并发访 ...
- 使用:/usr/bin/phpize 报错
使用:/usr/bin/phpize 出现下面错误提示 Can't find PHP headers in /usr/include/php The php-devel package is requ ...
- PHPGGC学习----实践
本文首发于先知:https://xz.aliyun.com/t/5450 PHPGGC学习----理论部分对PHPGGC工具的使用方法有了一个基本的了解,接下来需要利用实践环境进行一个实践操作,巩固一 ...