跳到正文

题解

P1138题解

WZR_cantAKCSP约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年2月26日
更新
2026年2月26日
篇幅
约 2 分钟
题目
P1138

版本

  1. v1当前

一看到排序、去重

本蒟蒻想到了STL中的set(bushi)

ta能自动排序、去重

面对这题似乎很有用

所以:

C++
#include <bits/stdc++.h>
using namespace std;
int n,k,x;
set<int> st;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++){
		cin>>x;
		st.insert(x);
	}
	if(st.size()<k)cout<<"NO RESULT";
	else cout<<st[k-1];
    return 0;
}

(???)好像set不能用st[k-1]?

那就遍历一下:时间O(n+k)O(n+k)

C++
#include <bits/stdc++.h>
using namespace std;
int n,k,x;
set<int> st;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>k;
    for(int i=0;i<n;i++){
		cin>>x;
		st.insert(x);
	}
	if(st.size()<k)cout<<"NO RESULT";
	else{
		auto t=st.begin();
		for(int i=1;i<k;i++){
			t++;
		}
		cout<<*t;
	}
    return 0;
}
/*
STL set不能用下标太可惜了
*/

最后AC!

原文