/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

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

int solve(string& s, int n) {
    // Misplaced 1's in non-decreasing order (0's first, then 1's)
    int onesBeforeZero = 0;
    int onesCount = 0;
    
    // Traverse the string to count misplaced 1's for non-decreasing order
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1') {
            onesCount++;
        } else {
            onesBeforeZero += onesCount;
        }
    }

    // Misplaced 0's in non-increasing order (1's first, then 0's)
    int zeroesBeforeOne = 0;
    int zeroCount = 0;
    
    // Traverse the string to count misplaced 0's for non-increasing order
    for (int i = 0; i < n; ++i) {
        if (s[i] == '0') {
            zeroCount++;
        } else {
            zeroesBeforeOne += zeroCount;
        }
    }

    // Return the minimum of the two counts
    return min(onesBeforeZero, zeroesBeforeOne);
}

int main() {
    int T;
    cin >> T;

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

        cout << solve(S, N) << endl;
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1016 Swap sort
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-16 14:42:23
Judged At
2025-02-16 14:42:23
Judged By
Score
20
Total Time
5ms
Peak Memory
532.0 KiB