Submission #293549

#TimeUsernameProblemLanguageResultExecution timeMemory
2935493zpExercise Deadlines (CCO20_day1problem2)C++14
25 / 25
230 ms14700 KiB
#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)

Main.cpp:20:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   20 | main(){
      |      ^
Main.cpp: In function 'int main()':
Main.cpp:37:31: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |         while(q.size() + f[i] > i){
      |               ~~~~~~~~~~~~~~~~^~~
Main.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for(int i = 0; i < a.size(); i++){
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...