POJ 2388(快速)

1078 ワード

テーマリンク:http://poj.org/problem?id=2388
実はこの問題は水の問題で、sortは通ることができますが、練習のために、快速的な並べ替えを書きました。
そして.なんとタイムアウトしました。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=10010;
int n;
int a[maxn];

void Quick_Sort(int *a,int left,int right){
	if(left<right){
		int low=left;
		int high=right;
		int key=a[left];
		while(low<high){
			while(low<high&&a[high]>=key)
				high--;
			a[low]=a[high];
			while(low<high&&a[low]<=key)
				low++;
			a[high]=a[low];
		}
		a[low]=key;
		Quick_Sort(a,left,low-1);
		Quick_Sort(a,low+1,right);
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);
#endif
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		Quick_Sort(a,0,n-1);
		printf("%d
",a[(n-1)/2]); } return 0; }