Algorithm/C++ - BOJ

BOJ/백준 - 7785 회사에 있는 사람 C++

ㅇㅇ잉 2021. 1. 31. 20:25

원래 vector로 풀었었는데, 시간 초과 나길래 다른 코드 참조하면서 푼 문제.

set컨테이너를 몰랐었다.

이제 배웠으니까 앞으로는 비슷한 문제 안틀려야지

 

set에 원소를 추가하거나 지우는 작업은 O(log N)이다.

사전 역순으로 출력하기 위해, rbegin()과 rend()를 사용하면 된다.

 

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
#include <string>
#include <set>
#include <iostream>
 
using namespace std;
 
int main() {
    set <string> s;
    int N;
 
    cin >> N;
 
    while(N--){
        string tmp1;
        string tmp2;
 
        cin >> tmp1 >> tmp2;
 
        if (tmp2 == "enter") {
           s.insert(tmp1);
        }
        else {
           s.erase(tmp1);
        }
    }
 
    for (auto it = s.rbegin(); it != s.rend(); it++) {
        cout << *it << '\n';
    }
 
 
    return 0;
}
cs