Dang_Quan_10_Tin

DHBB112021_BITSTR

Aug 18th, 2021 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #define task "BITSTR"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <set>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. using ll = long long;
  11. using ld = long double;
  12.  
  13. void read(int &x)
  14. {
  15.     x = 0;
  16.     register int c;
  17.     while ((c = getchar()) && (c > '9' || c < '0'))
  18.         ;
  19.     for (; c >= '0' && c <= '9'; c = getchar())
  20.         x = x * 10 + c - '0';
  21. }
  22.  
  23. void readstr(string &s)
  24. {
  25.     s.clear();
  26.     register int c;
  27.     while ((c = getchar()) && (c > '1' || c < '0'))
  28.         ;
  29.     for (; c >= '0' && c <= '1'; c = getchar())
  30.         s.push_back(c);
  31. }
  32.  
  33. constexpr int N = 2e5 + 5;
  34. constexpr int Inf = 1e9 + 7;
  35. int n, k, l[N], r[N], a[N], v[N], m(0);
  36. string s;
  37.  
  38. #define bit(i, x) (((x) >> (i)) & 1)
  39.  
  40. void Read()
  41. {
  42.     cin >> n >> k >> s;
  43.     s = " " + s;
  44. }
  45.  
  46. void Sub_3()
  47. {
  48.     for (int i = 1, j = 1; i <= n; ++i)
  49.     {
  50.         while (j <= n && s[i] == s[j])
  51.             ++j;
  52.         a[++m] = j - i;
  53.         l[m] = r[m] = m;
  54.         i = j - 1;
  55.     }
  56.  
  57.     if (m <= k)
  58.     {
  59.         cout << 0;
  60.         return;
  61.     }
  62.  
  63.     if (k == 1)
  64.     {
  65.         cout << min(count(s.begin(), s.end(), '0'), count(s.begin(), s.end(), '1'));
  66.         return;
  67.     }
  68.  
  69.     int ans(Inf);
  70.  
  71.     for (int i = 0; i < 2; ++i)
  72.         for (int j = 0; j < 2; ++j)
  73.         {
  74.             int res(0);
  75.             if (i)
  76.                 res += a[1];
  77.             if (j)
  78.                 res += a[m];
  79.  
  80.             struct com
  81.             {
  82.                 bool operator()(const int &x, const int &y) const
  83.                 {
  84.                     return v[x] < v[y] || (v[x] == v[y] && x < y);
  85.                 }
  86.             };
  87.  
  88.             set<int, com> q;
  89.  
  90.             for (int t = 2 + i; t <= m - 1 - j; ++t)
  91.             {
  92.                 r[t] = t;
  93.                 l[t] = t;
  94.                 v[t] = a[t];
  95.                 q.insert(t);
  96.             }
  97.  
  98.             int sz(q.size() + 2);
  99.  
  100.             while (sz > k)
  101.             {
  102.                 int x = *q.begin();
  103.                 q.erase(q.begin());
  104.  
  105.                 res += v[x];
  106.                 sz -= 2;
  107.  
  108.                 if (q.find(l[x - 1]) == q.end())
  109.                 {
  110.                     auto v = q.find(r[x] + 1);
  111.                     if (v != q.end())
  112.                         q.erase(v);
  113.                 }
  114.                 else if (q.find(r[x] + 1) == q.end())
  115.                     q.erase(l[x - 1]);
  116.                 else
  117.                 {
  118.                     q.erase(l[x - 1]);
  119.                     q.erase(r[x] + 1);
  120.  
  121.                     l[r[r[x] + 1]] = l[x - 1];
  122.                     r[l[x - 1]] = r[r[x] + 1];
  123.                     v[l[x - 1]] = v[l[x - 1]] + v[r[x] + 1] - v[x];
  124.                     q.insert(l[x - 1]);
  125.                 }
  126.             }
  127.             ans = min(ans, res);
  128.         }
  129.  
  130.     cout << ans;
  131. }
  132.  
  133. int32_t main()
  134. {
  135.     ios::sync_with_stdio(0);
  136.     cin.tie(0);
  137.     cout.tie(0);
  138.     if (fopen(task ".INP", "r"))
  139.     {
  140.         freopen(task ".INP", "r", stdin);
  141.         freopen(task ".OUT", "w", stdout);
  142.     }
  143.     Read();
  144.     Sub_3();
  145. }
  146.  
Add Comment
Please, Sign In to add comment