当前位置: 代码迷 >> 综合 >> CodeForces 873B Balanced Substring (预处理)
  详细解决方案

CodeForces 873B Balanced Substring (预处理)

热度:30   发布时间:2023-11-15 11:09:04.0

把0当成-1预处理所有前缀和即可

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
int n;
char s[maxn];
vector<int> g[maxn<<1];
int main(){cin>>n;int v=0,ans=0;g[maxn].push_back(0);scanf("%s",s);rep(i,0,n){if(s[i]=='0') v--;else v++;g[v+maxn].push_back(i+1);}rep(i,0,maxn<<1){int sz=g[i].size();if(sz==0) continue;sort(g[i].begin(),g[i].end());ans=max(ans,g[i][sz-1]-g[i][0]);}cout<<ans;return 0;
}

 

  相关解决方案