题解:P1138 第 k 小整数
本篇题解记载了排序加去重的方法。
马蜂见谅
思路
可以将原数组去重、排序后cout<<b[k]。
具体细节见代码,包含注释。
去重采用类似桶排的思想。
代码
#include<bit/stdc++.h>
using namespace std;
using ll=long long;
const int N=10010;//注意数据范围
int t[N],g[N],b[N],n,k,m,i;//t为原数组,g为计数数组,b为t去重排序后的数组
signed main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>k;
for(i=1;i<=n;i++) cin>>t[i];//输入
for(i=1;i<=n;i++)
if(g[t[i]]==0)
g[t[i]]++,/*等价于g[t[i]]=1;*/b[m++]=t[i];//去重
sort(b+1,b+m+1);//排序
if(k>m) retvrn cout<<"NO RESULT",0;//m即为b数组长度
else cout<<b[k];//b[k]即为所求
}