/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int solve(string& s, int n) {
    int onesBeforeZero = 0; // Misplaced '1's for non-decreasing order
    int zeroesBeforeOne = 0; // Misplaced '0's for non-increasing order

    int onesCount = 0;
    int zeroCount = 0;

    // Calculate misplaced '1's (for non-decreasing order)
    // Count how many '1's are before a '0' for non-decreasing order
    for (int i = 0; i < n; ++i) {
        if (s[i] == '1') {
            onesCount++;
        } else {
            // Each '0' after a '1' means we need to swap
            onesBeforeZero += onesCount;
        }
    }

    // Calculate misplaced '0's (for non-increasing order)
    // Count how many '0's are before a '1' for non-increasing order
    for (int i = 0; i < n; ++i) {
        if (s[i] == '0') {
            zeroCount++;
        } else {
            // Each '1' after a '0' means we need to swap
            zeroesBeforeOne += zeroCount;
        }
    }

    // Return the minimum of the two cases
    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:35:52
Judged At
2025-02-16 14:35:52
Judged By
Score
20
Total Time
14ms
Peak Memory
536.0 KiB