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. int consistency4(int n, int k){
  131.  
  132. int res = 0;
  133.  
  134. vector<int> val(n, 0);
  135. unordered_map<int,int> mp;
  136.  
  137.  
  138. for(int i=0; i<n; i++){
  139.  
  140. if(i-k>=0){
  141. int lastVal = val[i-k];
  142. mp[lastVal]--;
  143. if(mp[lastVal] == 0) mp.erase(lastVal);
  144. }
  145.  
  146. int count = mp[0];
  147.  
  148. int currVal = a[i];
  149. if(count & 1) currVal ^= 1;
  150.  
  151. val[i] = currVal;
  152. mp[currVal]++;
  153.  
  154. if(currVal == 0){
  155. if(i+k-1 >= n) return -1;
  156. res++;
  157. }
  158. }
  159. return res;
  160. }
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188. int practice(int n, int k){
  189.  
  190.  
  191. return 0;
  192. }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. void solve() {
  199.  
  200. int n, k;
  201. cin>> n >> k;
  202.  
  203. a.resize(n);
  204. for(int i=0; i<n; i++) cin >> a[i];
  205.  
  206. cout << consistency1(n, k) << " " << consistency4(n,k) << endl;
  207.  
  208.  
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. int32_t main() {
  216. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  217.  
  218. int t = 1;
  219. cin >> t;
  220. while (t--) {
  221. solve();
  222. }
  223.  
  224. return 0;
  225. }
Success #stdin #stdout 0s 5316KB
stdin
3
3 2
1 1 0
8 3
0 0 0 1 0 1 1 0
3 2
0 1 1
stdout
-1 -1
3 3
-1 -1