QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411216 | #6299. Binary String | liuhengxi | WA | 0ms | 3636kb | C++14 | 1.5kb | 2024-05-15 10:02:34 | 2024-05-15 10:02:35 |
Judging History
answer
// created: May/15/2024 09:42:30
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define F(i,l,r) for(int i=(l),i##_end=(r);i<i##_end;++i)
#define I128 //||is_same<T,__int128_t>::value||is_same<T,__uint128_t>::value
using namespace std;
template<typename T>enable_if_t<is_integral<T>::value I128,void> readmain(T &x)
{
bool neg=false;int c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')neg=true;
for(x=0;isdigit(c);c=getchar())x=(T)(10*x+(c-'0'));
if(neg)x=-x;
}
template<typename T>T& read(T &x){readmain(x);return x;}
template<typename T,typename ...Tr>void read(T &x,Tr&... r){readmain(x);read(r...);}
constexpr int N=1e6+5;
int n,a[N],p;
char s[N];
int flip()
{
int one=0;
F(i,0,n)one+=s[i];
one-='0'*n;
if(2*one<n)
{
F(i,0,n)s[i]^=1;
reverse(s,s+n);
one=n-one;
}
return one;
}
void rotate()
{
static char t[N];
int h=0,mh=0,mp=0;
F(i,0,n)
{
h+=2*s[i]-(2*'0'+1);
if(h<mh)mh=h,mp=i+1;
}
if(!mp&&s[n-1]=='1')
{
mp=n-1;
while(mp&&s[mp-1]=='1')--mp;
}
memcpy(t,s+mp,n-mp);
memcpy(t+n-mp,s,mp);
memcpy(s,t,n);
}
int solve()
{
int one=flip();
if(one==n)return 1;
if(one<n)rotate();
int ans=0;
p=0;
for(int l=0,r=0;l<n;l=r+1)
{
while(s[l]==s[r+1])++r;
if(s[l]=='0')F(i,l,r)ans=max(ans,i-a[--p]);
else F(i,l,r)a[p++]=i;
}
ans>>=1;
ans+=2*one==n?2:n;
return ans;
}
int main()
{
int tt;
read(tt);
while(tt--)
{
scanf("%s",s);
n=(int)strlen(s);
printf("%d\n",solve());
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3636kb
input:
3 1 001001 0001111
output:
1 6 9
result:
wrong answer 2nd numbers differ - expected: '3', found: '6'