구현(Implementation)

Codeforces Round #486 (Div. 3) - A. Diverse Team

cepiloth 2018. 6. 19. 11:21
반응형

http://codeforces.com/contest/988/problem/A


1. 문제

A. Diverse Team
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students (1kn) 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 k distinct numbers which should be the indices of students in the team you form. If there are multiple answers, print any of them.


2. 알고리즘

정수를 입력을 받아 SET 자료형에 넣은다.

SET 에 이미 있는 정수면 삽입하지 않는다.


3. 코드 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <set>
using namespace std;
 
int main() {
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    int n, k;
    cin >> n >> k;
    set<int> el;
    vector<int> ans;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (!el.count(x)) {
            ans.push_back(i);
            el.insert(x);
        }
    }
 
    if (int(ans.size()) < k) {
        cout << "NO\n";
    } else {
        cout << "YES\n";
        for (int i = 0; i < k; ++i)
            cout << ans[i] + 1 << " ";
        cout << endl;
    }
 
    return 0;
}
cs

반응형