Tuesday, October 19, 2021

CF Educational Round 11. C. Hard Process

1.Problem

Problem - C - Codeforces

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;  
 }