HDU 1394 Minimum Inversion Number (segment tree)

Source: Internet
Author: User

Minimum Inversion number Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 13797 Accepted Submission (s): 8423



Problem DescriptionThe Inversion number of a given number sequence A1, A2, ..., the number of pairs (AI, aj) that SA Tisfy 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'll Obtain another sequence. There is 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 is asked to write a program to find the minimum inversion number out of the above sequences.

Inputthe input consists of a number of test cases. Each case consists of the 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.

Outputfor each case, output the minimum inversion number to a single line.

Sample Input
101 3 6 9 0 8 5 7 4 2

Sample Output
16

Authorchen, Gaoli
Sourcezoj Monthly, January 2003

Test Instructions: input n, the following gives the number of n, respectively, is 0-n, sequence, can be moved by the sequence, each move the first number of moves to the back, asked by moving the formation of the sequence, so that the number of reverse order to get the least
This data is small, can be ordinary violent water over, of course, the more formal method is the line tree.
The title requires the smallest number of reverse order. Observation of the sequence, you can find a rule: the number of the first number of series moved to the last one, reverse order to become sum+n-1-2*a[i]. This rule is still relatively easy to push.
So what we have to do is to find the number of the first sequence in reverse order, the other can be obtained through this rule, and finally a minimum value can be found.
The explanations I see from others =_=:
A segment tree with a Val of 0 is established with the interval [0,9] as the root node,
Let's look at how to find the following sequence in reverse order:
1 3 6 9 0 8 5 7 4 2
Insert 1 in the segment tree, asking the interval [1,9] The number of nodes inserted before inserting (if present, it will be reversed with 1) v1=0
Insert 3 in the segment tree, asking the interval [3,9] The number of nodes inserted before inserting (if present, it will be reversed with 3) v2=0
Insert 6 in the segment tree, asking the interval [6,9] The number of nodes inserted before inserting (if present, it will be reversed with 6) v3=0
Insert 9 in the segment tree, asking the interval [9,9] The number of nodes inserted before inserting (if present, it will be reversed with 9) v4=0
Insert 0 in the segment tree, asking the interval [0,9] The number of nodes inserted before inserting (if present, it will be reversed with 0) v5=4
Insert 8 in the segment tree, asking the interval [8,9] The number of nodes inserted before inserting (if present, it will be reversed with 8) v6=1
Insert 5 in the segment tree, asking the interval [5,9] The number of nodes inserted before inserting (if present, it will be reversed with 5) v7=3
Insert 7 in the segment tree, asking the interval [7,9] The number of nodes inserted before inserting (if present, it will be reversed with 7) v8=2
Insert 4 in the segment tree, asking the interval [4,9] The number of nodes inserted before inserting (if present, it will be reversed with 4) v9=5
Insert 2 in the segment tree, asking the interval [2,9] The number of nodes inserted before inserting (if present, it will be reversed with 2) v10=7
Add v1+......+v10 = 22, this is the reverse number of 1 3 6 9 0 8 5 7 4 2.
is to insert a number into a segment tree, asking for a few more than this number before each insert. is actually asking how many numbers are inserted in a range!
#include <stdio.h> #include <string.h> #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&LT;=MID) update (l,r,root<<1); else if (l>mid) update (L,R, ROOT&LT;&LT;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&LT;=MID) return query (l,r,root<<1); else if (L>mid) return Query (L,R,ROOT&LT;&LT;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<n;i++) { scanf ("%d", &a[i]);   Tot=tot+query (a[i],n-1,1); Ask "a[i],n-1" There are several numbers, that is, ask than a[i] large number of several. Update (a[i],a[i],1);}     int Mini=tot;for (i=0;i<n;i++) {tot=tot+n-1-2*a[i];if (Tot<mini) Mini=tot;} printf ("%d\n", mini);} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1394 Minimum Inversion Number (segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.