인지용

[백준] 2841 외계인의 기타 연주 [python, 실버1, 스택] 본문

알고리즘

[백준] 2841 외계인의 기타 연주 [python, 실버1, 스택]

인지용 2026. 1. 16. 15:35

 

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

 

import sys

def input():
    return sys.stdin.readline().strip()

arr = list(map(int, input().split()))
stack = [[] for _ in range(7)]
count = 0

for i in range(arr[0]):

    temp = list(map(int, input().split()))
    K = temp[0]
    P = temp[1]

    if not stack[K] or stack[K][-1] < P:
        stack[K].append(P)
        count += 1
    else:
        while True:
            if not stack[K] or stack[K][-1] < P:
                stack[K].append(P)
                count += 1
                break
            elif stack[K][-1] == P:
                break
            else:
                stack[K].pop()
                count += 1

print(count)

 

 


 

같은 플랫이어도 이동해야 한다면 손가락을 떼고 이동하는 식으로 구현하면 된다.

 

같은 위치라면 그대로 유지, 이동해야하는 음이 현재 플랫의 맨 마지막보다 작다면

 

하나씩 손가락을 떼주면 된다. (이것도 카운트)