알고리즘/백준

백준 2841 외계인의 기타 연주

등반 2021. 12. 26. 00:05

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

 

2841번: 외계인의 기타 연주

첫째 줄에 멜로디에 포함되어 있는 음의 수 N과 한 줄에 있는 프렛의 수 P가 주어진다. (N ≤ 500,000, 2 ≤ P ≤ 300,000) 다음 N개 줄에는 멜로디의 한 음을 나타내는 두 정수가 주어진다. 첫 번째 정수

www.acmicpc.net

복수의 스택을 이용한 문제다.

특정 스택의 top과 원하는 프렛을 비교하면 된다.

 

#include <iostream>
#define fio cin.tie(0)->sync_with_stdio(0)
using namespace std;

#include <vector>
#include <stack>
#include <string>

stack<int> stack_[7];//1번 줄 to 6번줄
int main(){
    fio;
    int N, P; cin >> N >> P;
    int cnt = 0;
    for(int i=0; i < N; i++){
        int h, f; cin >> h >> f;
        if(stack_[h].empty()){
            stack_[h].push(f);
            cnt++;
        }else{
            while(!stack_[h].empty() && stack_[h].top() > f){
                stack_[h].pop();
                cnt++;
            }
            if(stack_[h].empty()){
                stack_[h].push(f);
                cnt++;
            }else if(stack_[h].top() == f){
                //통과
            }else{
                stack_[h].push(f);
                cnt++;
            }
        }
    }
    cout << cnt;
    return 0;
}

'알고리즘 > 백준' 카테고리의 다른 글

백준 5397 키로거  (0) 2021.12.26
백준 3111 검열  (0) 2021.12.26
백준 5964 Best Parenthesis  (0) 2021.12.26
백준 4889 안정적인 문자열  (0) 2021.12.25
백준 2504 괄호의 값  (0) 2021.12.25