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 |
megersort居然错?#include <iostream> using namespace std; const int MAX = 500001; long long a[MAX],b[MAX]; long long res; void merge(int begin,int mid,int end) { int i,j,k = 0; for(i = begin,j = mid+1; i <= mid && j <= end; k++){ if(a[i] <= a[j]) b[k] = a[i++]; else { b[k] = a[j++]; res += mid+1-i; } } for(; i <= mid; i++) b[k++] = a[i]; for(; j <= end; j++) b[k++] = a[j]; for(i = 0; i < k; i++) a[begin+i] = b[i]; } void msort(int s, int t) { if(s < t){ int m = (s+t)>>1; msort(s,m); msort(m+1,t); merge(s,m,t); } } int main() { int test; int i; while (scanf("%d",&test) && test) { res=0; for (i=0;i<test;i++) { scanf("%lld",&a[i]); } msort(0,test-1); printf("%lld\n",res); } } 想不出来个理由? Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator