#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)
{
int id=-1;
for(int j=tmp; j<i; j++)
{
int64_t pos=a[j].second+QueryL(a[j].second)-QueryR(a[j].second);
int64_t moveL=pos-L-1;
int64_t moveR=R-pos-1;
if(moveL<moveR)
{
res+=moveL;
L++;
UpdateL(a[j].second);
}
else
{
id=j;
break;
}
}
if(id!=-1)
for(int j=i-1; j>=id; j--)
{
int64_t pos=a[j].second+QueryL(a[j].second)-QueryR(a[j].second);
int64_t moveR=R-pos-1;
res+=moveR;
R--;
UpdateR(a[j].second);
}
tmp=i;
}
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
424 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
476 KB |
Output is correct |
7 |
Correct |
2 ms |
524 KB |
Output is correct |
8 |
Correct |
2 ms |
528 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
720 KB |
Output is correct |
2 |
Correct |
2 ms |
720 KB |
Output is correct |
3 |
Correct |
2 ms |
720 KB |
Output is correct |
4 |
Incorrect |
3 ms |
720 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
2156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |