반응형

https://www.acmicpc.net/problem/21939

 

21939번: 문제 추천 시스템 Version 1

tony9402는 최근 깃헙에 코딩테스트 대비 문제를 직접 뽑아서 "문제 번호, 난이도"로 정리해놨다. 깃헙을 이용하여 공부하시는 분들을 위해 새로운 기능을 추가해보려고 한다. 만들려고 하는 명령

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. set 자료구조를 활용하여 문제의 난이도와 번호를 insert 합니다.

 

2. 문제 번호에 따라 난이도를 알 수 있도록 level 배열을 만들어 저장합니다.

 

3. 가장 어려운 난이도의 문제는 set 자료구조의 제일 마지막에 있고, 가장 쉬운 난이도의 문제는 set 자료구조의 제일 앞에 있으므로 명령문에 따라 출력해 줍니다.

 

[ 소스코드 ]

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<set>
#include<string>
 
using namespace std;
 
int N, M;
int level[100001];
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    set<pair<int,int>> s;
 
    for (int i = 0; i < N; i++) {
        int P, L;
        cin >> P >> L;
 
        level[P] = L;
        s.insert({ L,P });
    }
    
    cin >> M;
 
    for (int i = 0; i < M; i++) {
        string cmd;
        cin >> cmd;
 
        if (cmd == "add") {
            int P, L;
            cin >> P >> L;
 
            level[P] = L;
            s.insert({ L,P });
        }
        else if (cmd == "recommend") {
            int x;
            cin >> x;
 
            if (x == 1) {
                cout << (*s.rbegin()).second << '\n';
            }
            else {
                cout << (*s.begin()).second << '\n';
            }
        }
        else {
            int P;
            cin >> P;
 
            s.erase({ level[P],P });
        }
    }
}
cs
반응형

+ Recent posts