/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 28ms 560.0 KiB
#3 Wrong Answer 29ms 556.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

int maxConsecutiveOnes(string S, int K) {
    int N = S.length();
    vector<int> groups; 
    int count = 0;

    for (char c : S) {
        if (c == '1') {
            count++;
        } else {
            if (count > 0) {
                groups.push_back(count);
                count = 0;
            }
        }
    }
    if (count > 0) {
        groups.push_back(count);
    }

    if (groups.empty()) {
        return 0;
    }

    int maxLen = 0;
    int left = 0;
    int right = 0;
    int zeroCount = 0;

    while (right < groups.size()) {
        if (right > 0) {
            zeroCount++;
        }

        while (zeroCount > K) {
            if (left < right) {
                zeroCount--;
                left++;
            } else {
                break;
            }
        }

        int currentLen = 0;
        for (int i = left; i <= right; i++) {
            currentLen += groups[i];
        }

        maxLen = max(maxLen, currentLen);

        right++;
    }

    return maxLen;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        int siz, k;
        string s;
        cin >> siz >> k;
        cin >> s;
        cout << maxConsecutiveOnes(s, k) << endl;
    }
    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:19:53
Judged At
2025-02-17 15:19:53
Judged By
Score
0
Total Time
29ms
Peak Memory
560.0 KiB