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 |
不知为啥,同样的代码提交出现了tle。好几次啊,后来啥都没改居然就过了。还391ms,代码如下,有兴趣的可以看看什么原因,难道我的人品出问题啦?#include<iostream> #include <stdlib.h> #include<cstdio> using namespace std; #define mmax 500001 int a[mmax]; int t[mmax]; __int64 sum ; void merge(int s,int mid ,int e) { int p = s,q = mid + 1; int mm = 0; while(p <= mid && q <= e) { if(a[p] > a[q]) { t[mm++] = a[q++]; sum += mid - p + 1; } else t[mm++] = a[p++]; } while(p <= mid) t[mm++] = a[p++]; while(q <= e) t[mm++] = a[q++]; for(int i = 0;i < mm;i++) a[s+i] = t[i]; } void mergeSort(int s,int e) { if(s < e){ int mid = (s+e)/2; mergeSort(s,mid); mergeSort(mid+1,e); merge(s,mid,e); } } int main() { int i; int num; while(1) { scanf("%d",&num); if(!num) break; sum = 0; for(i = 0;i < num;i++) scanf("%d",&a[i]); mergeSort(0,num-1); printf("%I64d\n",sum); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator