HDU 1394 Problem Solving report minimum inversion number

Source: Internet
Author: User
Minimum inversion number

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


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 is also a classic line segment tree. First, you must know how to find the reverse sequence of a sequence. For example, before 2514: 2, there is no number greater than it, so it is 0. before 5, there is no number greater than it, so for 0, 2, 5 before 1 are greater than it, SO 2, 4 before only 5 is greater than it, so it is 1, so the number of reverse orders of 2514 is: 0 + 0 + 2 + 1 = 3; from the above description, we can find that, as long as we accumulate the first few numbers in turn to exceed the current number. Therefore, we can use an array L [N] (initialized to 0), and then identify L [x [I] + = 1 for each read number x [I, however, before the identification, we need to calculate how many numbers are greater than X [I], that is, the cumulative L [x [I] + 1] +... + L [n-1], apparently the accumulative time complexity is O (n). Can we hurry up? How can we find the smallest number of reverse orders? (I suppose each number in a sequence is different here.) If the reverse order of ABCDE... Is Sum, what is the reverse order of bcde...? Let's assume that ABCDE... if the number smaller than a is X [I], then the number greater than a is n-X [I]. When a is moved to the left, the previously larger than a has now become the reverse direction of A, so X [I] + 1 is reduced, that is to say, N-X [I]-X [I]-1 is added in total. In fact, the line segment tree is used only once, and the minimum value is useless, so should be able to use violence, sum and then update the N--1 times can get the minimum value, not complex!

So I

#include <iostream>#include<stdio.h>using namespace std;#define N 50000int l[N<<2],x[N];void build(int num ,int s,int e){      l[num]=0;      if(s==e)            return ;      int mid=(s+e)>>1;      build(num<<1,s,mid);      build(num<<1|1,mid+1,e);}int query(int num ,int s,int e ,int a,int b){      if(a<=s&&b>=e)            return l[num];      int mid=(s+e)>>1;      int ret=0;      if(mid>=a)            ret+=query(num<<1,s,mid,a,b);      if(mid<b)            ret+=query(num<<1|1,mid+1,e,a,b);      return ret;}void update(int num ,int s,int e,int a){      if(s==e)      {           l[num]++;           return ;      }      int mid=(s+e)>>1;      if(a<=mid)            update(num<<1,s,mid,a);      else            update(num<<1|1,mid+1,e,a);      l[num]=l[num<<1]+l[num<<1|1];}int main(){    int n,i;    while(scanf("%d",&n)!=EOF)    {          build(1,0,n-1);          int sum=0;         for(i=0;i<n;i++)         {               scanf("%d",&x[i]);               sum=sum+query(1,0,n-1,x[i],n-1);               update(1,0,n-1,x[i]);         }         int re=sum;         for(i=0;i<n-1;i++)         {               sum+=n-x[i]-x[i]-1;               if(sum<re)                  re=sum;         }         printf("%d\n",re);    }    return 0;}

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.