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 |
为何超时?//利用快排折半查找,理论上比快排快不少. //算法复杂度为0(2*n) #include <algorithm> #include <time.h> using namespace std; typedef unsigned int uint; uint DC[250003]; int midpos; void swap(int i, int j) { uint temp = DC[i]; DC[i] = DC[j]; DC[j] = temp; } uint quickFind(int left, int right) { if (left + 16 > right) { std::sort(DC + left, DC + right + 1); return DC[midpos]; } int L = left + 1, R = right; swap(left, rand() % (R - L + 2) + left); uint pivot = DC[left]; while ( true ) { while (L < right && DC[L] <= pivot) L++; while (DC[R] > pivot) R--; if (L >= R) break; swap(L++, R--); } if (R > left) swap(left, R); if (R == midpos) return DC[R]; else if (R > midpos) return quickFind(left, R - 1); else return quickFind(R + 1, right); } int main( ) { int n; scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%u",&DC[i]); midpos = n / 2 + 1; srand(time(0)); uint mid2 = quickFind(1,n); if (n % 2 == 1) printf("%.1lf\n",(double)mid2); else { uint mid1 = quickFind(1, midpos--); /*uint mid1 = DC[1]; for(int i = 1; i < midpos; i++) { if ( DC[i] <= mid2 && DC[i] >= mid1 ) mid1 = DC[i]; }*/ printf("%.1lf\n",(mid1 + mid2)/2.0); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator