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;
typedef pair<int64_t,int64_t> ii;
int64_t n,a[300005],f1[300005],f2[300005],tree[300005];
ii b[300005];
void Update(int64_t idx)
{
while(idx>0)
{
tree[idx]++;
idx-=idx&-idx;
}
}
int64_t Query(int64_t idx)
{
int64_t rs=0;
while(idx<=n)
{
rs+=tree[idx];
idx+=idx&-idx;
}
return rs;
}
int main()
{
cin>>n;
for(int64_t i=1;i<=n;i++)
{
cin>>a[i];
b[i]=ii(a[i],i);
}
sort(b+1,b+n+1);
a[b[1].second]=1;
int Count=1;
for(int64_t i=2;i<=n;i++)
if(b[i].first>b[i-1].first)
a[b[i].second]=++Count;
else
a[b[i].second]=Count;
for(int64_t i=1;i<=n;i++)
{
f1[i]=f1[i-1]+Query(a[i]+1);
Update(a[i]);
}
for(int64_t i=1;i<=n;i++)
tree[i]=0;
for(int64_t i=n;i>=1;i--)
{
f2[i]=f2[i+1]+Query(a[i]+1);
Update(a[i]);
}
int64_t res=1e12;
for(int64_t i=1;i<=n;i++)
res=min(res,min(f1[i]+f2[i+1],f1[i-1]+f2[i]));
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |