/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 764.0 KiB
#2 Accepted 1ms 344.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 484.0 KiB
#5 Accepted 1ms 532.0 KiB
#6 Accepted 1ms 512.0 KiB
#7 Accepted 1ms 532.0 KiB
#8 Accepted 1ms 440.0 KiB
#9 Accepted 1ms 532.0 KiB
#10 Wrong Answer 1ms 320.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 1ms 520.0 KiB
#13 Accepted 1ms 324.0 KiB
#14 Accepted 1ms 532.0 KiB
#15 Accepted 1ms 532.0 KiB
#16 Accepted 1ms 532.0 KiB
#17 Accepted 1ms 444.0 KiB
#18 Accepted 1ms 532.0 KiB
#19 Accepted 1ms 324.0 KiB
#20 Accepted 1ms 532.0 KiB
#21 Wrong Answer 1ms 532.0 KiB

Code

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

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

    vector<int> a(n);
    int maxVal = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] > maxVal)
            maxVal = a[i];
    }

    // Step 1: Build frequency of all elements to the right
    vector<int> freq(maxVal + 2, 0); // +2 for safety
    for (int i = 0; i < n; i++) {
        freq[a[i]]++;
    }

    // Step 2: Build suffix sum for ≥ condition
    vector<int> suffixSum(maxVal + 2, 0);
    for (int i = maxVal; i >= 0; i--) {
        suffixSum[i] = freq[i] + suffixSum[i + 1];
    }

    int specialCount = 0;
    vector<int> prefix(maxVal + 2, 0); // for left ≤ condition

    for (int i = 0; i < n; i++) {
        // Before checking right, remove current value (we're "at" this index)
        freq[a[i]]--;
        suffixSum[a[i]]--;

        // recompute suffixSum for current a[i]
        int rightGE = suffixSum[a[i]];

        // Compute prefix sum for ≤ a[i]
        int leftLE = 0;
        for (int k = 0; k <= a[i]; k++) {
            leftLE += prefix[k];
        }

        if (rightGE >= a[i] || leftLE >= a[i]) {
            specialCount++;
        }

        // Now add this element to prefix for future
        prefix[a[i]]++;
    }

    cout << specialCount << endl;
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1184 The Curious Kid and the Number Game
Contest
Brain Booster #9
Language
C++17 (G++ 13.2.0)
Submit At
2025-04-06 16:03:12
Judged At
2025-04-06 16:03:12
Judged By
Score
19
Total Time
1ms
Peak Memory
764.0 KiB