hdu 1394 求循环串的最小逆序数 暴力法 线段树 归并排序3种方法

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6743    Accepted Submission(s): 4112


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

Sample Output
  
  
16
 

Author
CHEN, Gaoli
 

Source
 

Recommend
Ignatius.L
 
题意:
一个由0..n-1组成的序列,每次可以把队首的元素移到队尾,
          求形成的n个序列中最小逆序对数目
 
思路:

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

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

 

暴力法300ms:

#include<stdio.h>
int a[5555];
int main()
{
    int n,i,j,ans=999999999;
    while(scanf("%d",&n)!=EOF)
    {
        ans=999999999;
           for(i=0;i<n;i++) scanf("%d",&a[i]);
           int cnt=0;
           for(i=0;i<n;i++)
               for(j=i+1;j<n;j++)
               {
                   if(a[i]>a[j]) cnt++;
               }
           // printf("cnt=%d\n",cnt);
           if(ans>cnt)  ans=cnt;
           for(i=0;i<n;i++)
           {
               cnt=cnt-a[i]+n-1-a[i];
               if(ans>cnt)  ans=cnt;
            }
           printf("%d\n",ans);
    }
    return 0;
}


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

#include<stdio.h>
int a[10000];
struct haha
{
    int left;
    int right;
    int num;
}node[10000*4];
void build(int left,int right,int nd)
{
    node[nd].left=left;
    node[nd].right=right;
    node[nd].num=0;
    if(left==right) 
    {
        return ;
    }
    int mid=(left+right)/2;
    build(left,mid,nd*2);
    build(mid+1,right,nd*2+1);
}
int query(int left,int right,int nd)
{
    int mid=(node[nd].left+node[nd].right)/2;
    if(node[nd].left==left&&node[nd].right==right)
    {
        return node[nd].num;
    }

    if(right<=mid)
    {
          return query(left,right,nd*2);
    }
    else if(left>mid)
    {
        return query(left,right,nd*2+1);
    }
    else
    {
        return query(left,mid,nd*2)+query(mid+1,right,nd*2+1);
    }
}
void update(int pos,int nd)
{
     
    if(node[nd].left==node[nd].right) {node[nd].num++;return ;}
    
    int mid=(node[nd].left+node[nd].right)/2;
    if(pos<=mid)  update(pos,nd*2);
    else update(pos,nd*2+1);
    node[nd].num=node[nd*2].num+node[nd*2+1].num;
}
int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
          for(i=0;i<n;i++)
              scanf("%d",&a[i]);
          build(0,n-1,1);
          int sum=0;
          for(i=0;i<n;i++)
          {
              //printf("i=%d  sum=%d\n",i,sum);
              sum+=query(a[i],n-1,1);
             // printf(">>>");
              update(a[i],1);
          }
         // printf("%d\n",sum);
          int ans=99999999;
          if(ans>sum)  ans=sum;
           for(i=0;i<n;i++)
           {
               sum=sum-a[i]+n-1-a[i];
               if(ans>sum) ans=sum;
           }

               printf("%d\n",ans);
    }
    return 0;
}


下面是归并排序方法:

套用归并排序模板

#include<stdio.h>
#include<malloc.h>
int ans,a[5050],b[5050];
void merge(int left,int mid,int right)
{
    int i,j,cnt=0;
    int *p;
    p=(int *)malloc((right-left+1)*sizeof(int));
    i=left;
    j=mid+1;
    while(i<=mid&&j<=right)//这时候i 和 j 指向的部分都排序完毕了 现在合并
    {
        if(a[i]<=a[j])
        {
            p[cnt++]=a[i];
            i++;
        }
        else
        {
            p[cnt++]=a[j];
            j++;
            ans+=mid-i+1;//第i个比j大 由于i已经从小到大排过序了 那么i+1到mid的也会比j大
        }
    }
    while(i<=mid)
    {
        p[cnt++]=a[i++];
    }
    while(j<=right)
    {
        p[cnt++]=a[j++];
    }
    cnt=0;
    for(i=left;i<=right;i++)
        a[i]=p[cnt++];
    free(p);

}
void merge_sort(int left,int right)
{
    if(left<right) //长度大于1  这是个判断不是循环
    {
        int mid;
        mid=(left+right)/2;
        merge_sort(left,mid);
        merge_sort(mid+1,right);
        merge(left,mid,right);
    }
}

int main()
{
    int n,i,j ;
    while(scanf("%d",&n)!=EOF)
    {

    for(i=0;i<n;i++) {scanf("%d",&a[i]);b[i]=a[i];}
        ans=0;
        merge_sort(0,n-1);
        //printf("ans=%d\n",ans);
        int cnt=999999999;
        if(cnt>ans) cnt=ans;

           for(i=0;i<n;i++)
           {
               //printf("a[i]=%d\n",a[i]);
               ans=ans-b[i]+n-1-b[i];
               if(cnt>ans)  cnt=ans;
            }
           printf("%d\n",cnt);
    }
    return 0;
}


 

 

 


 
  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值