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. JAVA 的wait(), notify()与synchronized同步机制

    转自:http://blog.csdn.net/zyplus/article/details/6672775 在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过syn ...

  2. 网站优化不等于搜索引擎优化SEO

    对于SEO相信搞网络营销的人基本上都知道这个名词,英文全称为search engine optimization,中文一般叫搜索引擎优化,也有的叫搜索引擎定位(Search Engine Positi ...

  3. LeetCode 第 338 题 (Counting Bits)

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  4. form表单的enctype属性

    ①application/x-www-form-urlencoded : 数据被编码为名称/值对. ②multipart/form-data : 数据被编码为一条消息,页上的每个控件对应消息中的一个部 ...

  5. Mirror--如何对运行中的镜像端点更换证书

    如果使用证书配置镜像时,没有设置证书的时间,则默认证书有效期为一年,当证书快过期时,需要更换证书. 下面代码演示如何对正在运行的镜像更换证书 --=========================== ...

  6. iOS10网络权限数据

    参考地址:1.http://www.cocoachina.com/ios/20180723/24274.html   https://blog.csdn.net/wang_bo_justone/art ...

  7. MyEclipse安装及破解步骤

    MyEclipse2013 (32+64)下载地址(建议使用迅雷下载)http://downloads.myeclipseide.com/downloads/products/eworkbench/2 ...

  8. ubuntu 安装ftp,配置,和java调用

    1:安装 ftp服务器 sudo apt-get install vsftpd 自动安装使用的是主机的用户名和密码:liyafei,1367xxx 访问方式,ftp://localhost:21,ft ...

  9. Log4net 日志传到 graylog监控

    graylog是java的一个日志监控插件.存储用的是mongoDB,效率还是挺高的.不过嘛,文档太少了,安装和配置都很不容易. 官网:http://www.graylog.org/ 在graylog ...

  10. CSS中 Zoom属性

    CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...