unique
unique 是 c++ 标准函数库之一 ,需要配合头文件 #include<algorithm>
使用
作用:‘去掉’容器中相邻元素的重复元素 ,伪去除,相当于将重复元素添加到容器末尾
使用前提: 容器内元素必须有序
结果:返回最后一个不重复元素的地址下标
int a[N];
int dis = unique(a,a+n) - a;
//dis 为 去重后最后一个不重复元素的地址下标
//例如 1 2 3 4 4 dis = 4 ; 1 2 3 3 4 4 dis = 4
第 k 小整数
题目描述
现有 \(n\) 个正整数,要求出这 \(n\) 个正整数中的第 \(k\) 个最小整数(相同大小的整数只计算一次)。
输入格式
第一行为 \(n\) 和 \(k\); 第二行开始为 \(n\) 个正整数的值,整数间用空格隔开。
输出格式
第\(k\)个最小整数的值;若无解,则输出 NO RESULT
。
样例 #1
样例输入 #1
10 3
1 3 3 7 2 5 1 2 4 6
样例输出 #1
3
提示
\(n \leq 10000\),\(k \leq 1000\),正整数均小于 \(30000\)。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100100;
int a[N];
int n,k;
int main()
{
cin>>n>>k;
for(int i = 0; i < n; i ++) cin>>a[i];
sort(a,a + n);
int dis = unique(a,a + n) - a;
if(k < dis) cout<<a[k - 1];
else cout<<"NO RESULT";
return 0;
}
标签:unique,正整数,int,元素,include,dis
From: https://www.cnblogs.com/Elgina/p/17690956.html