Partition-based general selectio…

Description
A worst-case linear algorithm for the general case of selecting the kth largest element was published by Blum, Floyd, Pratt, Rivest, and Tarjan in their 1973 paper Time bounds for selection. The algorithm that it is based on was conceived by the inventor of quicksort, C.A.R. Hoare, and are known as Hoare's selection algorithm. In quicksort, there is a subprocedure called partition which can, in linear time, group the list into two parts, those less than a certain value (left), and those greater than or equal to a certain value (right, which ends up containing pivotValue). Your task: selecting the kth largest element in a list of n numbers. (n< = 50000)
Input
two lines the first line have two numbers , the first numbers is total of list and the second numbers is the kth largest element the second line is a list of n numbers
Output
the kth largest element in a list of n numbers

Sample Input

9 5
4 1 10 9 7 12 8 2 15

Sample Output

8
太狠了!!排序遍历wa,数组下标标记就re,最后找到一个好的算法,用到一个伴随数组,可以去看看这个:
寻找给定区间内的第k小(大)的元素
看完后仿佛如梦初醒!!
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
  int num;
  int data;
}p[50001];
bool cmp(node a,node b)
{
 return a.data<b.data;
}
int main()
{
   int n,i,k;
   cin>>n>>k;
   for(i=1;i<=n;i++)
   {
    cin>>p[i].data;
    p[i].num=i;
   }
   sort(p+1,p+n+1,cmp);
   for(i=1;i<=n;i++)
   {
    if(p[i].num>=1&&p[i].num<=n)
     k--;
    if(k==0)
     break;
   }
   cout<<p[i].data<<endl;
}
 
我的更多文章:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值