QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99266 | #6299. Binary String | Fanch100 | WA | 121ms | 13752kb | C++14 | 2.1kb | 2023-04-21 19:35:43 | 2023-04-21 19:35:45 |
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(){
ios::sync_with_stdio(false);
int t; cin>>t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13740kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 121ms
memory: 13752kb
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...
result:
wrong answer 1536th numbers differ - expected: '25', found: '24'