fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24. //_ ***************************** START Below *******************************
  25.  
  26.  
  27.  
  28. vector<int> a;
  29.  
  30. //* Template 1 :
  31.  
  32. int consistency1(int n){
  33.  
  34. int k = 0;
  35. for(int i=0; i<n; i++) if(a[i] == 1) k++;
  36. if(k == 0) return -1;
  37.  
  38. int s = 0, e = 0;
  39. int zeroes = 0;
  40. int ans = INF;
  41.  
  42. while(e<n){
  43. if(a[e] == 0) zeroes++;
  44.  
  45. if(e-s+1 < k){
  46. e++;
  47. }
  48. else{
  49. ans = min(ans, zeroes);
  50. if(a[s] == 0) zeroes--;
  51. s++;
  52. e++;
  53. }
  54. }
  55.  
  56. return ans;
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //* Template 2 :
  64.  
  65. int consistency2(int n){
  66.  
  67. int k = 0;
  68. for(int i=0; i<n; i++) if(a[i] == 1) k++;
  69. if(k == 0) return -1;
  70.  
  71. int s = 0, e = 0;
  72. int zeroes = 0;
  73. int ans = INF;
  74.  
  75.  
  76. for(int i=0; i<n; i++){
  77. if(a[i] == 0) zeroes++;
  78.  
  79. if(i-k>=0){
  80. if(a[i-k] == 0) zeroes--;
  81. }
  82.  
  83.  
  84. if(i-k+1>=0){
  85. ans = min(ans, zeroes);
  86. }
  87. }
  88.  
  89. return ans;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. int practice(int n){
  108.  
  109. return 0;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. void solve() {
  116.  
  117. int n;
  118. cin >> n;
  119. a.resize(n);
  120. for(int i=0; i<n; i++) cin >> a[i];
  121.  
  122. cout << consistency1(n) << " " << consistency2(n) << endl;
  123.  
  124. // cout << consistency1(n) << " " << practice(n) << endl;
  125.  
  126.  
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. int32_t main() {
  134. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  135.  
  136. int t = 1;
  137. while (t--) {
  138. solve();
  139. }
  140.  
  141. return 0;
  142. }
Success #stdin #stdout 0.01s 5288KB
stdin
6
1 0 1 0 1 1
stdout
1 1