Submission #268286

#TimeUsernameProblemLanguageResultExecution timeMemory
268286imeimi2000Exercise Deadlines (CCO20_day1problem2)C++17
25 / 25
157 ms14844 KiB
#include <bits/stdc++.h>

using namespace std;

int n;
int D[200001];
vector<int> L[200001];
int R[200001];

int fen[200001];
void update(int i) {
    while (i <= n) {
        ++fen[i];
        i += i & -i;
    }
}

int query(int i) {
    int ret = 0;
    while (i) {
        ret += fen[i];
        i -= i & -i;
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> D[i], L[D[i]].push_back(i);
    priority_queue<int> pq;
    long long ans = 0;
    for (int i = n; i > 0; --i) {
        for (int j : L[i]) pq.push(j);
        if (pq.empty()) {
            printf("-1\n");
            return 0;
        }
        R[i] = pq.top(); pq.pop();
        ans += query(R[i]);
        update(R[i]);
    }
    printf("%lld\n", ans);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...