QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#390787 | #6299. Binary String | OccDreamer | WA | 111ms | 3780kb | C++14 | 4.0kb | 2024-04-15 21:43:24 | 2024-04-15 21:43:25 |
Judging History
answer
//code by Emissary
#include<bits/stdc++.h>
#define vc vector
#define db double
#define fi first
#define se second
#define ll long long
#define mk make_pair
#define pb push_back
#define RI register int
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << " -_- " << endl
#define debug cerr << " ------------------- " << endl
#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)
#define NO puts("No")
#define YES puts("Yes")
//#define OccDreamer
//#define int long long
using namespace std;
namespace IO{
inline int read(){
int X=0, W=0; char ch=getchar();
while(!isdigit(ch)) W|=ch=='-', ch=getchar();
while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
return W?-X:X;
}
inline void write(int x){
if(x<0) x=-x, putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void sprint(int x){write(x), putchar(32);}
inline void eprint(int x){write(x), putchar(10);}
}using namespace IO;
const int MAXN = 1e7 + 7;
int topf;
int n, tot;
int nxt[MAXN];
bool mark[MAXN<<1];
char s[MAXN<<1];
struct range{
int l, r, num, nowtim;
}Q[MAXN], stk[MAXN];
inline void Rotate(int len){
for(int i=n+1;i<n+len;++i) s[i]=s[i-n];
for(int i=1;i<=n;++i) s[i]=s[i+len-1];
return ;
}
inline int Nxt(int x){return x==n?1:x+1;}
inline void solve(){
scanf("%s",s+1); n=strlen(s+1);
int fir=0;
for(int i=1;i<=n;++i)
if(s[i]!=s[1]){fir=i; break;}
if(!fir) return eprint(1);
Rotate(fir); int sum0=0, sum1=0;
int nowlen=0, num=s[1]-'0';
for(int i=1;i<=n;++i){
if(s[i]-'0'==num) ++nowlen;
else{
if(nowlen>1) sum0+=(num==0)*nowlen, sum1+=(num==1)*nowlen;
num^=1; nowlen=1;
}
}
if(nowlen>1) sum0+=(num==0)*nowlen, sum1+=(num==1)*nowlen;
if(sum1<sum0){
for(int i=1;i<=n;++i) s[i]^=1;
reverse(s+1,s+1+n);
}
sum0=0, sum1=0;
nowlen=0, num=s[1]-'0'; int pos=1, lim=0;
for(int i=1;i<=n;++i){
if(s[i]-'0'==num) ++nowlen;
else{
if(nowlen>1){
sum0+=(num==0)*nowlen, sum1+=(num==1)*nowlen;
if(sum1-sum0<lim) lim=sum1-sum0, pos=i;
}
num^=1; nowlen=1;
}
}
if(nowlen>1) sum0+=(num==0)*nowlen, sum1+=(num==1)*nowlen;
Rotate(pos); tot=0;
nowlen=0, num=s[1]-'0';
for(int i=1;i<=n;++i){
if(s[i]-'0'==num) ++nowlen;
else{
if(nowlen>1) Q[++tot]=range{i-nowlen,i-1,num,0};
num^=1; nowlen=1;
}
}
int maxn=0; topf=0;
if(nowlen>1) Q[++tot]=range{n+1-nowlen,n,num,0};
for(int i=1;i<=tot;++i){
if(Q[i].num==1) stk[++topf]=Q[i], stk[topf].l++;
else{
Q[i].r--;
while(topf && Q[i].l<=Q[i].r){
if(Q[i].nowtim<stk[topf].nowtim) Q[i].r-=stk[topf].nowtim-Q[i].nowtim, Q[i].l-=stk[topf].nowtim-Q[i].nowtim, Q[i].nowtim=stk[topf].nowtim;
else stk[topf].r+=Q[i].nowtim-stk[topf].nowtim, stk[topf].l+=Q[i].nowtim-stk[topf].nowtim, stk[topf].nowtim=Q[i].nowtim;
int tim=(Q[i].l-stk[topf].r)/2;
stk[topf].nowtim+=tim; Q[i].nowtim+=tim; int len=min(stk[topf].r-stk[topf].l+1,Q[i].r-Q[i].l+1);
if(Q[i].r-Q[i].l+1>=stk[topf].r-stk[topf].l+1){
Q[i].r-=len;
Q[i].nowtim+=len; maxn=max(maxn,Q[i].nowtim);
--topf;
}
else{
stk[topf].l+=len;
stk[topf].nowtim+=len; maxn=max(maxn,stk[topf].nowtim);
break;
}
}
}
}
int ans=maxn;
for(int i=1;i<=topf;++i) stk[i].l+=maxn-stk[i].nowtim, stk[i].r+=maxn-stk[i].nowtim;
for(int i=1;i<=n;++i) mark[i]=0; pos=1; mark[0]=1;
for(int i=1;i<=topf;++i){
for(int j=stk[i].l;j<=stk[i].r;++j) mark[j%n==0?n:j%n]=1, pos=j%n==0?n:j%n;
}
int las=pos;
for(int i=Nxt(pos);i!=pos;i=Nxt(i)){
if(mark[i]) continue;
mark[i]=mark[las]^1; las=i;
}
for(int i=1;i<=n;++i) s[i]='0'+(int(mark[i]));
pos=1;
for(int i=1;i<=n;++i)
if(s[i]!=s[1]) {pos=i; break;}
Rotate(pos); int now=0;
for(int i=1;i<=n;++i) s[i+n]=s[i];
for(int i=2;i<=n<<1;++i){
while(now && s[now+1]!=s[i]) now=nxt[now];
if(s[now+1]==s[i]) ++now;
nxt[i]=now;
}
eprint(ans+((n<<1)-nxt[n<<1]));
}
signed main(){
int t=read();
while(t--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3780kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 111ms
memory: 3660kb
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 23 21 21 22 23 19 19 19 19 1...
result:
wrong answer 92nd numbers differ - expected: '22', found: '23'