STACK, QUE, HEAP

오성인·2023년 3월 8일
0

알고리즘

목록 보기
4/18
post-custom-banner

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

t = int(input())
for _ in range(t):
    data = input()
    sum = 0
    for i in data:
        if i == '(':
            sum += 1
        if i == ')':
            sum -= 1
        if sum < 0:
            print('NO')
            break
    if sum == 0:
        print('YES')
    elif sum > 0:
        print('NO')

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

n = int(input())
cnt = 0
stack = []
result = []
no_message = True
for i in range(n):
    num = int(input())
    while cnt < num:
        cnt += 1
        stack.append(cnt)
        result.append('+')
    if stack[-1] == num:
        stack.pop()
        result.append('-')
    else:
        no_message = False

if no_message == False:
    print('NO')
else:
    for i in result:
        print(i) 

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


while True:
    string = input()
    if string == ".":
        break
    stk = []
    
    for i in string:
        if i not in '()[]':
            continue
        if i == '(' or i == '[':
            stk.append(i)
        elif (i == ')' and stk and stk[-1] == "(") or (i==']' and stk and stk[-1] == '['):
            stk.pop()
        else:
            stk.append(0)
            break
    if stk:
        print('no')
    else:
        print('yes')

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

from collections import deque
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
position = list(map(int, input().split()))
dq = deque([i for i in range(1, n+1)])
count = 0
for i in position:
    while True:
        if dq[0] == i:
            dq.popleft()
            break
        else:
            if dq.index(i) < len(dq)/2:
                while dq[0] != i:
                    dq.append(dq.popleft())
                    count += 1
            else:
                while dq[0] != i:
                    dq.appendleft(dq.pop())
                    count += 1

print(count)

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

import heapq, sys

input = sys.stdin.readline

numbers = int(input())
heap = []

for _ in range(numbers):
    num = int(input())
    if num != 0:
        heapq.heappush(heap, (-num))
    else:
        try:
            print(-1 * heapq.heappop(heap))
        except:
            print(0)
profile
기여하는 개발자
post-custom-banner

0개의 댓글