加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

求第K大数[STL:nth_element]

发布时间:2020-12-14 03:02:33 所属栏目:大数据 来源:网络整理
导读:输入n,k,求第k大 5 2 5 4 1 3 1 结果:4 #includecstdio#includecmath#includecstring#includeset#includestack#includequeue#includevector#includeiostream#includealgorithmusing namespace std;#define ll long long#define INF 0x7FFFFFFF#define INT_M
输入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,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("%dn",arr[n-k]);
    }
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读