QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#360947 | #6299. Binary String | mazihang2022 | TL | 0ms | 13672kb | C++14 | 2.8kb | 2024-03-22 16:23:24 | 2024-03-22 16:23:25 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pii pair<int,int>
#define isz(v) (int)(v.size())
using namespace std;
const int maxn=1000005;
const int inf=0x3f3f3f3f;
namespace Solve {
int n;
int a[maxn];
int c[maxn];
int res[maxn];
int fail[maxn];
void clear() {
fill(c+1,c+n+1,0);
fill(res+1,res+n+1,0);
fill(fail+1,fail+n+1,0);
}
void deal(int p) {
int t[maxn]={},cnt=0;
for(int i=p+1;i<=n;i++) {
t[++cnt]=a[i];
}
for(int i=1;i<=p;i++) {
t[++cnt]=a[i];
}
for(int i=1;i<=n;i++) {
a[i]=t[i];
}
}
void main(int tid) {
string s;
cin>>s;
n=isz(s);
for(int i=1;i<=n;i++) {
a[i]=s[i-1]-'0';
}
int cc=accumulate(a+1,a+n+1,0);
if(!cc||cc==n) {
cout<<"1\n";
return ;
}
if(cc>=n-cc) {
for(int i=1;i<=n;i++) {
a[i]^=1;
}
reverse(a+1,a+n+1);
}
int sum=0,mi=inf,pos=0;
for(int i=n;i>=1;i--) {
sum+=!a[i]?1:-1;
if(sum<mi) {
mi=sum;
pos=i;
}
}
if(pos) {
deal(pos-1);
}
for(int i=1;i<=n;i++) {
cerr<<a[i]<<" ";
}
cerr<<"\n";
sum=0,pos=0;
for(int i=n;i>=1;i--) {
sum+=!a[i]?1:-1;
if(!sum) {
pos=i;
}
}
if(pos) {
deal(pos-1);
}
for(int i=1;i<=n;i++) {
cerr<<a[i]<<" ";
}
cerr<<"\n";
int cnt=0,p=0;
++cnt;
while(a[c[cnt]+1]) {
c[cnt]++;
}
for(int i=1;i<=n;i++) {
if(!a[i]) {
if(p) {
++cnt;
for(int j=p+1;j<=i;j++) {
c[cnt]+=a[j];
}
}
p=i;
}
}
// for(int i=1;i<=cnt;i++) {
// cerr<<c[i]<<" ";
// }
// cerr<<"\n";
int ans=0;
while(*max_element(c+1,c+cnt+1)>1) {
for(int i=1;i<=cnt;i++) {
if(c[i]) {
int j=i;
while(j<=cnt&&c[j]) {
j++;
}
j--;
c[j]--;
c[i-1]++;
i=j;
}
}
// cerr<<"c: ";
// for(int i=1;i<=cnt;i++) {
// cerr<<c[i]<<" ";
// }
// cerr<<"\n";
ans++;
}
// cerr<<ans<<"\n";
int cur=0;
for(int i=1;i<=cnt;i++) {
res[++cur]=0;
while(c[i]--) {
res[++cur]=1;
}
}
// for(int i=1;i<=n;i++) {
// cerr<<res[i]<<" ";
// }
// cerr<<"\n";
int j=0;
for(int i=2;i<=n;i++) {
while(j&&res[i]!=res[j+1]) {
j=fail[j];
}
if(res[i]==res[j+1]) {
j++;
}
fail[i]=j;
}
if(n%(n-fail[n])==0) {
ans+=n-fail[n];
} else {
ans+=n;
}
cout<<ans<<"\n";
}
void init() {
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T=1;
cin>>T;
Solve::init();
for(int t=1;t<=T;t++) {
Solve::main(t);
Solve::clear();
}
#ifndef ONLINE_JUDGE
cerr<<"Time: "<<(1.0*clock()/CLOCKS_PER_SEC)*1000<<"ms\n";
#endif
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13672kb
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...