/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Wrong Answer 20ms 572.0 KiB
#3 Wrong Answer 6ms 448.0 KiB

Code

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

int maxConsecutiveOnes(string &s) {
    int max_count = 0, count = 0;
    for (char c : s) {
        if (c == '1') {
            count++;
            max_count = max(max_count, count);
        } else {
            count = 0;
        }
    }
    return max_count;
}

int calculateMaxConsecutiveOnes(int n, string s) {
    vector<int> segments;
    int current_segment = 0;
    for (char c : s) {
        if (c == '1') {
            current_segment++;
        } else {
            if (current_segment > 0) {
                segments.push_back(current_segment);
            }
            current_segment = 0;
        }
    }
    if (current_segment > 0) {
        segments.push_back(current_segment);
    }

    sort(segments.rbegin(), segments.rend());
    int max_consecutive_ones = 0;

    for (int i = 0; i < segments.size(); i += 2) {
        max_consecutive_ones += segments[i];
    }
    return max_consecutive_ones;
}

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

    while (t--) {
        int n;
        cin >> n;
        string s;
        cin >> s;
        cout << calculateMaxConsecutiveOnes(n, s) << endl;
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1113 Fliping Game
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-06 14:57:28
Judged At
2024-11-11 02:24:53
Judged By
Score
5
Total Time
20ms
Peak Memory
572.0 KiB