문제 제목 : 교차로
문제 출처 : 소프티어
알고리즘 : simulation
문제원본링크
https://softeer.ai/practice/info.do?idx=1&eid=803
import sys; reader = sys.stdin.readline
from collections import defaultdict,deque
from sys import setrecursionlimit
setrecursionlimit(5000)
INF = int(2e9)
aord = ord('A'); tonum = defaultdict(int); tonum[(1<<0)] = 0; tonum[(1<<1)] = 1; tonum[(1<<2)] = 2; tonum[(1<<3)] = 3
block = defaultdict(int)
block[0] = 3; block[1] = 0; block[2] = 1; block[3] = 2
n = int(reader().rstrip())
INF = 0
car = [deque([]) for _ in range(4)]
all = 0
for nn in range(n):
time, w = input().split()
time = int(time)
car[ord(w) - aord].append( [time,nn] )
all +=1
INF = max( INF, time+1)
ans = [-1] * n
pre = -1
for _ in range(all):
nowbit, nowtime,cnt = 0, INF, 0
flag = False
for i in range(4):
if car[i] :
if pre+1 >= car[i][0][0]:
if flag:
nowbit |= (1<<i)
cnt +=1
else:
nowbit = (1<<i)
cnt = 1;
nowtime = pre+1
flag = True
else:
if flag: continue
if nowtime > car[i][0][0]:
nowbit = (1<<i)
nowtime = car[i][0][0]
cnt = 1
elif nowtime == car[i][0][0]:
nowbit |= (1<<i)
cnt += 1
if cnt == 0: break
if cnt == 4:break
bit = nowbit
while bit:
nextt = bit&-bit
bit &= (bit-1)
num = tonum[nextt]
if nowbit & (1<<block[num]): continue
ans[ car[num][0][1] ] = nowtime
car[num].popleft()
pre = nowtime
print(*ans, sep="\n")
'Problem Solving' 카테고리의 다른 글
[Math] 점프와 순간이동-프로그래머스 (0) | 2022.10.12 |
---|---|
[Queue] 다리를 지나는 트럭-프로그래머스 (0) | 2022.10.11 |
[Brute Force] 카펫-프로그래머스(Lv.2) (0) | 2022.07.24 |
[Brute Force] 소수 찾기-프로그래머스(Lv.2) (0) | 2022.07.24 |
[Brute Force] 모의고사-programmers(Lv.1) (0) | 2022.07.24 |