归并排序 例题即模版 XMU1328 hdu3743

#include <stdio.h>  
#include <stdlib.h>  
#define MAXN 1000100  
int input[MAXN] = {0};  
int tmp[MAXN];   
void merge(int left, int middle, int right)  
{  
     int i, j, k;  
     i = left, j= middle+1, k = 1;  
     while(i<= middle && j <= right)  
     {  
               if(input[j] < input[i])  
               {  
                    tmp[k++] = input[j++];  
               }  
               else  
        {  
                    tmp[k++] = input[i++];  
        }  
     }  
     while(i <= middle)    
    tmp[k++] = input[i++];  
     while(j <= right)   
    tmp[k++] = input[j++];       
     for(i = left, k = 1; i<= right; i++, k++)  
            input[i] = tmp[k];  
}  
void merge_sort(int left, int right)  
{  
     if(left < right)  
     {  
             int middle = (left + right)/2;  
             merge_sort(left, middle);  
             merge_sort(middle+1, right);  
             merge(left, middle, right);  
     }  
}  
int main(){  
    int n,i;  
	while( scanf("%d",&n)&&n)
	{
    for(i=0;i<n;++i)  
        scanf("%d",&input[i]);  
    merge_sort(0,n-1);  
    for(i=0;i<n-1;++i)  
		printf("%d ",input[i]);
	printf("%d\n",input[i]);
	}
    return 0;  
}   


上面是模版

只要看下代码 就能理解是如何实现的了

 

下面是一个应用 XMU1328

1328.不高兴的bobo
Time Limit: 2000 MS         Memory Limit: 65536 K
Total Submissions: 589 (85 users)         Accepted: 55 (42 users)

Description

Bobo手下有n个小弟,身高分别是a1,a2……an,bobo看他们高矮不一,于是很不高兴,定义有两个小弟不按高矮站时,bobo的不高兴值加1(即(i,j),i<j,ai>aj),问bobo的总不高兴值为多少。

Input

一个整数n,表示bobo有n个小弟。

以下有n个整数ai,代表小弟的身高。

N<=10^6,ai<2^32

Output

一个整数为bobo的不高兴值。

Sample Input

5

5 4 3 2 1

Sample Output

10

 

#include <stdio.h>  
#include <stdlib.h>  
#define MAXN 1000100  
int input[MAXN] = {0};  
int tmp[MAXN];  
long long result;  
void merge(int left, int middle, int right)  
{  
     int i, j, k;  
     i = left, j= middle+1, k = 1;  
     while(i<= middle && j <= right)  
     {  
               if(input[j] < input[i])  
               {  
                    tmp[k++] = input[j++];  
                    result += middle - i + 1;     
               }  
               else  
        {  
                    tmp[k++] = input[i++];  
        }  
     }  
       
     while(i <= middle)    
    tmp[k++] = input[i++];  
     while(j <= right)   
    tmp[k++] = input[j++];       
     for(i = left, k = 1; i<= right; i++, k++)  
            input[i] = tmp[k];  
}  
void merge_sort(int left, int right)  
{  
     if(left < right)  
     {  
             int middle = (left + right)/2;  
             merge_sort(left, middle);  
             merge_sort(middle+1, right);  
             merge(left, middle, right);  
     }  
}  
int main(){  
    int n,i;  
    scanf("%d",&n);  
    for(i=0;i<n;++i)  
        scanf("%d",&input[i]);  
    result=0;  
    merge_sort(0,n-1);  
    printf("%lld\n",result);  
    return 0;  
}  

 

下面这个题和上面是一抹一样的

Frosh Week

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1027    Accepted Submission(s): 319


Problem Description
During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.
 


Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No student number will appear more than once.
 


Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number.
 


Sample Input
   
   
   
   
3 3 1 2
 


Sample Output
   
   
   
   
2
 


Source

 

#include<stdio.h>
#include<malloc.h>
__int64 ans;/////////////// 精度
__int64 a[1000000+50];
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;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        merge_sort(0,n-1);
        printf("%I64d\n",ans);//////////
    }
    return 0;
}
 


你可能感兴趣的:(归并排序 例题即模版 XMU1328 hdu3743)