QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#99264 | #6299. Binary String | Fanch100 | TL | 2ms | 13732kb | C++14 | 2.1kb | 2023-04-21 19:34:45 | 2023-04-21 19:34:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
const int INF = 0x3f3f3f3f;
int a[N], mx[N], val[N], sum[N], b[N], mov[N];
int n;
void init(){
string s; cin>>s;
n=s.size();
int cnt0=0, cnt1=0;
for (int i=0;i<n;++i) cnt0+=s[i]=='0', cnt1+=s[i]=='1';
if (cnt0<cnt1){
for (int i=1;i<=n;++i) a[i]=(s[i-1]=='0')?1:-1;
}
else for (int i=1;i<=n;++i) a[i]=(s[i-1]=='0')?-1:1;
mx[n+1]=-INF;
int sm=0;
for (int i=n;i>=1;--i) sm+=a[i], mx[i]=max(mx[i+1],sm);
int pos=0; sm=-INF;
for (int i=1;i<=n;++i) {
sum[i]=sum[i-1]+a[i];
// printf("i=%d sum=%d a=%d mx=%d\n",i,sum[i],a[i],mx[i]);
sm+=a[i];
if (a[i]==1){
sm=max(sm,0);
int tmp=max(sm,sum[i]+mx[i+1]);
if (!tmp) {pos=i; break;}
}
else sm+=a[i];
}
for (int i=1;i<=n;++i) b[i]=max(a[i],0);
pos--;
for (int i=1;i<=n;++i) a[i]=b[(i+pos-2)%n+1];
// for (int i=1;i<=n;++i) printf("%d",a[i]); puts("");
// printf("pos=%d\n",pos);
}
int fail[N];
void solve(){
init();
val[1]=-1; b[1]=0;
int pos=1, mx=0;
sum[1]=0;
for (int i=2;i<=n;++i) {
b[i]=0;
val[i]=val[i-1]+(a[i]==0?-1:1);
sum[i]=sum[i-1]+a[i];
// printf("i=%d sum=%d\n",i,sum[i]);
if (a[i]==1) {
val[i]=max(val[i],0);
if (val[i]<=0) pos=i;
mov[i]=sum[i-1]-sum[pos-1];
mx=max(mx,mov[i]);
// printf("i=%d mov=%d pos=%d\n",i,mov[i],pos);
}
}
// printf("mx=%d\n",mx);
for (int i=1;i<=n;++i){
if (a[i]==1){
b[(i-(mx-val[i])+n-1)%n+1]=1;
}
}
// for (int i=1;i<=n;++i) printf("%d",b[i]); puts("");
fail[1]=0;
for (int i=2,j=0;i<=n;++i){
while(j>0 && b[i]!=b[j+1]) j=fail[j];
if (b[i]==b[j+1]) j++;
fail[i]=j;
}
// printf("%d\n",fail[n]);
int ans=(n%(n-fail[n])==0?n-fail[n]:n);
printf("%d\n",ans+mx);
}
int main(){
int t; cin>>t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 13732kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Time Limit Exceeded
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...
output:
1 18 18 19 18 18 19 20 18 18 18 20 19 19 20 21 18 18 18 19 18 18 20 21 19 19 19 21 20 20 21 22 18 18 18 19 18 18 19 21 18 18 18 21 20 20 21 22 19 19 19 19 19 19 21 22 20 20 20 22 21 21 22 23 18 18 18 19 18 18 19 20 18 18 18 20 19 19 21 22 18 18 18 19 18 18 21 22 20 20 20 22 21 21 22 23 19 19 19 19 1...