답안 #293578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293578 2020-09-08T08:17:28 Z achibasadzishvili Exercise Deadlines (CCO20_day1problem2) C++14
0 / 25
1000 ms 12416 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,a[500005];
set<ll>st;
set<ll>::iterator it;
ll b[500005],ans;
vector<ll>g[500005];
void add(ll x,ll y){
    while(y<=n){
        b[y]+=x;
    	y+=y&-y;
    }
}
ll sum(ll y){
    ll x=0;
	while(y>0){
        x+=b[y];
        y-=y&-y;
    }
    return x;
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    
    for(int i=1; i<=n; i++){
        cin >> a[i];
        g[a[i]].pb(i);
    }
    
    for(int i=n; i>=1; i--){
        for(int j=0; j<g[i].size(); j++)
            st.insert(g[i][j]);
        if(!st.size()){
            cout << -1;
            return 0;
        }
        it = st.end();
        it--;
        ll ind = (*it);
        st.erase(it);
        ind -= sum(ind);
        ans += i - ind;
        add(1 , ind);
    }
    
    cout << ans << '\n';
    
    
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for(int j=0; j<g[i].size(); j++)
      |                      ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1071 ms 12416 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1071 ms 12416 KB Time limit exceeded
2 Halted 0 ms 0 KB -