QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#302247 | #6299. Binary String | PhantomThreshold | WA | 183ms | 3624kb | C++20 | 1.9kb | 2024-01-10 18:05:00 | 2024-01-10 18:05:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &a){
int ans=0;
int n=a.size();
/*
cerr << "n : " << n << endl;
for (auto e:a) cerr << e << " ";
cerr << endl;
*/
vector<pair<int,int>> v;
for (int l=0,r=0;l<n;l=r+1){
for (r=l;r<n && a[r]==a[l];) r++;
r--;
if (l==r) continue;
if (a[l]==1){
v.emplace_back(l,r);
}
else{
int tmpl=l,tmpr=r;
for (;!v.empty() && tmpl<tmpr;){
auto& P=v.back();
if (P.second-P.first<=tmpr-tmpl){
ans=max(ans,(tmpl-P.second-1)/2+(P.second-P.first));
tmpl+=P.second-P.first;
v.pop_back();
}
else{
ans=max(ans,(tmpl-P.second-1)/2+(tmpr-tmpl));
P.second-=tmpr-tmpl;
tmpl=tmpr;
}
}
}
}
/*
for (auto [l,r]:v){
cerr << "l,r : " << l << " " << r << endl;
}
*/
int prel=-1,prer=-1;
for (auto [l,r]:v){
for (int i=l;i<=r;i++) a[i]=1;
for (int i=l-1;i>=prer+1;i--) a[i]=(l-1-i)%2;
prel=l;
prer=r;
}
vector<int> nxt(n);
nxt[0]=-1;
int k=-1;
for (int i=1;i<n;i++){
while (k>-1 && a[k+1]!=a[i]){
k=nxt[k];
}
if (a[k+1]==a[i]) k++;
nxt[i]=k;
}
int L=n-(nxt[n-1]+1);
if (n%L==0) ans+=L;
else ans+=n;
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
int Tcase=1;
cin >> Tcase;
for (;Tcase--;){
string str;
cin >> str;
int n=str.length();
vector<int> a(n);
for (int i=0;i<n;i++) a[i]=str[i]-'0';
int cnt=0;
for (auto x:a){
if (x==1) cnt++;
else cnt--;
}
if (cnt<0){
reverse(a.begin(),a.end());
for (auto &x:a) x=1-x;
}
int pos=-1,mn=n+1,sum=0;
for (int i=0;i<n;i++){
if (a[i]==1) sum++;
else sum--;
if (sum<mn){
mn=sum;
pos=i;
}
}
rotate(a.begin(),a.begin()+pos+1,a.end());
cout << solve(a) << "\n";
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3548kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 183ms
memory: 3624kb
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 512th numbers differ - expected: '10', found: '26'