HDU 1394 tree array (number of reverse orders)

Source: Internet
Author: User
Minimum inversion number

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10100 accepted submission (s): 6192


Problem descriptionthe 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.

 

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

 

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

 

Sample input101 3 6 9 0 8 5 7 4 2

 

Sample output16

 

 

Question meaning:

Give a sequence of N, A1, a2...... an-1,. Num0.0.

Then we get the first element A1 of the sequence to the end:

A2, a3...... an-1, an, and A1 are in the descending order of num1.

Get the first Element A2 of the sequence to the end:

A3, a4... an, a1, a2 at this time, number num2.

...

Continuously perform the preceding operations to obtain a reverse order number each time and find the minimum number of these reverse orders.

 

The number of reverse orders can be determined by the line segment tree, tree array, and merging and sorting. I will not elaborate on it here, but Baidu may be a friend I don't know.

Do I have to simulate the above operation and find the smallest one? No. In this case, it will definitely time out. The following describes a simple analysis method:

Set the number of reverse orders obtained for the first time to num0, and the number of reverse orders obtained for the second time to num1... and so on, you only need to find the n-1 reverse order number, and then repeat it. The first reverse order number can be obtained using the above three methods (it is best not to use the merge order, in this question, we need to save the original sequence. Merging and sorting will disrupt the original sequence. In this case, we need to open an auxiliary array.) num0 is used to find the original sequence. Observe the above operation. If A1 is placed at the end, because A1, the numbers that are later than A1, that is, the numbers that are not in the reverse order with A1, constitute the reverse order with A1 (set these numbers larger than A1 to S1, number smaller than A1 is S2), then num0 + 1, and the latter is smaller than A1, that is, the former and A1 constitute the number of reverse orders. At this time, it cannot be a reverse order with A1, num0-s2, so num1 = num0 + s1-s2.

Then A2 is in the first place. Repeat the preceding operation to obtain num2.

...

In this case, num0 ---- num (n-1) is obtained, and the smallest of them is the answer.

 

Code:

1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 # include <iostream> 5 using namespace STD; 6 # define n 5005 7 # define INF 999999999 8 9 int C [N]; 10 int A [n]; 11 12 INT lowbit (int x) {13 return X & (-x); 14} 15 16 void Update (int x) {17 while (x <n) {18 C [x] ++; 19 x + = lowbit (x); 20} 21} 22 23 int get_sum (int x) {24 int ans = 0; 25 while (x> 0) {26 ans + = C [X]; 27 X-= lowbit (x); 28} 29 return ans; 30} 31 32 Main () 33 {34 int N, I, j, k; 35 while (scanf ("% d", & n) = 1) {36 memset (C, 0, sizeof (c )); 37 int num = 0; 38 for (I = 1; I <= N; I ++) {39 scanf ("% d", & A [I]); 40 A [I] ++; 41 num + = get_sum (N-1)-get_sum (A [I]); 42 update (A [I]); 43} // use a tree array to obtain the number of reverse orders of sequence a 44 int Minh = inf; 45 for (I = 1; I <= N; I ++) 46 {47 num = num-(A [I]-1) + (n-A [I]); // The number of reverse orders at this time is 48 Minh = min (Minh, num) each time from the previous Reverse Order Number; // update the minimum value 49} 50 printf ("% d \ n ", minh); 51} 52}

 

Related Article

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.