fork download
  1. //UVA 465.cpp
  2. #include<bits/stdc++.h>
  3. #define readf freopen("in" , "r" , stdin);
  4. #define writef freopen("out" , "w" , stdout);
  5. #define gcu() getchar_unlocked()
  6. typedef long long ll ;
  7. typedef unsigned long long ull ;
  8. const ll INFLL = 4e18;
  9. const int INFINT = 2e9;
  10. using namespace std;
  11. int dx[]={-1, 0, 1, 0,-1, 1, 1,-1};
  12. int dy[]={ 0, 1, 0,-1, 1, 1,-1,-1};
  13. int SIZE = 1e5;
  14. /*--------------------------------------------------*/
  15. int n, k;
  16. vector<int>a, cumSum;
  17. /*-----------------MAIN FUNCTION--------------------*/
  18. int main()
  19. {
  20. ios_base::sync_with_stdio(false);
  21. cin.tie(NULL);
  22. #ifndef ONLINE_JUDGE
  23. readf
  24. //writef//
  25. #endif
  26. int num;
  27. cin >> n >> k;
  28. cumSum.push_back(0);
  29. for(int i = 0 ; i < n ; i++)
  30. {
  31. cin >> num;
  32. a.push_back(num);
  33. cumSum.push_back(cumSum[i] + a[i]);
  34. }
  35. int f = 0 , l = 0, e = 0, s = 0;
  36. while(e < cumSum.size())
  37. {
  38. int cmp = e - s - (cumSum[e] - cumSum[s]);
  39. while(cmp <= k)
  40. {
  41. if(e - s > l - f && cmp <= k)f = s, l = e;
  42. e++;
  43. cmp = e - s - (cumSum[e] - cumSum[s]);;
  44. }
  45. if(cmp < k)e++;
  46. else s++;
  47. }
  48. cout << l-- - f << endl;
  49. while(f<=l)a[f++] = 1;
  50. int i = 0, len = a.size();
  51. while(i < len)cout << a[i++] << " ";
  52. //------------------------------------------------------------------------
  53. }
  54.  
Success #stdin #stdout 0s 4384KB
stdin
10 2
1 0 0 1 0 1 0 1 0 1
stdout
5
1 0 0 1 1 1 1 1 0 1