百度(1条未读通知)首页2019牛客暑期多校训练营(第三场)_ACM/noip/noil…B- Crazy Binary String_2019牛客暑期多校训练营(时间限制:CC++1秒,其他语言2秒
空间限制:CC++262144K,其他语言524288K
64bit lO Format: %lld
题目描述x
ZYB loves binary strings(strings that only contains 0'and 1). And he loves Itextit(equal binary strings) more, where the number of O' and the number of 1"in the string are equal
ZYB wants to choose a substring from an original string T so that it is an Itextitequal binary string with the longest length possible. He also wants to choose a subsequence of T which meets the
same requirements
A string v is a substring of a string w if v is empty, or there are two integers I andr(1<LSTslwl)such that v =w) wl+I. Wr. A string v is a subsequence of a string w if it can be derived from w
输入描述:
The first line of the input contains a single integer N(I<N<100000), the length of the original string T. The second line contains a binary
string with exactly N characters, the original string T
输出描述
Print two integers A and B, denoting the answer for substring and subsequence respectively
示例1
复制
For simplicity, you only need to output the maximum possible length. Note that the empty string is both a substring and a subsequence of any string
输入描述
The first line of the input contains a single integer N(1<N<100000), the length of the original string T. The second line contains a binary
string with exactly N characters, the original string T
输入
8
01001001
输出
4 6
#include <bits/stdc++.h>
using namespace std;
struct node{
int maxp,minp;
};
bool cmp(node a,node b){
return a.maxp-a.minp>b.maxp-b.minp;
}
int main() {
int n,zero_c=0,one_c=0,dif;
scanf("%d",&n);
string s;
cin>>s;
vector<node>cc(3*n,{-1,-1});
cc[n+0].minp=0;
for(int i=0;i<n;i++){
if(s[i]=='0')zero_c++;
else if(s[i]=='1') one_c++;
dif=zero_c-one_c;
if(cc[n+dif].minp==-1)cc[n+dif].minp=i+1;
if(cc[n+dif].maxp==-1)cc[n+dif].maxp=i+1;
if(cc[n+dif].minp>i+1)cc[n+dif].minp=i+1;
else if(cc[n+dif].maxp<i+1)cc[n+dif].maxp=i+1;
}
sort(cc.begin(), cc.end(), cmp);
printf("%d %d",cc[0].maxp-cc[0].minp,2*min(zero_c,one_c));
return 0;
}