Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394
Idea: first calculate the number of reverse orders, then there is a technique (true pitfall) ...... can be done using a line segment tree or a tree array. The shift technique is that the reverse order number is reduced ant [I], while the n-1-ant [I] is increased.
#include <iostream>#include <cstring> #include <cstdio> #define MAXSIZE 500000using namespace std;struct node{ int l,r; int sum;}tree[MAXSIZE*4];void build(int root,int l,int r){ tree[root].l = l; tree[root].r = r; if(l == r) { return; } int mid = (l+r)/2; build(root*2,l,mid); build(root*2+1,mid+1,r);}void update(int root,int pos,int value){ if(tree[root].r == tree[root].l&&tree[root].l == pos) { tree[root].sum += value; return; } int mid = (tree[root].r+tree[root].l)/2; if(pos>mid) { update(root*2+1,pos,value); } if(pos<=mid) { update(root*2,pos,value); } tree[root].sum = tree[root*2].sum + tree[root*2+1].sum; }int query(int root,int l,int r){ if(tree[root].l==l&&tree[root].r==r) { return tree[root].sum; } int mid = (tree[root].r +tree[root].l)/2; if(r<=mid) { query(root*2,l,r); } else if(l>mid) { query(root*2+1,l,r); } else { int a = query(root*2,l,mid); int b = query(root*2+1,mid+1,r); return a+b; } }int main(){// freopen("data.txt","r",stdin); int n; int sum; int res; int ant[5500]; while(scanf("%d",&n)!=EOF) { memset(ant,0,sizeof(ant)); memset(tree,0,sizeof(tree)); build(1,0,n); sum = 0; for(int i=1;i<=n;i++) { scanf("%d",&ant[i]); sum += query(1,ant[i]+1,n); update(1,ant[i],1); } res = sum; for(int i = 1;i<=n;i++) { sum = sum+n-2*ant[i]-1; res = min(sum,res); } printf("%d\n",res); } return 0;}
There is an error... Wrong took us an hour .. That is, when the line segment tree is built, it must be open to N or it will overflow...
HDU 1394 minimum inversion number