求第K大数[STL:nth_element]

来源:互联网 发布:如何彻底卸载软件 编辑:程序博客网 时间:2024/06/08 06:00
输入n,k,求第k大
5 2

5 4 1 3 1

结果:4


#include<cstdio>#include<cmath>#include<cstring>#include<set>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>using namespace std;#define ll long long#define INF 0x7FFFFFFF#define INT_MIN -(1<<31)#define eps 10^(-6)#define Q_CIN ios::sync_with_stdio(false)#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define REV( i , n ) for ( int i = n - 1 ; i >= 0 ; -- i )#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define CLR( a , x ) memset ( a , x , sizeof (a) )#define RE freopen("in.txt","r",stdin)#define WE freopen("out.txt","w",stdout)#define NMAX 10002#define min(a,b) ((a)>(b)?(b):(a))int read(){    int x=0,f=1;char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}int arr[5000100];int main(){    int n,k;    RE;    while(~scanf("%d%d",&n,&k))    {        REP(i,n)            arr[i] = read();        nth_element(arr,arr+(n-k),arr+n);        printf("%d\n",arr[n-k]);    }    return 0;}


0 0
原创粉丝点击