洛谷 P1923:【深基9.例4】求第 k 小的数 ← 快读

【题目来源】
https://www.luogu.com.cn/problem/P1923

【题目描述】
输入 n(1≤n<
5000000 且 n 为奇数)个数字 ai(1≤ai<10^9),输出这些数字的第 k 小的数。
最小的数是第 0 小。
请尽量不要使用 nth_element 来写本题,因为本题的重点在于练习分治算法。

【输入格式】


【输出格式】


【输入样例】
5 1
4 3 2 1 5

【输出样例】
2

【算法分析】
● 算法竞赛‌中,处理 ‌
1e6 及以上‌个整数输入时,使用“快读”可显著减少输入时间,避免超时。

● 快读函数:
https://blog.csdn.net/hnjzsyjyj/article/details/120131534

int read() { //fast read
    int x=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') { //!isdigit(c)
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9') { //isdigit(c)
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}

利用“快读”函数读入整数的代码:int x=read();

【算法代码】

#include 
using namespace std;

const int maxn=5e6+5;
int a[maxn];

int read() { //fast read
    int x=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') { //!isdigit(c)
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9') { //isdigit(c)
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}

int main() {
    int n=read();
    int k=read();
    for(int i=0; i



【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/146331059
https://www.acwing.com/solution/content/272133/
https://www.acwing.com/solution/content/272132/
https://www.acwing.com/solution/content/272131/
 

你可能感兴趣的:(信息学竞赛,#,STL标准库,数据结构,快读)