Hdu 1394 Minimum Inversion Number (line segment tree)
Minimum Inversion NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 13797 Accepted Submission (s): 8423
Problem Description The inversion number of a given number sequence a1, a2,..., an is the number of pairs (ai, aj) that satisfy I <j and ai> aj.
For a given sequence of numbers a1, a2 ,..., an, if we move the first m> = 0 numbers to the end of the seqence, we will obtain another sequence. there are totally n such sequences as the following:
A1, a2,..., an-1, an (where m = 0-the initial seqence)
A2, a3,..., an, a1 (where m = 1)
A3, a4,..., an, a1, a2 (where m = 2)
...
An, a1, a2,..., an-1 (where m = N-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
Input The input consists of a number of test cases. each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
Output For each case, output the minimum inversion number on a single line.
Sample Input
101 3 6 9 0 8 5 7 4 2
Sample Output
16
Author CHEN, Gaoli
Source ZOJ Monthly, January 2003
Question: Enter n. The numbers of n are given below, which are 0-n. The order is not fixed. You can move the sequence and move the first number to the back of each movement, ask the sequence formed by moving to minimize the number of reverse orders.
The data for this question is small, and it can be used by violent water. Of course, the more formal method is the line segment tree.
The minimum number of reverse orders is required. Observe the series, we can find a rule: Move the first number of the series to the last bit, the number of reverse order into sum + n-1-2 * a [I]. This rule is easy to push.
So what we need to do is to find the reverse number of the first series. Other values can be obtained through this rule, and finally we can find a minimum value.
My explanations from the perspective of others = _ =:
First, a line segment tree with val values of 0 is created for the root node in the interval,
Let's take a look at how to calculate the reverse number of the following sequence:
1 3 6 9 0 8 5 7 4 2
Insert 1 in the online segment tree. before inserting, ask the number of inserted nodes in the interval [] (If yes, it must be in reverse order with 1) v1 = 0
Insert 3 in the online segment tree. before inserting, ask the number of inserted nodes in the interval [3, 9] (If yes, it must be in reverse order with 3) v2 = 0
Insert 6 to the online segment tree. before inserting, ask [6, 9] about the number of inserted nodes (if any, it must be in reverse order with 6). v3 = 0
Insert 9 in the online segment tree. before inserting, ask [9, 9] The number of inserted nodes (if any, it must be in reverse order with 9) v4 = 0
Insert 0 to the online segment tree. before inserting, ask [0, 9] about the number of inserted nodes (If yes, it must be in reverse order with 0) v5 = 4
Insert 8 into the online segment tree. before inserting, ask the number of inserted nodes in the range [8, 9] (If yes, it must be in reverse order with 8) v6 = 1
Insert 5 to the online segment tree. before inserting, ask [5, 9] The number of inserted nodes (if any, it must be in reverse order with 5). v7 = 3
Insert 7 to the online segment tree. before inserting, ask the number of inserted nodes in the range [] (If yes, it must be in reverse order with 7) v8 = 2
Insert 4 in the online segment tree. before inserting, ask the number of inserted nodes in the interval [] (If yes, it must be in reverse order with 4). v9 = 5
Insert 2 in the online segment tree. before inserting, ask the number of inserted nodes in the interval [] (If yes, it must be in reverse order with 2). v10 = 7
Accumulate v1 + ...... + V10 = 22, which is the reverse number of 1 3 6 9 0 8 5 7 4 2.
It is the number of inserts to the line tree one by one. Before each insert, there are several larger numbers. It is actually to ask how many numbers are inserted in a range!
#include
#include
#define M 5005struct tree{int l,r,sum;}tree[M<<2];int x[M];int max(int a,int b){if(a>b)return a;else return b;}void build(int l,int r,int root){tree[root].l=l;tree[root].r=r;tree[root].sum=0;if(l==r)return;int mid=l+r>>1;build(l,mid,root<<1);build(mid+1,r,root<<1|1);}void pushup(int root){if(tree[root].l==tree[root].r)return;tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;}void update(int l,int r,int root){if(tree[root].l==l&&tree[root].r==r){tree[root].sum++;return;}int mid=tree[root].l+tree[root].r>>1;if(r<=mid)update(l,r,root<<1);else if(l>mid)update(l,r,root<<1|1);else {update(l,mid,root<<1);update(mid+1,r,root<<1|1);}pushup(root);}int query(int l,int r,int root){if(tree[root].l==l&&tree[root].r==r){return tree[root].sum;}int mid=tree[root].l+tree[root].r>>1;if(r<=mid)return query(l,r,root<<1);else if(l>mid)return query(l,r,root<<1|1);else return query(l,mid,root<<1)+query(mid+1,r,root<<1|1);}int main(){int n,m,i,j,k,a[M],b,tot;while(scanf(%d,&n)!=EOF){tot=0;build(0,n-1,1);for(i=0;i