#include<cstdio>
#include<algorithm>
std::pair<int,int> a[300001];
int BIT[300001];
void upd(int x){while(x<=300000){BIT[x]++;x+=x&-x;}}
int sum(int x){int r=0;while(x){r+=BIT[x];x-=x&-x;}return r;}
int main()
{
long long r=0;
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i].first);
a[i].second=i;
}
std::sort(a+1,a+n+1);
for(i=1;i<=n;i++)
{
r+=std::min(a[i].second-sum(a[i].second)-1,n-a[i].second-i+sum(a[i].second)+1);
upd(a[i].second);
}
printf("%lld",r);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4604 KB |
Output is correct |
2 |
Correct |
0 ms |
4604 KB |
Output is correct |
3 |
Correct |
0 ms |
4604 KB |
Output is correct |
4 |
Correct |
0 ms |
4604 KB |
Output is correct |
5 |
Correct |
0 ms |
4604 KB |
Output is correct |
6 |
Correct |
0 ms |
4604 KB |
Output is correct |
7 |
Correct |
0 ms |
4604 KB |
Output is correct |
8 |
Correct |
0 ms |
4604 KB |
Output is correct |
9 |
Correct |
0 ms |
4604 KB |
Output is correct |
10 |
Incorrect |
0 ms |
4604 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4604 KB |
Output is correct |
2 |
Incorrect |
0 ms |
4604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
4604 KB |
Output is correct |
2 |
Incorrect |
0 ms |
4604 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
4604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |