CF660C Hard Process 题解
Solution
一眼 \(\mathcal{O}(n)\) 吊打 \(\mathcal{O}(n \log n)\)。
求个前缀和,然后双指针去移动,求最大值。
int qzh[N][2], a[N];
int maxx = 0, ansl, ansr;
int f = 0;
int cnt = 0;
signed main()
{
int n = read(), k = read();
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i <= n; i++) {
if(a[i] == 1) cnt++, maxx = max(cnt, maxx);
else maxx = max(cnt, maxx), cnt = 0;
}
for(int i = 1; i <= n; i++) {
if(a[i]) qzh[i][1] = qzh[i - 1][1] + 1, qzh[i][0] = qzh[i - 1][0];
else qzh[i][0] = qzh[i - 1][0] + 1, qzh[i][1] = qzh[i - 1][1];
}
int l = 1, r = 1;
while(l <= r && r <= n) {
if(qzh[r][0] - qzh[l - 1][0] <= k) {
if(maxx < r - l + 1) {
maxx = r - l + 1;
ansl = l, ansr = r;
}
r++;
}
else l++;
}
if(k) for(int i = ansl; i <= ansr; i++) a[i] = 1;
printf("%d\n", maxx);
for(int i = 1; i <= n; i++) printf("%d ", a[i]);
return 0;
}