/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 21ms 572.0 KiB
#3 Wrong Answer 5ms 532.0 KiB

Code

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

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

    int T;
    cin >> T;
    while (T--) {
        int N;
        string S;
        cin >> N >> S;
        
        vector<int> segments;
        int count = 0;
        
        // Collect all segments of consecutive '1's
        for (int i = 0; i < N; ++i) {
            if (S[i] == '1') {
                ++count;
            } else {
                if (count > 0) {
                    segments.push_back(count);
                }
                count = 0;
            }
        }
        // Add the last segment if it ends with '1'
        if (count > 0) {
            segments.push_back(count);
        }

        // Sort segments in descending order to simulate optimal play
        sort(segments.rbegin(), segments.rend());

        // Roy's optimal score: sum the lengths of every other segment (1st, 3rd, etc.)
        int max_consecutive_1s = 0;
        for (int i = 0; i < segments.size(); i += 2) {
            max_consecutive_1s += segments[i];
        }

        cout << max_consecutive_1s << endl;
    }
    return 0;
}


Information

Submit By
Type
Submission
Problem
P1113 Fliping Game
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 15:22:33
Judged At
2024-11-05 15:22:33
Judged By
Score
5
Total Time
21ms
Peak Memory
572.0 KiB