๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/PS

[BOJ/๋ฐฑ์ค€] 20923 ์ˆซ์ž ํ• ๋ฆฌ๊ฐˆ๋ฆฌ ๊ฒŒ์ž„ - ํŒŒ์ด์ฌ

๋ฌธ์ œ์„ค๋ช…

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

 

20923๋ฒˆ: ์ˆซ์ž ํ• ๋ฆฌ๊ฐˆ๋ฆฌ ๊ฒŒ์ž„

์ฒซ์งธ ์ค„์—๋Š” ๋„๋„์™€ ์ˆ˜์—ฐ์ด๊ฐ€ ๊ฐ€์ง€๋Š” ์นด๋“œ์˜ ๊ฐœ์ˆ˜ $N$($ 1 \leq N \leq 30\,000$)๊ณผ ๊ฒŒ์ž„ ์ง„ํ–‰ ํšŸ์ˆ˜ $M$($ 1 \leq M \leq 2\,500\,000$)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ $N$๊ฐœ์˜ ์ค„์—๋Š” ๋„์–ด์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋„๋„์™€ ์ˆ˜์—ฐ

www.acmicpc.net

๊ฒŒ์ž„์˜ ๊ทœ์น™์€ ํ”ํžˆ ์•Œ๊ณ  ์žˆ๋Š” ํ• ๋ฆฌ๊ฐˆ๋ฆฌ ๊ฒŒ์ž„์˜ ๊ทœ์น™๊ณผ ์œ ์‚ฌํ•˜๋‹ค. 

๊ฒŒ์ž„์„ m๋ฒˆ ๋ฐ˜๋ณตํ–ˆ์„ ๋•Œ ์นด๋“œ๋ฅผ ๋” ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์‚ฌ๋žŒ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ

 

๋ฌธ์ œํ’€์ด

์ฒ˜์Œ์—๋Š” ๋„๋„์™€ ์ˆ˜์—ฐ์ด์˜ ์นด๋“œ ๋ฑ๊ณผ ๊ทธ๋ผ์šด๋“œ ๋ฑ์„ ๊ฐ๊ฐ 2๊ฐœ์”ฉ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ํ’€์—ˆ์—ˆ๋Š”๋ฐ,

if ๋ฌธ์˜ ์กฐ๊ฑด๋„ ๋ณต์žกํ•ด์ง€๊ณ  while๋ฌธ์„ ๋‚จ๋ฐœํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค

 

์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ๋ชปํ•ด์„œ ๊ฒฐ๊ตญ ์ถœ์ œ์ง„ ๋ถ„์ด ์˜ฌ๋ ค์ฃผ์‹  ํ•ด์„ค ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค

๊ธฐ๋ณธ์ ์ธ ์•„์ด๋””์–ด๋Š” ๋น„์Šทํ•˜๋‚˜, ์นด๋“œ ๋ฑ๊ณผ ๊ทธ๋ผ์šด๋“œ ๋ฑ์„ 2์ฐจ์›์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์šฉํ•ด ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค

ํŠนํžˆ, ๋ฑ์„ ํ•ฉ์น˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑํ•œ for๋ฌธ์„ ๋ณด๊ณ  ์šฐ์™€! ํ–ˆ๋‹ค..

 

์ฝ”๋“œ

์‹œ๊ฐ„์ดˆ๊ณผ ๋‚ฌ๋˜ ์ฝ”๋“œ

import sys
from collections import deque

n,m=map(int,input().split())
do,su=deque([]),deque([])

for i in range(n):
    a,b=map(int,input().split())
    do.append(a)
    su.append(b)
    
tmp1,tmp2=deque([]),deque([])
for i in range(m):
    if i%2==0: #do๊ฐ€ ๋จผ์ €
        tmp1.append(do.pop())
    else:
        tmp2.append(su.pop())
    if (tmp1 and tmp1[-1]==5) or (tmp2 and tmp2[-1]==5):##do๊ฐ€ ์ข…
        while(tmp2):
            do.appendleft(tmp2.popleft())
        while(tmp1):
            do.appendleft(tmp1.popleft())
    elif tmp1+tmp2==5 and tmp1 and tmp2:
        while(tmp1):
            su.appendleft(tmp1.popleft())
        while(tmp2):
            su.appendleft(tmp2.popleft())
    if len(do)==0:
        break
    elif len(su)==0:
        break

if len(do)>len(su):
    print('do')
elif len(do)<len(su):
    print('su')
else:
    print('dosu')

ํ†ต๊ณผํ•œ ์ฝ”๋“œ

import sys
from collections import deque

n,m=map(int,input().split())
start=[deque(), deque()]
temp=[deque(),deque()]

for i in range(n):
    a,b=map(int,input().split())
    start[0].appendleft(a)
    start[1].appendleft(b)

t=0
for _ in range(m):
    temp[t].appendleft(start[t].popleft())
    if not start[t]:##๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ 0์ด ๋˜๋ฉด
        break
    win=-1
    for i in [0,1]:
        if temp[i] and temp[i][0]==5: # ๋‘˜์ค‘ ํ•˜๋‚˜๊ฐ€ 5
            win=0
    if temp[0] and temp[1] and temp[0][0]+temp[1][0]==5: 
            win=1
    if win!=-1:##์ข… ์น˜์ง€ ์•Š์„ ๊ฒฝ์šฐ pass
        for i in [1-win,win]: ##๋ˆ„๊ตฌ ์นด๋“œ๋ฅผ ๋จผ์ € ๊ฐ€์ ธ์˜ค๋Š”์ง€
            while temp[i]:
                start[win].append(temp[i].pop())
    t=1-t

if len(start[0])>len(start[1]):
    print('do')
elif len(start[1])>len(start[0]):
    print('su')
else:
    print('dosu')

 

์–ด๋ ค์› ๋˜ ์ /๋Š๋‚€ ์ 

ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜๋“ค์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ž˜ ์•Œ์•„๋ด์•ผ๊ฒ ๋‹ค.. ์‹œ๊ฐ„์ดˆ๊ณผ ์•ˆ๋‚˜๋„๋ก ๋” ํšจ์œจ์ ์œผ๋กœ ์ฝ”๋“œ ์งœ๊ธฐ..!