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 |
求大牛帮忙看一下,wa了无数次了#include <stdio.h> #include <stdlib.h> __int64 count = 0; void merge(__int64 *A,long p,long mid,long q) { __int64* t1 = (__int64*)malloc(sizeof(__int64)*(mid-p+1)); __int64* t2 = (__int64*)malloc(sizeof(__int64)*(q-mid)); long i,j,k; for(i = 0;i<mid-p+1;i++) t1[i] = A[p+i]; for(i = 0;i<q-mid;i++) t2[i] = A[mid+1+i]; i=0;j=0; for(k=p;k<=q;k++) { if(i<mid-p+1 && j<q-mid) { if(t1[i]<t2[j] ) { A[k] = t1[i++]; } else { A[k] = t2[j++]; count++; } continue; } if(i<mid-p+1) { count += (mid-p-i)*j ; for(;k<=q;k++) A[k] = t1[i++]; break;; } if(j<q-mid) { A[k] = t2[j++]; continue; } } free(t1); free(t2); } void mergesort(__int64* A,long p,long q) { long mid; if(p<q) { mid = (q+p)/2; mergesort(A,p,mid); mergesort(A,mid+1,q); merge(A,p,mid,q); } } int main() { long n,i; while(1) { scanf("%ld",&n); if(n!=0) { __int64* A = (__int64*)malloc(sizeof(__int64)*n); i = 0; while(i<n) { scanf("%ld",&A[i++]); } count = 0; mergesort(A,0,n-1); printf("%I64d\n",count); free(A); } else break; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator