Hdu 1394 Minimum Inversion Number (segment tree or tree-like array)

Source: Internet
Author: User
Tags acos cmath

Minimum Inversion numberTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 11981 Accepted Submission (s): 7321


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
RECOMMENDIGNATIUS.L | We have carefully selected several similar problems for you:1540 1542 1255 1828 1541



Test instructions

To find the minimum number of reverse order. n times, each time the first number is placed on the last side.


Exercises

Solve a set of reverse order numbers, then ans=ans+n-a[i]*2+1; find the minimum.


About reverse order number idea (big God God explanation): http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html

. After discretization, how to use the discrete result array to manipulate the tree array to calculate the number of reverse order?

If the data is not very large, it can be inserted into the tree array,

Each insertion of a number, counting the number of smaller numbers than he,

The corresponding reverse order is i-getsum (Aa[i]),

Where I is the number of currently inserted numbers,

Getsum (Aa[i]) is the number of smaller numbers than aa[i],

I-sum (Aa[i]) is larger than the number of aa[i, that is, the number of reverse order

However, if the data is large, the discretization method must be used

Assuming that the input array is 9 1 0 5 4, the result of the discretization is aa[] = {5,2,1,4,3};

Based on the intermediate results of discrete results, the process of calculating the inverse number is a process.

1, enter 5, call Update (5, 1), set 5th bit to 1

1 2 3) 4 5

0 0 0) 0 1

Do you calculate a number that is smaller than 5 on 1-5? Here is a tree-like array of getsum (5) = 1 operations,

Now use the input subscript 1-getsum (5) = 0 To get the reverse number of 5 for 0.

2. Enter 2, call Update (2, 1), set 2nd bit to 1

1 2 3) 4 5

0 1 0) 0 1

Do you calculate a number that is smaller than 2 on 1-2? Here is a tree-like array of getsum (2) = 1 operations,

Now use the input subscript 2-getsum (2) = 1 to get the reverse number of 2 for 1.

3. Enter 1, call Update (1, 1), set 1th bit to 1

1 2 3) 4 5

1 1 0) 0 1

Do you calculate a number that is smaller than 1 on 1-1? Here is a tree-like array of getsum (1) = 1 operations,

Now use the input subscript 3-getsum (1) = 2 to get the reverse number of 1 for 2.

4. Enter 4, call Update (4, 1), set 5th bit to 1

1 2 3) 4 5

1 1 0) 1 1

Do you calculate a number that is smaller than 4 on 1-4? Here is a tree-like array of getsum (4) = 3 operations,

Now use the input subscript 4-getsum (4) = 1 to get the reverse number of 4 for 1.

5. Enter 3, call Update (3, 1), set 3rd bit to 1

1 2 3) 4 5

1 1 1) 1 1

Do you calculate a number that is smaller than 3 on 1-3? Here is a tree-like array of getsum (3) = 3 operations,

Now use the input subscript 5-getsum (3) = 2 to get the reverse number of 3 for 2.

6.0+1+2+1+2 = 6 This is the final reverse number.

Analyze the time complexity, first use the fast sort, the time complexity is O (NLOGN),



CODE:

Segment Tree:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 5010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0); const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int tree[n&    lt;<2],n,a[n];void Build (int l,int r,int idx) {tree[idx]=0;    if (l==r) return;    int mid= (L+R) >>1;    Build (Lson); Build (Rson);}        void update (int l,int r,int idx,int x) {if (l==r) {tree[idx]++;    Return    } int mid= (L+R) >>1;    if (x<=mid) update (LSON,X);    else update (RSON,X); TREE[IDX]=TREE[RC]+TREE[LC];} int query (int l,int r,int idx,int x,int y) {if(L&GT;=X&AMP;&AMP;Y&GT;=R)    {return TREE[IDX];    } int ans=0;    int mid= (L+R) >>1;    if (x<=mid) ans+=query (lson,x,y);    if (y>mid) ans+=query (rson,x,y); return ans;}        int main () {while (cin>>n) {build (1,n,1);            for (int i=1; i<=n; i++) {scanf ("%d", &a[i]);        a[i]++;        } int ans=0;            for (int i=1; i<=n; i++) {ans+=query (1,n,1,a[i]+1,n);        Update (1,n,1,a[i]);        } int Min=ans;            for (int i=1;i<=n;i++) {ans+=n-2*a[i]+1;        Min=min (Min,ans);    } cout<<min<<endl; } return 0;}


Tree-like array:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0); const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int sum[500    5];int bit[5050],a[5050];int n;int getsum (int i) {int s=0;        while (i>0) {s+=bit[i];    i-=i&-i; } return s;}        void Add (int i,int x)//update {while (i<=n) {bit[i]+=x;    i+=i&-i;        }}int Main () {while (cin>>n) {memset (bit,0,sizeof bit);            for (int i=1; i<=n; i++) {scanf ("%d", &a[i]);        a[i]++; } int ans=0;            for (int i=1; i<=n; i++) {Add (a[i],1);            Sum[i]=i-getsum (A[i]);        Ans+=sum[i];        } int x=0;        int Min=ans;            for (int i=1; i<=n; i++) {ans+=n-a[i]*2+1;        Min=min (Min,ans);    } cout<<min<<endl; } return 0;}


Hdu 1394 Minimum Inversion Number (segment tree or tree-like array)

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.