답안 #307137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307137 2020-09-27T07:30:06 Z Tamarpeikrishvili Exercise Deadlines (CCO20_day1problem2) C++14
0 / 25
1000 ms 5120 KB
#include <bits/stdc++.h>
using namespace std;

int tree[200005];
long long 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 (){
        ios_base::sync_with_stdio(0); cin.tie(0);
        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:49:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |                 for(int j=0; j<v[i].size(); j++) {
      |                              ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1095 ms 5120 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1095 ms 5120 KB Time limit exceeded
2 Halted 0 ms 0 KB -