1到n内0,1个数相同的个数的最长字串

\(i>=j\)

\[1的个数=0的个数
\]

\[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])
\]

这里把\((j-1)\)替换为\(j\)

\[2*sum[i]-2*sum[j]=i-j
\]

\[2*sum[i]-i=2*sum[j]-j
\]

枚举i,然后求前面和\(2*sum[i]-i\)相同的最小标号

这里可能有负数,所以map就好了

当然,因为\(2*sum[i]-i\)的值是在区间[-n,n]的

所以你可以直接+n用数组桶一下

不告诉你我写过线段树

#include <bits/stdc++.h>
#define ls rt<<1
#define rs rt<<1|1
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn=1e5+7;
const int inf=0x3f3f3f3f;
int n,a[maxn],sum[maxn];
//map<int,int> mi;
int mi[maxn*2];
int main()
{
scanf("%d",&n);
FOR(i,1,n) scanf("%1d",&a[i]),sum[i]=sum[i-1]+a[i];
int ans=0;
memset(mi,inf,sizeof(mi));
mi[n]=0;
FOR(i,1,n) {
int tmp=n+2*sum[i]-i;
int zz=mi[tmp];
// if(!zz&&tmp!=0) zz=inf,mi[tmp]=inf;
ans=max(i-zz,ans);
mi[tmp]=min(zz,i);
}
cout<<ans;
return 0;
}

CF873B Balanced Substring的更多相关文章

  1. CF873B Balanced Substring (前缀和)

    CF873B Balanced Substring (前缀和) 蛮有意思的一道题,不过还是.....................因为CF评测坏了,没有试过是否可过. 显然求\(\sum[i][0] ...

  2. [Codeforces 873B]Balanced Substring

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

  3. 837B. Balanced Substring

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

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

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

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

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

  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. 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 ...

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

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

随机推荐

  1. 一个JS Class的“增删改查”

    function AA (){ var r={}; this.get = function(key){ return r[key]; } this.put = function(key,x){ r[k ...

  2. 一句替换bbcode

    $message=preg_replace('/\[[^\[\]]{1,}\]/','',$message);

  3. 【Jason】Jason拓展

      Java-script是最基础语言(即前端脚本),而jquery是基于javascript封装出来的包,这些包里面含有能调用ajax的方法. ajax是一种异步交互,局部刷新的一种形式,是一种通讯 ...

  4. 冒泡排序快速版(C)

    冒泡排序C语言版:在每轮排序中检查时候有元素位置交换,如果无交换,说明数组元素已经有序,无需继续排序 #include <stdio.h> #include <stdlib.h> ...

  5. SQL Server学习路径(文章目录)

    SQL Server文章目录 SQL Server文章目录(学习路径)  转自:http://www.cnblogs.com/CareySon/archive/2012/05/08/2489748.h ...

  6. android 打开系统相机,

    1.第一步在androidmanifest.xml中注册 <uses-permission android:name="android.permission.WRITE_EXTERNA ...

  7. XtraBackup完整备份与增量备份的原理

    MySQL数据库实现备份的操作包括完整备份和增量备份等,本文我们主要介绍一下增量备份和完整备份的原理,接下来我们就一起来了解一下这部分内容. 完整备份的原理: 对于InnoDB,XtraBackup基 ...

  8. SQLAlchemy技术文档(中文版)(全)

    原文链接:http://www.cnblogs.com/iwangzc/p/4112078.html(感谢作者的分享) sqlalchemy 官方文档:http://docs.sqlalchemy.o ...

  9. R之ddlpy函数学习[转载]

    转自:https://www.cnblogs.com/aloiswei/p/6032513.html 1.函数 ddply(.data, .variables, .fun = NULL, ..., . ...

  10. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...