This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,i,e,f,g,n,m,k,l,ans,BITree[500005],A[500005];
vector <long long> v[500005];
priority_queue <long long> pq;
long long getSum(long long idx) {
idx=idx+1;
long long sum=0;
while(idx>0) {
sum+=BITree[idx];
idx-=idx&(-idx);
}
return sum;
}
void updateBIT(long long idx,long long val) {
idx=idx+1;
while(idx<=200005) {
BITree[idx]+=val;
idx+=idx&(-idx);
}
}
int main() {
cin>>n;
for(long long i=1;i<=n;i++) {
cin>>A[i];
v[A[i]].push_back(i);
}
sort(A+1,A+1+n);
for(long long i=1;i<=n;i++) {
if(A[i]<i) { cout<<-1; return 0; }
}
for(long long i=n;i>=1;i--) {
for(long long j=0;j<v[i].size();j++)
pq.push(v[i][j]);
k=pq.top();
//cout<<k<<" ";
pq.pop();
a=getSum(k-1);
//cout<<a<<" ";
ans+=i-(k-a);
//cout<<ans<<endl;
updateBIT(k,1);
}
cout<<ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:33:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(long long j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |