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

    本文介绍gate io API 所有交易对 API 返回所有系统支持的交易对 URL: https://data.gateio.io/api2/1/pairs 示例: # Request GET: h ...

  2. UIImage添加滤镜

    UIImage *image =[UIImage imageNamed:"]; NSArray *arr = [NSArray arrayWithObjects:@"CISRGBT ...

  3. (转)使用git stash解决git pull时的冲突

    在使用git pull代码时,经常会碰到有冲突的情况,提示如下信息: error: Your local changes to 'c/environ.c' would be overwritten b ...

  4. c#图像灰度化、灰度反转、二值化

    图像灰度化:将彩色图像转化成为灰度图像的过程成为图像的灰度化处理.彩色图像中的每个像素的颜色有R.G.B三个分量决定,而每个分量有255中值可取,这样一个像素点可以有1600多万(255*255*25 ...

  5. pandas.drop/isnull/fillna/astype的用法

    删除表中的某一行或者某一列更明智的方法是使用drop,它不改变原有的df中的数据,而是返回另一个dataframe来存放删除后的数据. (1)清理无效数据 df[df.isnull()] #返回的是个 ...

  6. python 的 json 转换

    python 的 json 转换 本文为原创文章,禁止转载! 本文以 json.dumps()  和 json.loads() 方法进行 Python 数据和 json 格式之间转换,进行讲解 首先比 ...

  7. input的text输入框设置大一点

    <input type="text" style="height:51px;width:449px;font-size:12px;">12可以随意改 ...

  8. animation-fill-mode

    animation-fill-mode: none:默认值.不设置对象动画之外的状态 forwards:结束后保持动画结束时的状态,但当animation-direction为0,则动画不执行,持续保 ...

  9. cmd中mysql中文乱码问题

    以下为自己亲试: 解决cmd中MySQL查询和命令返回的中文乱码问题 1.修改cmd字符集方法 rem 切换到UTF-8 chcp 65001 rem 切换到默认的GBK chcp 936 rem 美 ...

  10. iframe嵌套

    iframe基本内涵 通常我们使用iframe直接直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_iframe_sandbox.htm" ...