P1138题解

· · 题解

一看到排序、去重

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

ta能自动排序、去重

面对这题似乎很有用

所以:

#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)

#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!