1.Problem
2.Idea
Standard two pointers method
3.Source
void solve()
{
int n, k;
cin >> n >> k;
vector<int> a(n);
REP(i, n) cin >> a[i];
pi res(0, 0);
int right = 0;
int cnt = 0;
for (int left = 0; left < n; ++left) {
while (right < n && cnt + (!a[right]) <= k) {
cnt += (!a[right]);
right++;
}
if (res.first < right - left) {
res.first = right - left;
res.second = left;
}
if (left == right) right++;
else cnt -= (!a[left]);
}
cout << res.first << endl;
REP(i, n) {
if (i >= res.second && i < res.second + res.first) cout << 1 << " ";
else cout << a[i] << " ";
}
cout << endl;
}
No comments:
Post a Comment