Ankit_132

C

Oct 11th, 2023
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define pb     push_back
  8. #define ppb    pop_back
  9.  
  10.  
  11. int main()
  12. {
  13.     _test
  14.     {
  15.         int n, k;
  16.         string s;
  17.         cin>>n>>k>>s;
  18.  
  19.         vector<int> cnt0(k);
  20.         vector<int> cnt1(k);
  21.  
  22.         for(int i=1; i<=n; i++)
  23.         {
  24.             if(s[i-1] == '0')       cnt0[i%k]++;
  25.             if(s[i-1] == '1')       cnt1[i%k]++;
  26.         }
  27.  
  28.         ll int ans1 = 0;
  29.         ll int ans0 = 0;
  30.  
  31.         for(int i=0; i<k; i++)
  32.         {
  33.             ans0 += (cnt1[i])/2 + 2*(cnt1[i]%2);
  34.             ans1 += cnt0[i];
  35.  
  36.             if(cnt1[i]==0 && cnt0[i]!=0)     ans1 = 1e12;
  37.  
  38.             assert((cnt0[i] + cnt1[i]) >= 2);
  39.         }
  40.  
  41.         cout<<min(ans0, ans1)<<"\n";
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment