kdzhr

K-периодичная гирлянда

Jun 1st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. // OK E_div3_642 https://codeforces.com/contest/1353/problem/E
  2.  
  3. #include <iostream>
  4.  
  5. int main() {
  6.     std::ios_base::sync_with_stdio(false);
  7.     size_t t;
  8.     std::cin >> t;
  9.     while (t-- > 0) {
  10.         size_t n, k;
  11.         std::cin >> n >> k;
  12.         std::string s;
  13.         std::cin >> s;
  14.         size_t all = 0;
  15.         for (auto ch : s) {
  16.             all += (ch == '1');
  17.         }
  18.         size_t res = all;
  19.         for (size_t i = 0; i < k; i++) {
  20.             size_t cur = all;
  21.             int32_t start = i;
  22.             while (start + k < n) {
  23.                 start += k;
  24.             }
  25.             for (int32_t j = start; j >= 0; j -= k) {
  26.                 cur = std::min(cur - (s[j] == '1') + (s[j] == '0'), all + (s[j] == '0') - (s[j] == '1'));
  27.                 res = std::min(cur, res);
  28.             }
  29.         }
  30.         std::cout << res << '\n';
  31.     }
  32.     return 0;
  33. }
Add Comment
Please, Sign In to add comment