[Nov 8 P2, reverse order] the person closest to God

Source: Internet
Author: User

 

  [Description]
The rune was cracked, and FF opened the road to the ground. When he got to the bottom layer, he found a huge rock door in front of him, The door is carved with a pattern of ancient people performing certain activities. On the top of the stone gate, the ancient text says "the temple of God". Small FF Conjecture It is time to have the estate of the royal family. But now the question is how to open this door ......
After careful research, he found that the patterns on the door probably mean that ancient people thought that only wise men were the easiest way to approach the gods. The smartest People are often selected through a ceremony. The ceremony generally refers to a list of unordered numbers written by the retired wise man for his candidate, And let them perform an operation, that is, exchange the adjacent two elements in the sequence. The original sequence is converted into a non-descending sequence with the least number of exchanges The person is the next Wise man.
Small FF found that there are n numbers on the door. So he thinks the secret to opening this door is to find a sequence that will not decrease. The minimum number of times required by the sequence. But little FF won't ...... I had to find you again, and promised to score you three minutes later ......
[Input format]
The first behavior is an integer N, indicating the sequence length.
The second row contains N integers, indicating each element in the sequence.
[Output format]
An integer (ANS), that is, the minimum number of operations.
[Input example]
4
2 8 0 3
[Output example]
3
Example: The start sequence is 2 8 0 3 , The target sequence is 0 2 3 8 , Three operations can be performed to obtain the target sequence:
1 . Swap ( 8 , 0 ): 2 0 8 3
2 . Swap ( 2 , 0 ): 0 2 8 3
3 . Swap ( 8 , 3 ): 0 2 3 8
[Data Scope]
For 30 % Data 1 <= N <= 10 ^ 4 .
For 100 % Data 1 <= N <= 5 * 10 ^ 5 ; - Maxlongint <= A [I] <= Maxlongint

 

 

This question is very similar to the Bubble sorting. The general idea is to change the location of the big and small, and point it to the place where it should be. At this time, we thought of Reverse Order pairs. Obviously, the number of Reverse Order pairs is the solution of this question, because as long as there is a reverse order pair, it is necessary to exchange it once. We know that the process of solving the reverse order is a modified merge order. After the reverse order is found, the array is sorted. In this way, the two problems are unified.

 

ReferenceCode:

 

 program sophist; var N: longint; I, J: longint; Tot: int64; A, B: array [0 .. 500000] of longint; Procedure msort (L, R: longint); // search for reverse var I, j, M, K: longint; begin if l> = r then exit; m: = (L + r) SHR 1; msort (L, M); msort (m + 1, R); I: = L; J: = m + 1; k: = L; repeat if a [I]> A [J] Then begin tot: = tot + M-I + 1; B [k]: = A [J]; INC (j); Inc (k); End else begin B [k]: = A [I]; Inc (I); Inc (k); end; until (I> m) or (j> r); while I <= m do begin B [k]: = A [I]; Inc (I ); INC (k); end; while j <= r do begin B [k]: = A [J]; Inc (j); Inc (k); end; for I: = L to r do a [I]: = B [I]; // update the end of array a; begin readln (n); for I: = 1 to n do read (A [I]); msort (1, N); writeln (TOT); end. 

 

 

Address: http://www.cnblogs.com/saltless/archive/2010/11/09/1873111.html

(Saltless original, reprinted please indicate the source)

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.