## 题目分析

#include <cstdio>
#include <algorithm>
using namespace std;
#define mid (L+R)/2
#define lson o<<1, L, mid
#define rson o<<1|1, mid+1, R
const int maxn = 5005;
int sum[maxn<<2];

void pushup(int o){
sum[o] = sum[o<<1] + sum[o<<1|1];
}

void build(int o,int L,int R){
sum[o] = 0;
if(L == R) return ;
build(lson);
build(rson);
}

void update(int o,int L,int R,int p)
{
if(L == R){
sum[o]++;
return ;
}
if(p <= mid) update(lson, p);
else update(rson, p);
pushup(o);
}

int query(int o,int L,int R,int l,int r)
{
if(l <= L && R <= r)
return sum[o];
int ret = 0;
if(l <= mid) ret += query(lson, l, r);
if(r > mid) ret += query(rson, l, r);
return ret;
}

int a[maxn];
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
build(1, 0, n-1);
int sum = 0;
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum += query(1, 0, n-1, a[i], n-1);
update(1, 0, n-1, a[i]);
}
int ret = sum;
for(int i = 0; i < n; i++)
{
sum += n - 2*a[i] - 1;
ret = min(ret, sum);
}
printf("%d\n", ret);
}
return 0;
}


#### hdu1394Minimum Inversion Number(递归)

2015-12-09 19:40:52

#### hdu-1394-Minimum Inversion Number(树状数组)

2016-07-15 13:36:21

#### hdu Minimum Inversion Number（线段树求逆序数有关问题的一个小归纳）

2012-08-29 15:47:01

#### hdu 1394 Minimum Inversion Number

2013-09-01 19:39:11

#### HDU:1394 Minimum Inversion Number

2014-01-26 23:50:22

#### hdu(1394)——Minimum Inversion Number

2015-06-04 18:41:10

#### Hdu 1394 Minimum Inversion Number

2017-11-09 10:22:56

#### HDU 1394:Minimum Inversion Number

2017-07-13 22:27:03

#### Hdu 1394 Minimum Inversion Number

2013-08-03 20:38:51

## 不良信息举报

HDU 1394 Minimum Inversion Number