# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
293916 | sabamaku | Exercise Deadlines (CCO20_day1problem2) | C++14 | 244 ms | 11616 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 n,d[200005],p[2000005];
vector <int> mp[200005];
priority_queue <int> q;
int solve(int a){
int x = 0;
for(int i = a; i > 0; i -= (i & -i)){
x += p[i];
}
return x;
}
void update(int a){
for(int i = a + 1; i <= n; i += (i & -i)){
p[i]++;
}
}
int a;
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> d[i];
mp[d[i]].push_back(i);
}
sort(d + 1,d + n + 1);
for(int i = 1; i <= n; i++){
if(d[i] < i){
cout << -1;
return 0;
}
}
int ans = 0;
for(int i = n; i >= 1; i--){
for(int j = 0; j < mp[i].size(); j++){
q.push(mp[i][j]);
}
a = q.top();
q.pop();
int x = solve(a);
ans += i - (a - x);
update(a);
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |