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,res=0,treeL[300005],treeR[300005];
ii a[300005];
void UpdateL(int64_t idx)
{
while(idx>0)
{
treeL[idx]++;
idx-=idx&-idx;
}
}
int64_t QueryL(int64_t idx)
{
int64_t rs=0;
while(idx<=n)
{
rs+=treeL[idx];
idx+=idx&-idx;
}
return rs;
}
void UpdateR(int64_t idx)
{
while(idx<=n)
{
treeR[idx]++;
idx+=idx&-idx;
}
}
int64_t QueryR(int64_t idx)
{
int64_t rs=0;
while(idx>0)
{
rs+=treeR[idx];
idx-=idx&-idx;
}
return rs;
}
int main()
{
ios_base::sync_with_stdio(false);
//freopen("TEST.INP","r",stdin);
cin>>n;
for(int64_t i=1;i<=n;i++)
{
cin>>a[i].first;
a[i].second=i;
}
sort(a+1,a+n+1);
int64_t tmp=1,L=0,R=n+1;
for(int64_t i=2;i<=n;i++)
if(a[i].first!=a[i-1].first)
while(tmp<i)
{
int64_t moveL=a[tmp].second+QueryL(a[tmp].second)-L-1;
int64_t moveR=R-(a[tmp].second-QueryR(a[tmp].second))-1;
res+=min(moveL,moveR);
if(moveL<moveR)
{
L++;
UpdateL(a[tmp].second);
}
else
{
R--;
UpdateR(a[tmp].second);
}
tmp++;
}
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... |