记录编号 449451 评测结果 AAAAAAAAAA
题目名称 爱摔跤的比利海灵顿 最终得分 100
用户昵称 GravatarkZime 是否通过 通过
代码语言 C++ 运行时间 0.464 s
提交时间 2017-09-14 14:30:16 内存使用 38.71 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

inline char getc() {
    static char buf[1 << 18], *fs, *ft;
    return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}

inline int gn() {
    int k = 0, f = 1;
    char c = getc();
    for (; !isdigit(c); c = getc()) if (c == '-') f = -1;
    for (; isdigit(c); c = getc()) k = k * 10 + (c ^ 0x30);
    return k * f;
}

int n, k, s[10000023];

int qsort(int l, int r) {
    if(l == r) return s[l];
    register int tmp, i = l, j = r, M = s[l + rand() % (r - l + 1)];
    do{
        while(s[i] < M) ++i;
        while(s[j] > M) --j;
        if(i <= j) {
            tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;
            ++i, --j;
        }
    }while(i <= j);
    if(k <= j) return qsort(l, j);
    if(i <= k) return qsort(i, r);
    return tmp;
}

int main() { 
#ifndef LOCAL
    freopen("find_k.in", "r", stdin);
    freopen("find_k.out", "w", stdout);
#endif
    n = gn(), k = gn() - 1;
    srand(n);
    for (int i = 0; i < n; i++)
        s[i] = gn();
    cout << qsort(0, n - 1);
}