题解:P11084 [ROI 2019 Day 2] 灯串
xiezheyuan · · 题解
简要题意
对于一个
给定一个长度为
思路
不妨先考虑
考虑优化,首先一个重要的结论是虽然遍历每种长度的字符串无法做到的,但是遍历每种长度的字符串分割成了多少个字符串是可以做到的(调和级数求和,
考虑对于每一个长度,维护一个指针,先维护前缀
时间复杂度上界
代码
#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int N = 5e5 + 5;
int n, a[N], nxt[N][2];
string s;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> s;
s = " " + s;
for(int i=1;i<=n;i++) a[i] = a[i - 1] + (s[i] == '0');
nxt[n][0] = nxt[n][1] = n + 1;
for(int i=n;i>=1;i--){
nxt[i - 1][0] = nxt[i][0], nxt[i - 1][1] = nxt[i][1];
nxt[i - 1][s[i] - '0'] = i;
}
int ans = 0, ansx = 0, anstt = 0;
for(int i=1;i<=n;i++){
int tot = 0, cur = 0;
while(cur <= n){
int pos = lower_bound(a + cur + 1, a + n + 1, a[cur] + i) - a;
if(pos == n + 1) break;
tot++; cur = nxt[pos][1];
}
if(tot <= 1) continue;
int res = (tot - 1) * (i + 1) + i;
if(res > ans) ans = res, ansx = i, anstt = tot;
}
if(ans < count(s.begin(), s.end(), '1')){
cout << (ans = count(s.begin(), s.end(), '1')) << '\n';
for(int i=1;i<=ans;i++) cout << 1;
cout << '\n';
return 0;
}
cout << ans << '\n';
for(int i=1;i<=anstt;i++){
for(int j=1;j<=ansx;j++) cout << 0;
if(i != anstt) cout << 1;
}
cout << '\n';
return 0;
}
// Written by xiezheyuan