/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 332.0 KiB
#2 Wrong Answer 6ms 772.0 KiB
#3 Wrong Answer 8ms 540.0 KiB

Code

/* 
 *   @author:- MAHMUDUL HASAN SAKIB
 *   DATE & TIME :- 2025-02-14 00:32:20
 *   BANGLADESH , SYLHET.
 */ 
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define fi first
#define se second
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define PI acos(-1.0)
#define pb push_back
#define mp make_pair
#define vi vector<ll>
#define maxn 500005
#define mod 1000000007
#define inf 1000000007
#define pii pair<ll,ll>
#define vii vector<pii>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define lcm(a,b) ((a*b)/__gcd(a,b));
#define srt(v) sort(v.begin(),v.end())
#define rsrt(v) sort(v.rbegin(),v.rend())
#define setbits(x) __builtin_popcountll(x)
#define rep(i, a, b) for(ll i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define rev_str(str) reverse(str.begin(),str.end());
#define print(v) for(auto e:v) cout<<e<<" "; cout<<endl;
#define sum(a) (accumulate((a).begin(), (a).end(), 0LL))
#define printp(v) for(auto e:v) cout<<e.first<<" "<<e.second<<endl;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
bool sortByValue(const pair<int, int>& a, const pair<int, int>& b) {return a.second > b.second;}
const int N = 1e5+10;
const int M = 1e9+7;
bool is_Prime(ll n) {
    if (n < 2) return false; 
    for (ll i = 2; i * i <= n; ++i) {
        if (n % i == 0) return false; 
    }
    return true; 
}
long long Pow(ll a,ll b){
    ll ans = 1;
    while(b>0){
        if(b&1) ans*=a;
        b>>=1;
        a*=a;
    }
    return ans;
}
long long modPow(ll a,ll b){
    ll ans = 1;
    while(b>0){
        if(b&1) ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
// lies between divisor of a and b == GCD
// prime factorization while the taking the maximum power ==LCM...
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

void solve(){
    ll N,K;cin>>N>>K;
    string S;cin>>S;
    vi one,gap;
    ll count = 0;
    bool ok= false;
    for (int i = 0; i < N; i++) {
        if (S[i] == '1') {
            if (!ok) {
                one.push_back(0);
                ok = true;
            }
            one.back()++;
        }
        else {
            if (ok) {
                gap.push_back(0);
                ok = false;
            }
            if (!gap.empty()) gap.back()++;
        }
    }
    
    ll mx_Ones = 0, curr_Ones = 0, usedK = 0;
    ll left = 0;
    for (int right = 0; right < one.size(); right++) {
        curr_Ones += one[right];
        
        while (left < right && usedK + gap[left] > K) {
            curr_Ones -= one[left];
            usedK -= gap[left];
            left++;
        }
        
        if (right > 0) usedK += gap[right - 1];
        mx_Ones = max(mx_Ones, curr_Ones + min(K - usedK, N - curr_Ones));
    }
    
    cout<< mx_Ones << endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll t=1;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 15:58:44
Judged At
2025-02-17 15:58:44
Judged By
Score
0
Total Time
8ms
Peak Memory
772.0 KiB