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.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. //* TC = O(n)
  33. //* SC = O(n)
  34.  
  35. int consistency1(int n, int k){
  36.  
  37. int res = 0;
  38.  
  39. vector<int> flipCt(n+1, 0);
  40.  
  41. int countSoFar = 0;
  42.  
  43. for(int i=0; i<n; i++){
  44.  
  45. int count = countSoFar;
  46. if(i-k>=0) count = countSoFar - flipCt[i-k];
  47.  
  48. int val = a[i];
  49. if(count & 1) val ^= 1;
  50.  
  51. flipCt[i] = countSoFar;
  52.  
  53. if(val == 0){
  54. if(i+k-1 >= n) return -1;
  55. flipCt[i]++;
  56. countSoFar++;
  57. res++;
  58. }
  59. }
  60. return res;
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. //* TC = O(n)
  69. //* SC = O(k)
  70.  
  71. int consistency2(int n, int k){
  72.  
  73. int res = 0;
  74.  
  75. queue<int> q;
  76.  
  77. for(int i=0; i<n; i++){
  78.  
  79. while(!q.empty() && q.front() <= i-k) q.pop();
  80. int count = q.size();
  81.  
  82. int val = a[i];
  83. if(count & 1) val ^= 1;
  84.  
  85.  
  86. if(val == 0){
  87. if(i+k-1 >= n) return -1;
  88. q.push(i);
  89. res++;
  90. }
  91. }
  92. return res;
  93. }
  94.  
  95.  
  96.  
  97. //* TC = O(n)
  98. //* SC = O(1) Inplace (use a[] as prefix count)
  99.  
  100. int consistency3(int n, int k){
  101.  
  102. int res = 0;
  103.  
  104. int countSoFar = 0;
  105.  
  106. for(int i=0; i<n; i++){
  107.  
  108. int count = countSoFar;
  109. if(i-k>=0) count = countSoFar - a[i-k];
  110.  
  111. int val = a[i];
  112. if(count & 1) val ^= 1;
  113.  
  114. a[i] = countSoFar;
  115.  
  116. if(val == 0){
  117. if(i+k-1 >= n) return -1;
  118. a[i]++;
  119. countSoFar++;
  120. res++;
  121. }
  122. }
  123. return res;
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. int practice(int n, int k){
  146.  
  147.  
  148. return 0;
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155. void solve() {
  156.  
  157. int n, k;
  158. cin>> n >> k;
  159.  
  160. a.resize(n);
  161. for(int i=0; i<n; i++) cin >> a[i];
  162.  
  163. cout << consistency2(n, k) << endl;
  164.  
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. int32_t main() {
  173. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  174.  
  175. int t = 1;
  176. cin >> t;
  177. while (t--) {
  178. solve();
  179. }
  180.  
  181. return 0;
  182. }
Success #stdin #stdout 0s 5324KB
stdin
2
3 2
1 1 0
8 3
0 0 0 1 0 1 1 0
stdout
-1
3