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. 2018/03/31 每日一个Linux命令 之 date

    date 命令主要用于查看和修改时间和时区 -- 这里主要学习基本的查看和设置时间和时区的方法. 直接显示日期 date '+%D' 效果 vagrant@hong:~$ date '+%D' 03/ ...

  2. AndroidStudio自定义TODO

    1.增加自定义TODO标记 Preferences -> Editor -> TODO,然后点击左下角的加号,输入想要自定义的TODO的正则 输入\bX\b.*(X为TODO标签的名字), ...

  3. isScroll的滚动组件的用法

    <div class="wrapper">  <ul>     <li>1</li>     <li>2</li& ...

  4. 人活着系列之芳姐和芳姐的猪(Floyd)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2929 这个题一方面数据水,另一方面就是思维水, ...

  5. 在django项目中使用django-ckeditor

    安装django-ckeditor pip install django-ckeditor 安装Pillow Pillow是python的一个图像处理库,django-ckeditor需要依赖该库.最 ...

  6. svg绘图工具raphael.js的使用

    1.raphael.js svg画图的开源库,支持IE8+ 官方api: http://dmitrybaranovskiy.github.io/raphael/reference.html Githu ...

  7. pycharm跳到指定的行

    ctrl+g  弹出一个框,输入要跳的行数 ctrl+home  光标移到第一行 ctrl+end 光标移到最后一行

  8. ftp命令行敲不了

    最先安装了vsftpd,但是命令行敲ftp老是不行 解决方案:ftp命令是ftp客户端,vsftp是ftp服务器,两者不是一个概念.你需要安装ftp客户端 yum install ftp 可以自动安装 ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON ZoomImageFactor2

    zw版[转发·台湾nvp系列Delphi例程]HALCON ZoomImageFactor2 procedure TForm1.Button1Click(Sender: TObject);var op ...

  10. 跑道标识和那些复杂的灯光系统 and 简介、编号、参数、标志及数量 and 飞机跑道标准与参数

    http://www.360doc.com/content/16/0616/12/32670666_568219786.shtml http://news.carnoc.com/list/365/36 ...