/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 540.0 KiB
#2 Wrong Answer 5ms 564.0 KiB

Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

// Function to find the maximum number of consecutive 1s that can be achieved
int maximizeConsecutiveOnes(const string &S, int K) {
    int n = S.size();
    int maxOnes = 0;
    int zeroCount = 0;
    int left = 0;

    // Sliding window approach
    for (int right = 0; right < n; ++right) {
        // Count zeros in the current window
        if (S[right] == '0') {
            ++zeroCount;
        }

        // If zeroCount exceeds K, move the left pointer to maintain the constraint
        while (zeroCount > K) {
            if (S[left] == '0') {
                --zeroCount;
            }
            ++left;
        }

        // Update the maximum number of consecutive 1s
        maxOnes = max(maxOnes, right - left + 1);
    }

    return maxOnes;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int N, K;
        cin >> N >> K;
        string S;
        cin >> S;

        cout << maximizeConsecutiveOnes(S, K) << "\n";
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1159 Binary String
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 17:20:07
Judged At
2025-02-17 17:20:07
Judged By
Score
0
Total Time
5ms
Peak Memory
564.0 KiB