# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
293549 | 3zp | Exercise Deadlines (CCO20_day1problem2) | C++14 | 230 ms | 14700 KiB |
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 a[200009],d[200009],f[200009],F[200009];
vector<int> v[200009];
int n;
int cnt(int x){
int ans = 0;
while(x){
ans += F[x];
x -= x&-x;
}
return ans;
}
void upd(int x){
while(x <= n){
F[x] ++;
x += x&-x;
}
}
main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> d[i];
f[d[i]]++;
v[d[i]].push_back(i);
}
vector<int> a;
priority_queue<int> q;
for(int i = 1; i <= n; i++){
f[i] += f[i-1];
if(f[i] > i){
cout<<-1<<endl;
return 0;
}
}
for(int i = n; i >= 0; i--){
while(q.size() + f[i] > i){
a.push_back(q.top());
q.pop();
}
for(int x : v[i])
q.push(x);
}
long long ans = 0;
reverse(a.begin(), a.end());
for(int i = 0; i < a.size(); i++){
ans += i - cnt(a[i]);
upd(a[i]);
}
cout<<ans<<endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |