알고리즘/BAEKJOON

백준 2841번 외계인의 기타 연주

pureworld 2018. 12. 17. 11:12
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
#include <iostream>
#include <stack>
#include <string>
using namespace std;
 
int N, P;
int num;
stack<int> p[10];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    
    cin >> N >> P;
    
    int a, n;
    for (int i = 0; i < N; i++) {
        cin >> a >> n;
        if (p[a].empty()) {
            num++;
            p[a].push(n);
        }
        if (p[a].top() < n) {
            num++;
            p[a].push(n);
        }
        if (p[a].top() > n) {
            while (p[a].top() > n) {
                p[a].pop();
                num++;
                if (p[a].empty()) break;
            }
            if (p[a].empty()) {
                num++;
                p[a].push(n);
                continue;
            }
            if (p[a].top()==n) continue;
            num++;
            p[a].push(n);
        }
        
    }
    cout << num;
 
}
cs

문제 출처:https://www.acmicpc.net/problem/2841

문제 질문,피드백 댓글로 부탁드려요~!