您的位置:首页 > 其它

BZOJ1753 [Usaco2005 qua]Who's in the Middle

2014-11-08 19:41 447 查看
直接快排没意思。。。

于是想写写"快速选择算法"。。。结果啊。。。

调了1h。。。欧我去。。。至于么,已经蒟蒻到这种程度了?

反正我是记住你了快速选择。。。!

/**************************************************************
Problem: 1753
User: rausen
Language: C++
Result: Accepted
Time:8 ms
Memory:844 kb
****************************************************************/

#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 10005;
int n, a
;

inline int read(){
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9')
ch = getchar();
while (ch >= '0' && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}

int find(int l, int r, int rank){
int i = l, j = r + 1;
int x = a[l];
while (1){
while (a[++i] < x);
while (x < a[--j]);
if (i > j) break;
swap(a[i], a[j]);
}
swap(a[l], a[j]);
if (j - l + 1 == rank) return a[j];
else if (j - l + 1 > rank) return find(l, j - 1, rank);
else return find(j + 1, r, rank - (j - l + 1));
}

int main(){
n = read();
int i;
for (i = 1; i <= n; ++i)
a[i] = read();
printf("%d\r\n", find(1, n, (n + 1) >> 1));
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: