답안 #307142

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
307142 2020-09-27T07:44:00 Z NintsiChkhaidze Exercise Deadlines (CCO20_day1problem2) C++14
0 / 25
5 ms 5248 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define left (node<<1),l,((l+r)>>1)
#define right ((node<<1)|1),((l+r)>>1) + 1,r
#define N 200005
#define int long long
using namespace std;
priority_queue <int> q;
int fen[200005];
vector <int> v[200005];
void upd(int in,int val){
    while (in <= N){
        fen[in]+=val;
        in += (in&(-in));
    }
}
int get(int in){
    int sum=0;
    while (in > 0){
        sum+=fen[in];
        in -= (in&(-in));
    }
    return sum;
}
main(){
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    
    for (int i=1;i<=n;i++){
        int a;
        cin>>a;
        v[a].pb(i);
    }
    for (int i = 0; i < v[n].size(); i++)
        q.push(v[n][i]);
 
    int ind = n,ans = 0;
    while(q.size()){
        int x = q.top();
        q.pop();
        //cout<<x<<" "<<ind<<endl;
        ans+=ind - x;
        upd(x,1),upd(ind + 1,-1);
        if (ind > 1){
            ind--;
            for (int i = 0; i < v[ind].size(); i++){
                q.push(v[ind][i] - get(v[ind][i] - 1));
                //cout<<v[ind][i]<<" - "<<get(v[ind][i] - 1)<<endl;
            }
        }
    }
    if (ind != 1 && q.size() == 0) cout<<-1;
    else cout<<ans;
}

Compilation message

Main.cpp:27:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   27 | main(){
      |      ^
Main.cpp: In function 'int main()':
Main.cpp:37:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int i = 0; i < v[n].size(); i++)
      |                     ~~^~~~~~~~~~~~~
Main.cpp:49:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |             for (int i = 0; i < v[ind].size(); i++){
      |                             ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5248 KB Output isn't correct
2 Halted 0 ms 0 KB -