1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| # 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
import sys, heapq
input = sys.stdin.readline
def solve():
N = int(input().strip())
M = 2 * N
L = [0]*M
D = [0]*M
for i in range(M):
l, d = map(int, input().split())
L[i] = l; D[i] = d
# U 힙: (값, id). lazy deletion을 위해 살아있는지 체크 집합 유지
U_by_l = [(L[i], i) for i in range(M)]
U_by_d = [(D[i], i) for i in range(M)]
heapq.heapify(U_by_l)
heapq.heapify(U_by_d)
aliveU = [True]*M
# 스왑 힙 (증가비용, id)
L_sw = [] # (D[i]-L[i], i)
D_sw = [] # (L[i]-D[i], i)
inL = [False]*M
inD = [False]*M
def eraseU(i):
if aliveU[i]:
aliveU[i] = False
def move_U_to_L(i):
eraseU(i)
inL[i] = True
heapq.heappush(L_sw, (D[i]-L[i], i))
def move_U_to_D(i):
eraseU(i)
inD[i] = True
heapq.heappush(D_sw, (L[i]-D[i], i))
def move_D_to_L(i):
if not inD[i]: return
inD[i] = False
heapq.heappush(L_sw, (D[i]-L[i], i))
def move_L_to_D(i):
if not inL[i]: return
inL[i] = False
heapq.heappush(D_sw, (L[i]-D[i], i))
def pop_clean(heap, valid):
# valid: callable(id)->bool
while heap and not valid(heap[0][1]):
heapq.heappop(heap)
return heap[0] if heap else None
def valid_U(i):
return i >= 0 and aliveU[i]
def type1():
# U에서 서로 다른 두 아이템으로 U->L, U->D
a = pop_clean(U_by_l, valid_U)
b = pop_clean(U_by_d, valid_U)
if not a or not b: return None
if a[1] != b[1]:
return (a[0] + b[0], ('uL', a[1]), ('uD', b[1]))
# 겹치면 두 번째 후보 사용: 임시 pop 후 복구
x = heapq.heappop(U_by_l)
y = heapq.heappop(U_by_d)
a2 = pop_clean(U_by_l, valid_U)
b2 = pop_clean(U_by_d, valid_U)
cand = []
if a2: cand.append((a2[0] + y[0], ('uL', a2[1]), ('uD', y[1])))
if b2: cand.append((x[0] + b2[0], ('uL', x[1]), ('uD', b2[1])))
heapq.heappush(U_by_l, x)
heapq.heappush(U_by_d, y)
if not cand: return None
return min(cand, key=lambda t: t[0])
def type2():
# D->L + U->D + U->D
picks = []
popped = []
for _ in range(2):
b = pop_clean(U_by_d, valid_U)
if not b:
for z in popped: heapq.heappush(U_by_d, z)
return None
z = heapq.heappop(U_by_d)
popped.append(z)
picks.append(z)
sw = pop_clean(D_sw, lambda i: inD[i])
for z in popped: heapq.heappush(U_by_d, z)
if not sw: return None
cost = picks[0][0] + picks[1][0] + sw[0]
return (cost, ('dL', sw[1]), ('uD', picks[0][1]), ('uD', picks[1][1]))
def type3():
# L->D + U->L + U->L
picks = []
popped = []
for _ in range(2):
a0 = pop_clean(U_by_l, valid_U)
if not a0:
for z in popped: heapq.heappush(U_by_l, z)
return None
z = heapq.heappop(U_by_l)
popped.append(z)
picks.append(z)
sw = pop_clean(L_sw, lambda i: inL[i])
for z in popped: heapq.heappush(U_by_l, z)
if not sw: return None
cost = picks[0][0] + picks[1][0] + sw[0]
return (cost, ('lD', sw[1]), ('uL', picks[0][1]), ('uL', picks[1][1]))
total = 0
ans = [0]*(N+1)
for k in range(1, N+1):
cands = []
t1 = type1()
if t1: cands.append(t1)
t2 = type2()
if t2: cands.append(t2)
t3 = type3()
if t3: cands.append(t3)
best = min(cands, key=lambda t: t[0])
total += best[0]
for op in best[1:]:
if op[0] == 'uL': move_U_to_L(op[1])
elif op[0] == 'uD': move_U_to_D(op[1])
elif op[0] == 'dL': move_D_to_L(op[1])
elif op[0] == 'lD': move_L_to_D(op[1])
ans[k] = total
print('\n'.join(map(str, ans[1:])))
if __name__ == "__main__":
solve()
|