답안 #404925

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404925 2021-05-15T10:08:53 Z FatihSolak Exercise Deadlines (CCO20_day1problem2) C++17
0 / 25
3 ms 1912 KB
#include <bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
int arr[N];
int brr[N];
struct BIT{
    int n;
    vector<int> bit;
    BIT(int l){
        n = l+1;
        bit.assign(n,0);
    }
    void upd(int pos){
        for(pos++;pos<n;pos+=pos&-pos){
            bit[pos]++;
        }
    }
    int get(int pos){
        int ret = 0;
        for(pos++;pos>0;pos-=pos&-pos){
            ret += bit[pos];
        }
        return ret;
    }
}bt(N);
void solve(){
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> arr[i];
        brr[i] = arr[i];
    }
    sort(brr+1,brr+n+1);
    for(int i=1;i<=n;i++){
        if(brr[i] < i){
            cout << -1;
            return;
        }
    }
    int ans = 0;
    for(int i=1;i<=n;i++){
        if(arr[i] >i){
            continue;
        }
        ans += i+bt.get(i) - bt.get(arr[i]- 1) - arr[i];
        bt.upd(arr[i]);
    }
    cout << ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifdef Local
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    #ifdef Local
    cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1912 KB Output isn't correct
2 Halted 0 ms 0 KB -