Hdoj 1394 Minimum inversion number in order to find the smallest inverse of the loop string (violence && line tree)

Source: Internet
Author: User

Minimum Inversion number Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 14879 Accepted Submission (s): 9082


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




AC Code:

Brute Force Solution: 425ms

#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include < algorithm> #define INF 0x7fffffff#define MAXN 10010#define Max (A, b) A>b?a:b#define min (A, b) a>b?b:ausing Namespace Std;int num[maxn];int Main () {int i,j,n;while (scanf ("%d", &n)!=eof) {int m;int cnt=0;for (i=0;i<n;i++) scanf ("%d", &num[i]); for (i=0;i<n;i++) {for (j=i+1;j<n;j++) {if (num[j]<num[i]) cnt++;}} M=cnt;for (i=0;i<n;i++) {cnt=cnt-num[i]+n-1-num[i];if (m>cnt) m=cnt;} printf ("%d\n", M);} return 0;}
See a lot of people use line tree to write, I also want to write, tomorrow!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 1394 Minimum inversion number in order to find the smallest inverse of the loop string (violence && line 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.