Submission #293581

#TimeUsernameProblemLanguageResultExecution timeMemory
293581achibasadzishviliExercise Deadlines (CCO20_day1problem2)C++14
25 / 25
160 ms27616 KiB
#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); ll w = ind; st.erase(it); ind -= sum(ind); ans += i - ind; add(1 , w); } cout << ans << '\n'; return 0; }

Compilation message (stderr)

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++)
      |                      ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...