AIMEE11 @ 2022-11-07 22:37:19
#include<iostream>
using namespace std;
const int N=500000;
int main()
{
int n,a[N],k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]>a[j])
{
int t=a[j];
a[i]=a[j];
a[j]=t;
}
for(int i=1;i<=n;i++)
if((k+1)==i) cout<<a[i];
return 0;
}
by Zhangyanlin @ 2022-11-11 23:01:13
@AIMEE11 用sort+scanf/printf+O2优化即可 献上AC代码
#include<bits/stdc++.h>
using namespace std;
int m,k;
int a[5000000];
int main(){
scanf("%d%d",&m,&k);
for(int i=0;i<m;i++){
scanf("%d",&a[i]);
}
sort(a,a+m);
printf("%d",a[k]);
return 0;
}
记得要开O2!!!要不然会TLE
by Zhangyanlin @ 2022-11-11 23:04:12
这道题主要是cin和cout会严重超时,scanf和printf会相对快一些
by AIMEE11 @ 2022-11-12 08:53:06
@Zhangyanlin 谢谢了