[cpp]
#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
[html]
#include<stdio.h>
#include<malloc.h>
int 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;
}