Submission #307133

# Submission time Handle Problem Language Result Execution time Memory
307133 2020-09-27T07:27:35 Z Tamarpeikrishvili Exercise Deadlines (CCO20_day1problem2) C++14
0 / 25
1000 ms 5120 KB
#include <bits/stdc++.h>
using namespace std;

int tree[200005], ans = 0;
vector <int> v[200005];
priority_queue <int> pq;

int get(int idx){
        int sum = 0;
        while (idx > 0){
                sum += tree[idx];
                idx = idx - (idx & -idx);
        }
        return sum;
}

void upd(int idx ,int val, int MaxVal){
        while (idx <= MaxVal){
                tree[idx] += val;
                idx += (idx & -idx);
        }
}

int main (){
        int n;
        cin >> n;

        int a[n];
        for(int i=1; i<=n; i++) {
                cin >> a[i];
                
                v[a[i]].push_back(i);
        }

        sort(a+1, a+n+1); 

        for(int i=1; i<=n; i++){
                if(a[i] < i){
                        cout << -1 << endl;
                        return 0;
                }
        }
        for(int i=1; i<=n; i++) upd(i , 1, n);
        

        for(int i=n; i>0; i--){
                for(int j=0; j<v[i].size(); j++) {
                        pq.push(v[i][j]);
                }
                int k = pq.top(); 
                pq.pop();

                upd(n - k + 1, -1, n);
                ans = get(n - k + 1) + ans;
        }
        cout << ans;
 

}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:47:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |                 for(int j=0; j<v[i].size(); j++) {
      |                              ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 5120 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1094 ms 5120 KB Time limit exceeded
2 Halted 0 ms 0 KB -