Sample Input
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;
}