练习cf988A. Diverse Team

题目如下
https://codeforces.com/problemset/problem/988/A
A. Diverse Team
time limit per test1 second
memory limit per test256 megabytes
There are 𝑛 students in a school class, the rating of the 𝑖-th student on Codehorses is 𝑎𝑖. You have to form a team consisting of 𝑘 students (1≤𝑘≤𝑛) such that the ratings of all team members are distinct.

If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print 𝑘 distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.

Input
The first line contains two integers 𝑛 and 𝑘 (1≤𝑘≤𝑛≤100) — the number of students and the size of the team you have to form.

The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤100), where 𝑎𝑖 is the rating of 𝑖-th student.

Output
If it is impossible to form a suitable team, print "NO" (without quotes). Otherwise print "YES", and then print 𝑘 distinct integers from 1 to 𝑛 which should be the indices of students in the team you form. All the ratings of the students in the team should be distinct. You may print the indices in any order. If there are multiple answers, print any of them.

Assume that the students are numbered from 1 to 𝑛.

题目大意
现有n名学生,每名学生的等级各不相同,现在需要找出m名等级不同的学生组成新的队伍,如果有m名等级各不相同的学生,那么可以组成新的队伍输出“YES”并输出他们的编号;否则,输出“NO”。

题目分析
先确认原队伍是否有m名成绩不同的学生,使用map,存储每个等级出现的第一名学生即可,再根据这个等级的数量判断是否能组成新的队伍。
代码如下

点击查看代码
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    map<int, int> a;
    for(int i = 1; i <= n; i++){
        int num;
        cin >> num;
        if(a.count(num) == 0){
            a[num] = i;
        }
    }
    if(a.size() < m){
        cout << "NO" << endl;
        return 0;
    }else{
        cout << "YES" << endl;
        int count = 0;
        for(auto it = a.begin(); it != a.end() && count < m; ++it, ++count){
            cout << it->second << " ";
        }
    }
    return 0;
}

posted @ 2025-08-08 21:26  sirro1uta  阅读(34)  评论(0)    收藏  举报