HDU 1394 Minimum Inversion Number (segment tree, single-point update)

Source: Internet
Author: User

C-minimum Inversion number crawling in process ... crawling failed time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u SubmitStatus Practice HDU 1394Appoint Description:System Crawler (2015-08-17)

Description

The inversion number of a given number sequence A1, A2, ..., 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'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.

Input

The 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.

Output

For each case, output the minimum inversion number in a single line.

Sample Input

101 3 6 9 0 8 5 7 4 2

Sample Output

16 topic: This problem is to say, give you an arrangement of length n (composed of 0 to n-1), each time you can put the first element to the last surface, then will produce the reverse number, a permutation of the number of reverse is equal to the permutation of all the numbers in the inverse number of the sum. So, this problem, can be solved with the line segment tree, we first establish an empty tree, that is, the sum==0 of each node in the tree, representing all 0, and then, each time we insert a number, in the corresponding position of this number to put a 1, that is, according to the weights to establish the tree segment, The right value maintained by the leaf nodes in the bottom layer of the line tree is 0~n-1. Each time you insert a number, just count how many numbers are larger than the number you just inserted. Then we know that for an arrangement consisting only of 0~n-1, the first number is put to the last, and the contribution is reduced by a[i], and the n-1-a[i is increased). Then make a contribution every time, until you find the smallest contribution. Code:
# include<cstdio># include<iostream>using namespace std;# define MAX 5004# define INF 99999999# define lid id&    lt;<1# define RID id<<1|1struct segtree{int l,r; int sum;} Tree[max*4];int a[max];void push_up (int id) {tree[id].sum = Tree[lid].sum+tree[rid].sum;}    void build (int id,int l,int r) {tree[id].l = l; tree[id].r = R;    tree[id].sum = 0;    if (l==r) {return;    } int mid = (TREE[ID].L+TREE[ID].R)/2;    Build (Lid,l,mid);    Build (Rid,mid+1,r); PUSH_UP (ID);}        void update (int id,int x,int val) {if (TREE[ID].L==TREE[ID].R) {tree[id].sum = 1;    Return    } int mid = (TREE[ID].L+TREE[ID].R)/2;    if (x<=mid) update (lid,x,val);    else update (rid,x,val); PUSH_UP (ID);}    int query (int id,int l,int R) {if (tree[id].l==l&&tree[id].r==r) {return tree[id].sum;    } int mid = (TREE[ID].L+TREE[ID].R)/2;    if (R <= mid) return query (LID,L,R);else if (L > Mid) return query (RID,L,R);    else {return query (Lid,l,mid) +query (rid,mid+1,r);    }}int Main (void) {int n;        while (scanf ("%d", &n)!=eof) {for (int i = 0;i < n;i++) scanf ("%d", &a[i]);        Build (1,0,n-1);        int sum = 0;                for (int i = 0;i < n;i++) {if (a[i]!=n-1) {sum+= query (1,a[i]+1,n-1);            Update (1,a[i],1);                } else {sum+=query (1,a[i],n-1);            Update (1,a[i],1);        }} int ans = inf;        ans = min (ans,sum);            for (int i = 0;i < n;i++) {sum-=a[i];            Sum+=n-1-a[i];        ans = min (ans,sum);    } printf ("%d\n", ans); } return 0;}

  

HDU 1394 Minimum Inversion Number (segment tree, single-point update)

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.