โ 1874 ์คํ ์์ด โ
import sys
input = sys.stdin.readline
stack,sequence,ans = [],[],[]
cur = 0
for _ in range(int(input())):
sequence.append(int(input()))
sequence = sequence[::-1]
while len(sequence) != 0:
popped = sequence.pop()
if popped > cur:
for i in range(cur+1,popped+1):
stack.append(i)
ans.append('+')
cur = popped
if popped == stack[-1]:
stack.pop()
ans.append('-')
else:
ans = []
break
if len(ans) != 0:
for i in ans:
print(i)
else:
print('NO')
๐ซถ ์ฃผ์ด์ง stack์ ์ ๋ ฅํ ์์ ์์๋๋ก ์์ด์ ๋ง๋ค ์ ์๋ ์ง ๋ฌป๋ ๋ฌธ์ . 4๊ฐ ๋จผ์ ๋ค์ด๊ฐ ์์ผ๋ฏ๋ก 1 - 2 - 3 - 4 ์ฐจ๋ก๋๋ก push. (pushํ๋ ค๋ฉด ๋ฐ๋์ ์ค๋ฆ์ฐจ์์ ์ง์ผ์ผ ํ๋ค) ๊ทธ ๋ค์ push๋ stack์์ popํ๋ฉด ํด๋น pop๋ ์์(popped)๊ฐ ์์ด์ ์์. ์ 1-2 ๊ณผ์ ์ ๋ฐ๋ณตํ๋, pop๋ ๊ฒฐ๊ณผ๊ฐ ์ํ๊ณ ์ ํ๋ ์์ด์ ์์๋๋ก ๋์ค์ง ์๋๋ค๋ฉด, ์์ด์ ๋ง๋ค ์ ์๊ณ NO ์ถ๋ ฅ. ๋ฌธ์ ์์ ์ฃผ์ด์ง ๋๋ก push / pop ๊ตฌํ๋ง ์ํ๋ฉด ๋จ.
โ 4889 ์์ ์ ์ธ ๋ฌธ์์ด โ
import sys
input=sys.stdin.readline
num=0
while 1:
num+=1
left,right=0,0
parentheses=input().strip()
stack=[]
if '-' in parentheses:break
#stack operation
for p in parentheses:
if len(stack)!=0:
if p=='}' and stack[-1]=='{':stack.pop()
else:stack.append(p)
else: stack.append(p)
if len(stack)==0:print(f'{num}. 0')
else:
left=stack.count('{')
right=stack.count('}')
if left==0:print(f'{num}. {(right//2)}')
elif right==0:print(f'{num}. {(left//2)}')
else:
if left%2==1:print(f'{num}. {(left//2 + right//2 + 2)}')
else:print(f'{num}. {(left//2 + right//2)}')
๐ซถ ์์ ์ ์ธ ๋ฌธ์์ด์์ ํ์ธํ ๋ stack์ ์ ํ์ฉํ๋ ์ ํ์ ์ธ stack ๋ฌธ์ ์ ํ. 'pair๋ฅผ ํ์ฑํ๋์ง ํ์ธ - pair๊ฐ์ ๊ฒฐํฉ ๊ฒฐ๊ณผ ์ํ๋ ๋๋ก ํ์ฑ๋๋ ์ง ํ์ธ' ๋๋ก ๊ทธ๋๋ก stack์ผ๋ก ๊ฒ์ฌํด์ฃผ๋ฉด ๋๋ค. ์ถ๊ฐ๋ก, ๋ง์ฝ stack์ ์๊ดํธ๊ฐ ๋จ์์ ๊ฒฝ์ฐ, ๋ฌด์กฐ๊ฑด ์ง์ ๊ฐ์๋ก ๋จ๊ฒ ๋๊ณ , { ๋ } ํ ์ข ๋ฅ๋ง ๋จ์๋ค๋ฉด 2๋ก ๋๋ ๋ชซ์ด ์ต์ ๋ฐ๊พธ๋ ํ์ + {์ }๊ฐ ๊ฐ์ด ํผํฉ๋์ด ์๋ค๋ฉด, ๊ฐ๊ฐ ํ์ ๊ฐ์์ผ ๊ฒฝ์ฐ ๊ฐ๊ฐ์ 2๋ก ๋๋ ๋ชซ์ ์๋ก ๋ํ๊ณ , }์ {๊ฐ ๋จ์ผ๋ฏ๋ก ๊ฐ๊ฐ {์ }๋ก ๋ฐ๊ฟ์ฃผ๋ ์ฐ์ฐ ํ์ 2๋ฅผ ๋ํด์ฃผ๋ฉด ๋๋ค / ๊ฐ๊ฐ ์ง์ ๊ฐ์์ผ ๊ฒฝ์ฐ, ๊ฐ๊ฐ์ 2๋ก ๋๋ ๋ชซ๋ง ๋ํด์ฃผ๋ฉด ๋๋ค.(case์ ๋ฐ๋ผ ์ต์ ์ฐ์ฐ์ ์ ๊ณ ๋ ค)
โ 1406 ์๋ํฐ โ
import sys
input=sys.stdin.readline
#queue
from collections import deque
x=deque(input().rstrip())
y=deque([])
for _ in range(int(input())):
ord=input().split()
if ord[0] == 'P':
x.append(ord[1])
elif ord[0] == 'L':
if x:
y.appendleft(x.pop())
elif ord[0] == 'D':
if y:
x.append(y.popleft())
else: #B
if x:
x.pop()
print(*(x+y),sep='')
๐ซถ ์ปค์๋ฅผ ์๋ค๋ก ์ฎ๊ธฐ๋ ๋ฌธ์ ๋ฅผ ์ปค์๋ฅผ ๊ณ ์ ํ๊ณ ์ปค์ ์๊ณผ ๋ค์ 2๊ฐ์ deque์ด ์๋ idea๋ก ์๊ฐํ ์ ์๋ ๋ฌธ์ . ์ปค์๋ฅผ ์ผ์ชฝ์ผ๋ก ํ ์นธ ์ฎ๊ธฐ๋ ๊ฑด ์ผ์ชฝ deque์ popํด ์ค๋ฅธ์ชฝ deque์ appendleft. ์ปค์๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ์ฎ๊ธฐ๋ ๊ฑด ์ค๋ฅธ์ชฝ deque์ popleftํด ์ผ์ชฝ deque์ append. ์ปค์ ์ผ์ชฝ์ ์๋ ๋ฌธ์ ์ญ์ ๋ ์ผ์ชฝ deque pop. ์ปค์ ์ผ์ชฝ์ ๋ฌธ์ ์ถ๊ฐ๋ ์ผ์ชฝ deque append. ์ด ๋ pop(), popleft() ๋ deque์ ์์๊ฐ ์๋ ์ง ์๋ ์ง๋ง ์ฌ๋ถ ํ์ธ.
โ 5397 ํค๋ก๊ฑฐ โ
import sys
input=sys.stdin.readline
from collections import deque
for _ in range(int(input())):
string=input().rstrip()
x,y=deque(),deque()
for s in string:
if s == '<':
if x:
y.appendleft(x.pop())
elif s == '>':
if y:
x.append(y.popleft())
elif s == '-':
if x:
x.pop()
else:
x.append(s)
print(*(x+y),sep='')
๐ซถ ์ 1406 ์๋ํฐ idea ๋ฐ์๊ณผ ๋์ผํ ๋ฌธ์ . deque ๋์ stack 2๊ฐ ํ์ฉ๋ ๊ฐ๋ฅ. ๋ค๋ง ๋์ค์ ์ถ๋ ฅ ์ ์ ๊ตฌ/์ถ๊ตฌ ๊ตฌ๋ณํด์ stack reverse ์ฃผ์
โ 26111 Parentheses Tree โ
import sys
input=sys.stdin.readline
parentheses,stack=input(),[]
ans=0
flag=False
for p in parentheses:
if not stack: stack.append(p)
else:
if p=='(':
stack.append(p)
flag=True
else: #')'
stack.pop()
if flag:
ans += len(stack)
flag=False
print(ans)
๐ซถ ( ๋ง๋ ๋ stack์ ๋ฃ๊ณ ) ๋ง๋ ๋ ์ด๋ฏธ ์์ ๋ฐ๋ก (๊ฐ ์์๋ค๋ฉด ํด๋น ( ์์ ๊ณ ํ์ฌ stack์ ๋ค์ด๊ฐ (์ ๊ฐ์ ์นด์ดํธ. ์ ์ ์ ์๋ง ์นด์ดํธํ๋ฏ๋ก ๋ํด์ง๋ ์ซ์๋ ์์ ( ๊ฐ์. ์ด๋ ๊ณง len(stack). ํ์ดํ ์์๋๋ก stack์ append(), pop() ๋ฐ๋ณต
โ 6105 Look Up โ
#monotone stack
import sys
input=sys.stdin.readline
N=int(input())
stack=[]
ans=[0]*(N+1)
for i in range(1,N+1):
n=int(input())
while stack:
if stack[-1][1]<n:
popped = stack.pop()
ans[popped[0]]=i
else:
break
stack.append((i,n))
print(*ans[1:],sep='\n')
๐ซถ ์คํ ๊ธฐ์ค ๊ณ์ ๋ค์ด์ค๋ ์ซ์ ์ค ๊ฐ์ฅ ๋จผ์ ๋๋ณด๋ค ๋์ ์ซ์๋ฅผ ๊ฐ์ง๋ ์์ index๋ฅผ ๋ชจ๋ ์๋ง๋ค ๊ตฌํ๋ ๋ฌธ์ . ์ด๋ ์คํ <๋ชจ๋ ธํค ์คํ> ์ ํ์ฉํด์ ๊ตฌํ ์ ์๋ค. ์ด๋ ์คํ์ ๋ด๋ฆผ์ฐจ์์ ์ ์งํ๋ฉฐ ๋ชจ๋ ธํค ์คํ์ ๋ง๋๋ ๊ณผ์ ์ผ๋ก ์์ฉํ ์ ์์. ์ค๋ฆ์ฐจ์์ด ํ์ธ๋๋ฉด ๋ด๋ฆผ์ฐจ์์ด ๋๊ธฐ ์ while stack๋ฌธ์ ๊ณ์ ๋๋ ค ๋ค์ด์จ cow์ index๋ก pop๋๋ cow์ ans ๋ณ์๋ฅผ update.
โ 25096 Pancake Deque โ
import sys
input=sys.stdin.readline
from collections import deque
for i in range(1,int(input())+1):
N=int(input())
dq=deque(map(int,input().split()))
ans=0
cur=0
for j in range(N):
if j!=(N-1):
x,y=dq.popleft(),dq.pop()
if x<=y:#get x
dq.append(y)
if x>=cur:
cur=x
ans+=1
else:#get y
dq.appendleft(x)
if y>=cur:
cur=y
ans+=1
else:
x=dq.pop()
if x>=cur:
ans+=1
print(f'Case #{i}: {ans}')
๐ซถ ์ผ๋จ ์์ชฝ์์ ๊บผ๋ด๊ณ , ์์ชฝ ์ค deliciousness๊ฐ ์์ ๊ฒ์ ์ ํ(greedy)ํด ์๋น. ํ์ฌ cur๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด ์ ๋ฐ์ดํธํด์ ans ๊ฐ์ += 1. ๋งจ ๋ง์ง๋ง 1๊ฐ ๋จ์์ ๋๋ ๋ฌด์กฐ๊ฑด ๊บผ๋ด์ ํด๋น ๊ฒฐ๊ณผ๋ฅผ ์์ ๋์ผํ๊ฒ cur๊ณผ ๋น๊ต
โ 2346 ํ์ ํฐ๋จ๋ฆฌ๊ธฐ โ
import sys
input=sys.stdin.readline
from collections import deque
N=int(input())
balloons=list(map(int,input().split()))
ans=[]
dq=deque([(i,balloons[i-1]) for i in range(1,N+1)])
for _ in range(N):
balloon = dq.popleft()
ans.append(balloon[0])
num=balloon[1]
if len(dq)>=2:
if num > 0: # +
for _ in range(num-1):
dq.append(dq.popleft())
else: #- (no 0)
for _ in range(abs(num)):
dq.appendleft(dq.pop())
print(*ans)
๐ซถ ์์์ผ ๊ฒฝ์ฐ ์ค๋ฅธ์ชฝ์ผ๋ก ๋๊ณ / ์์์ผ ๊ฒฝ์ฐ ์ผ์ชฝ์ผ๋ก ๋๋, ์๋ฐฉํฅ์ผ๋ก ๋๊ธฐ์ deque ์ฌ์ฉ. ์ด ๋ ํ์ ์ ํฐ๋จ๋ฆฌ๊ณ ๋ ๋ค์์๋ ์๋์ผ๋ก ์ค๋ฅธ์ชฝ์ผ๋ก 1์นธ ์์ง์ธ ํ์ ์ด deque์ ๋งจ ์์ ์์ผ๋ฏ๋ก, ์์์ผ ๊ฒฝ์ฐ num-1๋ฒ ๋๊ณ / ์์์ผ ๊ฒฝ์ฐ abs(num)๋ฒ ๋๋ฆฌ๋ ๊ตฌํ idea๋ฅผ ์๊ฐํด์ผ ํจ. ์ถ๊ฐ๋ก deque์ ํ์ ์ด 1๊ฐ ๋จ์์ ๊ฒฝ์ฐ, ๋จ์ ํ์ ์ ์คํ ๋ฒํธ๋ ํ์ ์์ผ๋ฏ๋ก ๋ฐ๋ก ์ข ๋ฃ.
โ 24511 queuestack โ
import sys
input=sys.stdin.readline
from collections import deque
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
M=int(input())
C=list(map(int,input().split()))
ans=[]
dq=deque()
for a,b in zip(A,B):
if a == 0:
dq.append(b)
for c in C:
dq.appendleft(c)
ans.append(dq.pop())
print(*ans)
๐ซถ stack์ ๋ฃ๊ณ ๋นผ๋ฉด ์๊ธฐ ์์ ์ด ๋ฃ๊ณ ๋น ์ง๋ฏ๋ก ์ํฅ x / queue๋ ๋ด๊ฐ ๋ค์ด๊ฐ๊ณ ์๋ ๋ค์ด๊ฐ ์๋ queue์ ์์๊ฐ ๋์ด. ์ฆ, ์ฌ๋ฌ ๊ฐ์ queue๋ง ์ฐ๊ฒฐ๋ ๊ฑฐ๋ํ deque 1๊ฐ์ ์๋ฃ๊ตฌ์กฐ์์ ์ ์ ์์. queue์ ์์๋ง ๋ชจ์์ ํฐ 1๊ฐ์ deque์ผ๋ก ๋ง๋ค๊ณ , deque์ ์ผ์ชฝ์ ๋ฃ์ผ๋ฉด์ ์ค๋ฅธ์ชฝ์ผ๋ก 1๊ฐ์ฉ ๋นผ๋ฉด ๋! ์ฆ, stack ์ ์ธ queue๋ง ์ผ์ง์ ์ผ๋ก ๋ชจ์์ 1๊ฐ์ deque์ผ๋ก appendleft()์ pop() ์ฐ์ฐ
โ 12789 ๋ํค๋ํค ๊ฐ์๋๋ฆฌ๋ฏธ โ
import sys
input=sys.stdin.readline
N=int(input())
s1=list(map(int,input().split()))
s1=s1[::-1]
order = 1
s2=[]
while s1:
if order == s1[-1]:
s1.pop()
order+=1
if order>N:
print('Nice')
sys.exit()
else:
while s2:
if order == s2[-1]:
s2.pop()
order+=1
if order>N:
print('Nice')
sys.exit()
else:
break
s2.append(s1.pop())
if s2[::-1] == [x for x in range(order,N+1)]:
print('Nice')
else:
print('Sad')
๐ซถ ๋ ๊ฐ์ ์คํ์ ํ์ฉํ๋ ๋ฌธ์ . ์ผ๋จ s1์ ๋นผ๋ฉด์ order ๋ง๋ค๋ฉด ๋ฐ๋ก ๋นผ๊ณ order +=1 / order ํ๋ฆฌ๋ค๋ฉด s2๋ฅผ ๋๋ฆฌ๋ฉด์ order์ ๋ง๋ ์ง ๊ณ์ ํ์ธ / s2๊น์ง ๋ค ๋๋ ธ๋ค๋ฉด s2์ s1.pop() ๋ฃ๊ธฐ / ์์ธ์ ์ผ๋ก s1 ๋ชจ๋ ๋นผ๊ณ ๋ ๋ค s2์ ๋จ์ ์ผ๋ จ๋ฒํธ๊ฐ ์ค ๋ฒํธ์ ๋ง๋ค๋ฉด Nice ์ถ๋ ฅ / ํ stack ๋ด์ while ๋ ๋ค๋ฅธ stack ๋ฌธ ๋๋ฆฌ๊ธฐ ๊ธฐ์ต
โ 15500 ์ด์ํ ํ๋ ธ์ด ํ โ
import sys
input=sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
B=[]
K=0
moves=[]
while N>0:
if A and A[-1] == N:
A.pop()
K+=1
moves.append((1,3))
N-=1
continue
if B and B[-1] == N:
B.pop()
K+=1
moves.append((2,3))
N-=1
continue
if N in A:
while A[-1] != N:
B.append(A.pop())
K+=1
moves.append((1,2))
A.pop()
K+=1
moves.append((1,3))
N-=1
else:
while B[-1] != N:
A.append(B.pop())
K+=1
moves.append((2,1))
B.pop()
K+=1
moves.append((2,3))
N-=1
print(K)
for x in moves:
print(*x)
๐ซถ ๋ฐ๋์ ๋ฌด๊ฒ ์์๋๋ก ํ๋ ธ์ด ํ์ ์์ด์ผ ํ๋ค๋ ๊ท์น์ด ์์ผ๋ฏ๋ก, ๊ทธ์ ๊ฐ์ฅ ๋ฌด๊ฒ๊ฐ ๋ง์ด ๋๊ฐ๋ ์ํ์ ์ฐพ์ ๋๊น์ง pop() + append() ๋ฐ๋ณตํ๋ฉฐ ์๋ฎฌ๋ ์ด์ ์ผ๋ก ์ต์ข ๋ง๋์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.(์ด ๋ list์ ์๋ ๊ฒฝ์ฐ๋ง ์ฃผ์ ์ํด A and A[-1] == N ์ด๋ ๊ฒ ๋ ์กฐ๊ฑด and๋ก ๊ฒฐํฉ
โ 9523 Arithmetic with Morse โ
import sys
input=sys.stdin.readline
N=int(input())
morse = list(input().rstrip().split())
exp=[]
for code in morse:
if code == '+' or code == '*':
exp.append(code)
else:
mid = 0
for x in code:
if x == '=': mid+=10
elif x == ':': mid+=2
elif x == '.': mid+=1
else: mid+=5
exp.append(mid)
plus_stack=[]
ans=0
multiply = False
for x in exp:
if x == '*':
multiply = True
elif x == '+':
pass
else:
if multiply:
plus_stack.append(plus_stack.pop() * x)
multiply = False
else:
plus_stack.append(x)
print(sum(plus_stack))
๐ซถ ์ผ๋จ ๋ฌธ์ ์ ์ฃผ์ด์ง ๋๋ก +์ * ์ฌ์ด์ ์๋ ๊ฐ ๋ชจ์ค ๋ถํธ๋ฅผ ๋ชจ๋ ์ซ์๋ก ๋ฐ๊พธ์ด +์ * ์ฌ์ด์ ์ซ์ add up
๐ซถ ๊ทธ๋ฌ๋ฉด exp๊ฐ 3 + 5 * 3๊ณผ ๊ฐ์ ํํ๋ก ๋์จ๋ค. ์ด๋ฅผ ํ์ด์ฌ์์๋ eval()๋ก ํ ๋ฐฉ์ ๊ณ์ฐํ ์ ์์ผ๋, ์คํ์ ํ์ฉํด์๋ ๊ฐ๋จํ๊ฒ ๊ณ์ฐํ ์ ์๋ค. ์๋ ๊ทธ๋ฆผ ์ฐธ๊ณ
: ์คํ์ +ํ ์ซ์๋ค๋ง ๋ฃ๊ฒ ํ๊ธฐ ์ํด * ๋๋ ์ซ์๋ ์๋ก ์ซ์๋ผ๋ฆฌ ๊ณฑํ ๊ฒฐ๊ณผ๋ฅผ ๋ฃ๋๋ค. ์ฆ, A * B ํํ๊ฐ ์๋ค๋ฉด, * ๊ธฐํธ๋ฅผ ๋ง๋ฌ์ ๊ฒฝ์ฐ stack.pop()์ด A์ด๋ฏ๋ก * ๊ธฐํธ๋ฅผ ๋ง๋ฌ๋ค๋ flag ๋ณ์ True ์ฒ๋ฆฌ. ์ดํ flag ๋ณ์๊ฐ True์ผ ๋ ์์ stack.pop()๊ณผ ๊ณฑํ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ stack์ ๋ฃ๊ธฐ. ์ด๋ ๊ฒ +์ *๋ก ์ด๋ฃจ์ด์ง ์์ ์คํ์ ์ฌ์ฉํด ๊ณ์ฐํ ์ ์๋ค.
'BOJ > ๐ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โ Prefix Sum Upper + Intermediate I - 7 Solvedโ (0) | 2023.01.18 |
---|---|
โ PQ ์ค์๊ธ - 6๋ฌธ์ ()โ (0) | 2023.01.11 |
โ Bitmasking Intermediate I - 3 Solvedโ (0) | 2023.01.05 |
โ DP Upper-Intermediate I - 15 Solvedโ (0) | 2023.01.03 |
โ DP Intermediate I - 20 Solvedโ (0) | 2022.12.22 |
๋๊ธ