Submission #1282788

#TimeUsernameProblemLanguageResultExecution timeMemory
1282788herominhsteveExercise Deadlines (CCO20_day1problem2)C++20
25 / 25
118 ms11404 KiB
#include <bits/stdc++.h>
#define el '\n'
#define FNAME "Jop"
#define allof(x) x.begin(),x.end()
#define allof1(x) x.begin()+1,x.end()
#define mset(x,n) memset(x,(n),sizeof(x))
using namespace std;
const long long MOD = (long long) 1e9 + 7;
template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;}
template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;}

void setup(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
	if (fopen(FNAME".inp","r")){
		freopen(FNAME".inp","r",stdin);
		freopen(FNAME".out","w",stdout);
	}
}

template<typename T>
struct FenwickTree{
    int n;
    vector<T> bit;
    FenwickTree(int N=0){
        n=N;
        if (n>0){
            bit.resize(n+1,0);
        }
    }
    void update(int node,T val){
        while (node<=n){
            bit[node]+=val;
            node += (node & -node);
        }
    }
    T getVal(int node){
        T res=0;
        while (node>0){
            res+=bit[node];
            node -= (node & -node);
        }
        return res;
    }
    T range(int l,int r){
        return getVal(r) - getVal(l-1);
    }
};

int n;
vector<int> a;

void init(){
	cin>>n;
    a.assign(n+1,0);
    for (int i=1;i<=n;i++) cin>>a[i];
}

void sol(){
    set<int> processed;
	for (int i=1;i<=n;i++) processed.insert(-i);
    vector<int> pos(n+1,0);
    for (int i=n;i>=1;i--){
        set<int>::iterator it = processed.lower_bound(-a[i]);
        if (it==processed.end()){
            cout<<-1;
            exit(0);
        }
        pos[i] = (*it) * -1;
        processed.erase(it);
    }
    FenwickTree<long long> bit(n);
    long long res = 0;
    for (int i=1;i<=n;i++){
        res += bit.range(pos[i],n);
        bit.update(pos[i],+1);
    }
    cout<<res;
}

int main(){
    setup();
    init();
    sol();
}

Compilation message (stderr)

Main.cpp: In function 'void setup()':
Main.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...