AhmedAshraff

Untitled

Jun 27th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  3. #define ll long long
  4. #define sz(s) (int)(s).size()
  5. #define all(s) (s).begin(),(s).end()
  6. using namespace std;
  7. void File();
  8. void sol();
  9. int main() {
  10.     boAshraf
  11. //    File();
  12.     freopen("strips.in", "r", stdin);
  13.     int t = 1;
  14.     cin >> t;
  15.     while (t--) {
  16.         sol();
  17.     }
  18.     return 0;
  19. }
  20.  
  21. void sol() {
  22.     int n,k;
  23.     cin>>n>>k;
  24.     vector<pair<int,int>>v;
  25.     string s;cin>>s;
  26.     int cnt=0,bad=0;
  27.     char last=s[0];
  28.     for (int i = 0; i < n; ++i) {
  29.         if(i && s[i]!=s[i-1])bad++;
  30.         if(s[i]==last)cnt++;
  31.         else{
  32.             if(last=='0'){
  33.                 v.emplace_back(cnt,2);
  34.             }
  35.             cnt=1;
  36.             last=s[i];
  37.         }
  38.     }
  39.     if(last=='0')v.emplace_back(cnt,1);
  40.     if(s[0]=='0')v[0].second=1;
  41.     if(v.empty() || v[0].first==n)return void(cout<<0<<'\n');
  42.     int ans=bad;
  43.     vector<vector<int>>op(2);
  44.     for(auto [a,b]:v){
  45.         op[b-1].emplace_back(a);
  46.     }
  47.     sort(all(op[0]));
  48.     sort(all(op[1]));
  49.  
  50.     for(int j=0;j<2;j++)
  51.     for(int i=1;i<sz(op[j]);i++)op[j][i]+=op[j][i-1];
  52.  
  53.     for(int i=0;i<=sz(op[0]);i++){
  54.         int sum=(i?op[0][i-1]:0);
  55.         int rem=k-sum;
  56.         if(rem>=0){
  57.             int cnt1=i;
  58.             int cnt2= upper_bound(all(op[1]),rem)-op[1].begin();
  59.             ans=min(ans,bad-cnt1-cnt2*2);
  60.         }
  61.         else break;
  62.     }
  63.  
  64.     cout<<ans<<'\n';
  65. }
  66.  
  67. void File() {
  68. #ifndef ONLINE_JUDGE
  69.     freopen("input.txt", "r", stdin);
  70.     freopen("output.txt", "w", stdout);
  71. #endif
  72. }
Advertisement
Add Comment
Please, Sign In to add comment