This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |