| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
请教一下,MergeSort求逆序数,没看出来为什么WA了,麻烦大家指导下,谢谢诸位!//--------------2299 Ultra QuickSort
#include<stdio.h>
#define MAX 500001
_int64 MergeSort(long a[],long c[],int left,int right)
{
int i,j,mid,temp,count;
_int64 total;
total=0;
if (right>=left+1) {
mid=(left+right)/2;
total+=MergeSort(a,c,left,mid);
total+=MergeSort(a,c,mid+1,right);
count=0;
for (i=left,j=mid+1;i<=mid && j<=right;)
{
if (a[i]>a[j]) {
total+=mid-i+1;
count++;
c[count]=a[j];
j++;
}
else {
count++;
c[count]=a[i];
i++;
}
}
while (i<=mid) {
count++;
c[count]=a[i];
i++;
}
while (j<=right) {
count++;
c[count]=a[j];
j++;
}
for (i=left;i<=right;i++)
a[i]=c[i-left+1];
}
return total;
}
int main(void)
{
long a[MAX],c[MAX];
int n,i,j;
_int64 total;
scanf("%d",&n);
while (n!=0)
{
for (i=1;i<=n;i++)
scanf("%ld",&a[i]);
total=MergeSort(a,c,1,n);
printf("%d\n",total);
/* for (i=1;i<=n;i++) printf("%d ",a[i]);
printf("\n"); */
scanf("%I64d",&n);
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator