HDU 1394 minimum inversion number (line segment tree)

Source: Internet
Author: User

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 Input
 
101 3 6 9 0 8 5 7 4 2

Sample output
 
16

Authorchen, Gaoli
Sourcezoj monthly, January 2003

What is the reverse order of the minimum Reverse Order logarithm? This is for two numbers. For example, 2 1 and 2 are in front of 1, while 2 is greater than 1. This is a pair of reverse logarithm. The question is to put the first of the sequence at the end, output with the least logarithm of the reverse order in so many Sequences

First, the data is too big. We should use a line segment tree. The line segment tree stores the number of Le-to-ri values, for example, input 3, we need to query the number of existing numbers from 0 to 3. Let's just look at the code.

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define L (x) (x <1) # define R (X) (x <1 | 1) # define mid (x, y) (x + y)> 1) Using namespace STD; # define n 5005int A [n]; struct stud {int le, Ri; int Va;} f [N * 4]; void build (INT POs, int le, int RI) {f [POS]. le = Le; F [POS]. ri = Ri; F [POS]. va = 0; If (Le = RI) return; int mid = mid (Le, RI); Build (L (POS), Le, mid ); build (R (POS), Mid + 1, RI);} void Update (INT POs, in T le) {f [POS]. va ++; If (F [POS]. le = Le & F [POS]. ri = Le) return; int mid = mid (F [POS]. le, F [POS]. RI); If (mid> = Le) Update (L (POS), Le); else Update (R (POS), Le);} int query (INT POs, int le, int RI) {If (F [POS]. le> = Le & F [POS]. RI <= RI) return f [POS]. va; int mid = mid (F [POS]. le, F [POS]. RI); If (mid> = RI) return query (L (POS), Le, RI); else if (mid <le) return query (R (POS), Le, RI); Return query (L (POS), Le, mid) + query (R (POS), Mid + 1, RI);} int main () {int n, m, I; while (~ Scanf ("% d", & N) {int ans = 0; build (1, 0, n); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]); ans + = query (1, a [I], n-1); Update (1, A [I]);} int temp = ans; for (I = 0; I <n; I ++) {temp = temp-A [I] + N-A [I]-1; if (temp <ans) ans = temp;} printf ("% d \ n ", ans);} return 0 ;}







HDU 1394 minimum inversion number (line 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.