hdu 1394 Minimum Inversion Number

来源:互联网 发布:js文件解压缩 编辑:程序博客网 时间:2024/06/14 16:55

Problem Description
The 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.
 

Input
The 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.
 

Output
For 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
 

思路:

如果求出第一种情况的逆序列,其他的可以通过递推来搞出来,一开始是t[1],t[2],t[3]....t[N]

它的逆序列个数是N个,如果把t[1]放到t[N]后面,逆序列个数会减少t[1]个,相应会增加N-(t[1]+1)个  

 

暴力法300ms:

[cpp] view plain copy
  1. #include<stdio.h>  
  2. int a[5555];  
  3. int main()  
  4. {  
  5.     int n,i,j,ans=999999999;  
  6.     while(scanf("%d",&n)!=EOF)  
  7.     {  
  8.         ans=999999999;  
  9.            for(i=0;i<n;i++) scanf("%d",&a[i]);  
  10.            int cnt=0;  
  11.            for(i=0;i<n;i++)  
  12.                for(j=i+1;j<n;j++)  
  13.                {  
  14.                    if(a[i]>a[j]) cnt++;  
  15.                }  
  16.            // printf("cnt=%d\n",cnt);  
  17.            if(ans>cnt)  ans=cnt;  
  18.            for(i=0;i<n;i++)  
  19.            {  
  20.                cnt=cnt-a[i]+n-1-a[i];  
  21.                if(ans>cnt)  ans=cnt;  
  22.             }  
  23.            printf("%d\n",ans);  
  24.     }  
  25.     return 0;  
  26. }  


下面说一下线段树的做法  31ms
用线段树去求输入序列的逆序数
方法:
把树的叶子节点作为每个数的对应位置
枚举到第i个数时,我们需要求出前i次插入的数中有多少个比a[i]大, 
即去寻找已经插入的数中比a[i]大的数的个数  即查询叶子节点a[i]到n的数的个数

[cpp] view plain copy
  1. #include<stdio.h>  
  2. int a[10000];  
  3. struct haha  
  4. {  
  5.     int left;  
  6.     int right;  
  7.     int num;  
  8. }node[10000*4];  
  9. void build(int left,int right,int nd)  
  10. {  
  11.     node[nd].left=left;  
  12.     node[nd].right=right;  
  13.     node[nd].num=0;  
  14.     if(left==right)   
  15.     {  
  16.         return ;  
  17.     }  
  18.     int mid=(left+right)/2;  
  19.     build(left,mid,nd*2);  
  20.     build(mid+1,right,nd*2+1);  
  21. }  
  22. int query(int left,int right,int nd)  
  23. {  
  24.     int mid=(node[nd].left+node[nd].right)/2;  
  25.     if(node[nd].left==left&&node[nd].right==right)  
  26.     {  
  27.         return node[nd].num;  
  28.     }  
  29.   
  30.     if(right<=mid)  
  31.     {  
  32.           return query(left,right,nd*2);  
  33.     }  
  34.     else if(left>mid)  
  35.     {  
  36.         return query(left,right,nd*2+1);  
  37.     }  
  38.     else  
  39.     {  
  40.         return query(left,mid,nd*2)+query(mid+1,right,nd*2+1);  
  41.     }  
  42. }  
  43. void update(int pos,int nd)  
  44. {  
  45.        
  46.     if(node[nd].left==node[nd].right) {node[nd].num++;return ;}  
  47.       
  48.     int mid=(node[nd].left+node[nd].right)/2;  
  49.     if(pos<=mid)  update(pos,nd*2);  
  50.     else update(pos,nd*2+1);  
  51.     node[nd].num=node[nd*2].num+node[nd*2+1].num;  
  52. }  
  53. int main()  
  54. {  
  55.     int n,i,j;  
  56.     while(scanf("%d",&n)!=EOF)  
  57.     {  
  58.           for(i=0;i<n;i++)  
  59.               scanf("%d",&a[i]);  
  60.           build(0,n-1,1);  
  61.           int sum=0;  
  62.           for(i=0;i<n;i++)  
  63.           {  
  64.               //printf("i=%d  sum=%d\n",i,sum);  
  65.               sum+=query(a[i],n-1,1);  
  66.              // printf(">>>");  
  67.               update(a[i],1);  
  68.           }  
  69.          // printf("%d\n",sum);  
  70.           int ans=99999999;  
  71.           if(ans>sum)  ans=sum;  
  72.            for(i=0;i<n;i++)  
  73.            {  
  74.                sum=sum-a[i]+n-1-a[i];  
  75.                if(ans>sum) ans=sum;  
  76.            }  
  77.   
  78.                printf("%d\n",ans);  
  79.     }  
  80.     return 0;  
  81. }  


下面是归并排序方法:

套用归并排序模板

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<malloc.h>  
  3. int ans,a[5050],b[5050];  
  4. void merge(int left,int mid,int right)  
  5. {  
  6.     int i,j,cnt=0;  
  7.     int *p;  
  8.     p=(int *)malloc((right-left+1)*sizeof(int));  
  9.     i=left;  
  10.     j=mid+1;  
  11.     while(i<=mid&&j<=right)//这时候i 和 j 指向的部分都排序完毕了 现在合并  
  12.     {  
  13.         if(a[i]<=a[j])  
  14.         {  
  15.             p[cnt++]=a[i];  
  16.             i++;  
  17.         }  
  18.         else  
  19.         {  
  20.             p[cnt++]=a[j];  
  21.             j++;  
  22.             ans+=mid-i+1;//第i个比j大 由于i已经从小到大排过序了 那么i+1到mid的也会比j大  
  23.         }  
  24.     }  
  25.     while(i<=mid)  
  26.     {  
  27.         p[cnt++]=a[i++];  
  28.     }  
  29.     while(j<=right)  
  30.     {  
  31.         p[cnt++]=a[j++];  
  32.     }  
  33.     cnt=0;  
  34.     for(i=left;i<=right;i++)  
  35.         a[i]=p[cnt++];  
  36.     free(p);  
  37.   
  38. }  
  39. void merge_sort(int left,int right)  
  40. {  
  41.     if(left<right) //长度大于1  这是个判断不是循环  
  42.     {  
  43.         int mid;  
  44.         mid=(left+right)/2;  
  45.         merge_sort(left,mid);  
  46.         merge_sort(mid+1,right);  
  47.         merge(left,mid,right);  
  48.     }  
  49. }  
  50.   
  51. int main()  
  52. {  
  53.     int n,i,j ;  
  54.     while(scanf("%d",&n)!=EOF)  
  55.     {  
  56.   
  57.     for(i=0;i<n;i++) {scanf("%d",&a[i]);b[i]=a[i];}  
  58.         ans=0;  
  59.         merge_sort(0,n-1);  
  60.         //printf("ans=%d\n",ans);  
  61.         int cnt=999999999;  
  62.         if(cnt>ans) cnt=ans;  
  63.   
  64.            for(i=0;i<n;i++)  
  65.            {  
  66.                //printf("a[i]=%d\n",a[i]);  
  67.                ans=ans-b[i]+n-1-b[i];  
  68.                if(cnt>ans)  cnt=ans;  
  69.             }  
  70.            printf("%d\n",cnt);  
  71.     }  
  72.     return 0;  
  73. }  



0 0