标签:des style class code java ext
#include<stdio.h>
#include <algorithm>
using namespace std;
#define MAXN 50000
struct
Node{
int var;
int number;
};
Node
seq[MAXN*4];
void pushUp(int index){
seq[index].number=seq[index*2].number+seq[index*2+1].number;
}
void build(int
l,int r,int index){
seq[index].number=0;
if(l==r){
return ;
}
int mid=(l+r)/2;
build(l,mid,index*2);
build(mid+1,r,index*2+1);
}
int
Query(int from ,int to ,int
l,int r,int index){
int sum=0;
if(from<=l&&to>=r){
return
seq[index].number;
}
int mid=(l+r)/2;
if(from <=mid)
sum+=Query(from,to,l,mid,index*2);
if(to>mid)
sum+=
Query(from,to,mid+1,r,index*2+1);
return sum;
}
void
update(int p,int l,int
r,int index){
if(l==r){
seq[index].number++;
return ;
}
int mid = (l + r) /
2;
if (p <= mid)
update(p ,
l,mid,index*2);
else
update(p ,
mid+1,r,index*2+1);
pushUp(index);
}
int
main(){
int n;
while (~scanf("%d",&n))
{
build(0,n-1,1);
int sum=0;
for(int i=0;i<n;i++){
scanf("%d",&seq[i].var);
sum+= Query(seq[i].var+1,n-1,0,n-1,1);
update(seq[i].var,0,n-1,1);
}
//
printf("%d\n",sum);
int ans =
sum;
for (int
i = 0 ; i <
n ; i ++) {
sum
+= n - seq[i].var
- seq[i].var - 1;
ans =
min(ans , sum);
}
printf("%d\n",ans);
}
return 0;
}
【线段树】HDU 1394 Minimum Inversion Number,布布扣,bubuko.com
【线段树】HDU 1394 Minimum Inversion Number
标签:des style class code java ext
原文地址:http://www.cnblogs.com/zhaokongnuan/p/f4968f4f7eea1c347564d07735fadccd.html