|
Download tasks/equivalent-program-pair-search/contexts/sample-00006.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 246 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00006.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00006.md
-
curl -L -o sample-00006.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00006.md
246 kB
| ### 001 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 1) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 002 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = -2 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 003 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if not col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 004 | |
| ```python | |
| def _vertex(lit): | |
| if lit > 0: | |
| return -2 * (lit - 1) | |
| else: | |
| return 2 * (-lit - 1) + 1 | |
| def tarjan(graph): | |
| n = len(graph) | |
| dfs_num = [None] * n | |
| dfs_min = [n] * n | |
| waiting = [] | |
| waits = [False] * n | |
| sccp = [] | |
| dfs_time = 0 | |
| times_seen = [-1] * n | |
| for start in range(n): | |
| if times_seen[start] == -1: | |
| times_seen[start] = 0 | |
| to_visit = [start] | |
| while to_visit: | |
| node = to_visit[-1] | |
| if times_seen[node] == 0: | |
| dfs_num[node] = dfs_time | |
| dfs_min[node] = dfs_time | |
| dfs_time += 1 | |
| waiting.append(node) | |
| waits[node] = True | |
| children = graph[node] | |
| if times_seen[node] == len(children): | |
| to_visit.pop() | |
| dfs_min[node] = dfs_num[node] | |
| for child in children: | |
| if waits[child] and dfs_min[child] < dfs_min[node]: | |
| dfs_min[node] = dfs_min[child] | |
| if dfs_min[node] == dfs_num[node]: | |
| component = [] | |
| while True: | |
| u = waiting.pop() | |
| waits[u] = False | |
| component.append(u) | |
| if u == node: | |
| break | |
| sccp.append(component) | |
| else: | |
| child = children[times_seen[node]] | |
| times_seen[node] += 1 | |
| if times_seen[child] == -1: | |
| times_seen[child] = 0 | |
| to_visit.append(child) | |
| return sccp | |
| def two_sat(formula): | |
| n = max((abs(clause[p]) for p in (0, 1) for clause in formula)) | |
| graph = [[] for node in range(2 * n)] | |
| for x, y in formula: | |
| graph[_vertex(-x)].append(_vertex(y)) | |
| graph[_vertex(-y)].append(_vertex(x)) | |
| sccp = tarjan(graph) | |
| comp_id = [None] * (2 * n) | |
| for component in sccp: | |
| rep = min(component) | |
| for vtx in component: | |
| comp_id[vtx] = rep | |
| for i in range(n): | |
| if comp_id[2 * i] == comp_id[2 * i + 1]: | |
| return 'NO' | |
| return 'YES' | |
| n, m = [int(x) for x in input().split()] | |
| doors_status = [int(x) for x in input().split()] | |
| switches = [list(map(int, input().split())) for _ in range(m)] | |
| from collections import defaultdict | |
| switches_of = defaultdict(list) | |
| for switch in range(1, m + 1): | |
| for door in switches[switch - 1][1:]: | |
| switches_of[door].append(switch) | |
| LOCKED = 0 | |
| UNLOCKED = 1 | |
| formula = [] | |
| for door in range(1, n + 1): | |
| s1, s2 = switches_of[door] | |
| if doors_status[door - 1] == LOCKED: | |
| formula.append((s1, s2)) | |
| formula.append((-s1, -s2)) | |
| else: | |
| formula.append((s1, -s2)) | |
| formula.append((-s1, s2)) | |
| print(two_sat(formula)) | |
| ``` | |
| ### 005 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 006 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [1 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 1].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 007 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i - 1] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 008 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(3) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(0) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 009 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += -1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 010 | |
| ```python | |
| import math | |
| n, q = map(int, input().split()) | |
| maxh = int(math.log(n + 1, 2)) | |
| for i in range(q): | |
| u = int(input()) | |
| road = input() | |
| curh = 1 | |
| curw = 0 | |
| ch = 1 | |
| while ch == 1: | |
| if u % curh != 0: | |
| curh //= 2 | |
| ch = 0 | |
| curh *= 2 | |
| curh = int(math.log(curh, 2)) | |
| a = 1 << curh - 1 | |
| b = -1 << curh | |
| curw = (u - a) // b + 1 | |
| for move in road: | |
| if move == 'L': | |
| if curh != 1: | |
| curh -= 1 | |
| curw = curw * 2 - 1 | |
| u -= 1 << curh - 1 | |
| elif move == 'R': | |
| if curh != 1: | |
| curh -= 1 | |
| curw *= 2 | |
| u += 1 << curh - 1 | |
| elif curh != maxh: | |
| if curw % 2 == 1: | |
| u = (u + u + (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 + 1 | |
| else: | |
| u = (u + u - (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 | |
| print(u) | |
| ``` | |
| ### 011 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[2]) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (max(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 012 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = max([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 013 | |
| ```python | |
| import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time | |
| sys.setrecursionlimit(10 ** 7) | |
| inf = 10 ** 20 | |
| mod = 10 ** 9 + 7 | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def LF(): | |
| return [float(x) for x in sys.stdin.readline().split()] | |
| def LS(): | |
| return sys.stdin.readline().split() | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def F(): | |
| return float(sys.stdin.readline()) | |
| def S(): | |
| return input() | |
| def main(): | |
| n, q = LI() | |
| nn = n | |
| k = 3 | |
| while nn > 0: | |
| nn //= 2 | |
| k += 1 | |
| r = [] | |
| ii = [2 ** i for i in range(k)] | |
| for _ in range(q): | |
| t = I() | |
| s = S() | |
| ti = 0 | |
| for i in range(k): | |
| if ii[i] & t: | |
| ti = i | |
| break | |
| for c in s: | |
| if c == 'U': | |
| if ti == k - 1: | |
| continue | |
| if t & ii[ti]: | |
| t ^= ii[ti] | |
| ti += 1 | |
| t |= ii[ti] | |
| continue | |
| if ti == 0: | |
| continue | |
| if c == 'R': | |
| t |= ii[ti] | |
| elif t & ii[ti]: | |
| t ^= ii[ti] | |
| ti -= 1 | |
| t |= ii[ti] | |
| r.append(t) | |
| return '\n'.join(map(str, r)) | |
| print(main()) | |
| ``` | |
| ### 014 | |
| ```python | |
| def maxx(n): | |
| return n & -n | |
| n, q = map(int, input().split()) | |
| root = n % 2 + 1 | |
| while q > 0: | |
| x = int(input()) | |
| s = input() | |
| for i in s: | |
| if i == 'U' and x != root: | |
| p = x + maxx(x) | |
| if x == p - maxx(p) // 2: | |
| x = p | |
| else: | |
| x = x - maxx(x) | |
| elif i == 'L': | |
| x = x - maxx(x) // 2 | |
| elif i == 'R': | |
| x = x + maxx(x) // 2 | |
| q = q - 1 | |
| print(x) | |
| ``` | |
| ### 015 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 4: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 016 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j + w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 017 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 4 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 018 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(3) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 019 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 2] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 020 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [1 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 1].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is not None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 021 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [2] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 022 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {3: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 023 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-3, W)) | |
| ``` | |
| ### 024 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 3, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 1 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 026 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1 - 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 027 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[0]) | |
| if k < m: | |
| maxDist = edgeList[min(m, k) - 1][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 028 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += -1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 029 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 3 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 030 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 2 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 031 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 032 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[-2]) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (min(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 033 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[2]) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (min(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[2], reverse=True) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (min(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 035 | |
| ```python | |
| import math | |
| n, q = map(int, input().split()) | |
| maxh = int(math.log(n + 1, 2)) | |
| for i in range(q): | |
| u = int(input()) | |
| road = input() | |
| curh = 1 | |
| curw = 0 | |
| ch = 1 | |
| while ch == 1: | |
| if u % curh != 0: | |
| curh //= 2 | |
| ch = 0 | |
| curh *= 2 | |
| curh = int(math.log(curh, 2)) | |
| a = 1 << curh - 1 | |
| b = 1 << curh | |
| curw = (u - a) // b + -1 | |
| for move in road: | |
| if move == 'L': | |
| if curh != 1: | |
| curh -= 1 | |
| curw = curw * 2 - 1 | |
| u -= 1 << curh - 1 | |
| elif move == 'R': | |
| if curh != 1: | |
| curh -= 1 | |
| curw *= 2 | |
| u += 1 << curh - 1 | |
| elif curh != maxh: | |
| if curw % 2 == 1: | |
| u = (u + u + (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 + 1 | |
| else: | |
| u = (u + u - (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 | |
| print(u) | |
| ``` | |
| ### 036 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 037 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-0] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 038 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(3, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 039 | |
| ```python | |
| import math | |
| n, q = map(int, input().split()) | |
| maxh = int(math.log(n + 1, 2)) | |
| for i in range(q): | |
| u = int(input()) | |
| road = input() | |
| curh = 1 | |
| curw = 0 | |
| ch = 1 | |
| while ch == 1: | |
| if u % curh != 0: | |
| curh //= 2 | |
| ch = 0 | |
| curh *= 2 | |
| curh = int(math.log(curh, 2)) | |
| a = 1 << curh - 1 | |
| b = 2 << curh | |
| curw = (u - a) // b + 1 | |
| for move in road: | |
| if move == 'L': | |
| if curh != 1: | |
| curh -= 1 | |
| curw = curw * 2 - 1 | |
| u -= 1 << curh - 1 | |
| elif move == 'R': | |
| if curh != 1: | |
| curh -= 1 | |
| curw *= 2 | |
| u += 1 << curh - 1 | |
| elif curh != maxh: | |
| if curw % 2 == 1: | |
| u = (u + u + (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 + 1 | |
| else: | |
| u = (u + u - (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 | |
| print(u) | |
| ``` | |
| ### 040 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| VERLIST = [] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| VERLIST.append(a) | |
| VERLIST.append(b) | |
| VERLIST = sorted(set(VERLIST)) | |
| import heapq | |
| ANS = [-1 << 50] * k | |
| for start in VERLIST: | |
| MINCOST = [1 << 50] * (n + 1) | |
| checking = [(0, start)] | |
| MINCOST[start] = 0 | |
| j = 0 | |
| while j < k: | |
| if not checking: | |
| break | |
| cost, checktown = heapq.heappop(checking) | |
| if cost >= -ANS[0]: | |
| break | |
| if MINCOST[checktown] < cost: | |
| continue | |
| if cost != 3 and checktown > start: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -cost) | |
| j += 1 | |
| for to, co in COST_vertex[checktown]: | |
| if MINCOST[to] > cost + co: | |
| MINCOST[to] = cost + co | |
| heapq.heappush(checking, (cost + co, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 041 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x - 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 042 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(2, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 043 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k - 1] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 044 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[0]] = True | |
| queue.append(i[0]) | |
| if i[1] == -1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == 1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 045 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount *= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 046 | |
| ```python | |
| import sys | |
| n, m = map(int, input().split()) | |
| door = list(map(int, input().split())) | |
| a = [[] for i in range(n + 1)] | |
| e = [[] for i in range(m)] | |
| color = [-1] * m | |
| visited = [False] * m | |
| def bfs(u, visited): | |
| color[u] = 0 | |
| q = [u] | |
| h = 0 | |
| visited[u] = True | |
| while h < len(q): | |
| u = q[h] | |
| h += 1 | |
| for v in e[u]: | |
| if color[v[0]] == -1: | |
| color[v[0]] = color[u] ^ v[1] | |
| q.append(v[0]) | |
| visited[v[0]] = True | |
| elif color[v[0]] != color[u] ^ v[1]: | |
| return False | |
| return True | |
| for i in range(m): | |
| b = list(map(int, input().split())) | |
| for j in range(0, len(b)): | |
| k = b[j] | |
| a[k].append(i) | |
| if len(a[k]) == 2: | |
| e[a[k][0]].append((a[k][1], 1 - door[k - 1])) | |
| e[a[k][1]].append((a[k][0], 1 - door[k - 1])) | |
| flag = True | |
| for i in range(m): | |
| if color[i] == -1: | |
| if bfs(i, visited) == False: | |
| flag = False | |
| break | |
| if flag: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 047 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx - 1] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 048 | |
| ```python | |
| from math import * | |
| n, q = list(map(int, input().split())) | |
| depth = int(log2(n + 1)) | |
| def get_layer(u): | |
| for layer in range(depth): | |
| if u % 2 ** (layer + 1) == 2 ** layer: | |
| return layer | |
| def is_right_child(layer, u): | |
| return u % 2 ** (layer + 1) % 2 == 0 | |
| for x in range(q): | |
| u = int(input()) | |
| layer = get_layer(u) | |
| steps = input() | |
| for s in steps: | |
| if s == 'U': | |
| if layer == depth - 1: | |
| continue | |
| k = u // 2 ** (layer + 1) // 2 | |
| layer += 1 | |
| u = 2 ** (layer + 1) * k + 1 ** layer | |
| elif layer == 0: | |
| continue | |
| elif s == 'R': | |
| k = u // 2 ** (layer + 1) * 2 + 1 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif s == 'L': | |
| k = u // 2 ** (layer + 1) * 2 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| print(u) | |
| ``` | |
| ### 049 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < -1: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 050 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1 - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 051 | |
| ```python | |
| def maxx(n): | |
| return n & -n | |
| n, q = map(int, input().split()) | |
| root = n // 2 + 1 | |
| while q > 0: | |
| x = int(input()) | |
| s = input() | |
| for i in s: | |
| if i == 'U' and x != root: | |
| p = x + maxx(x) | |
| if x == p - maxx(p) % 2: | |
| x = p | |
| else: | |
| x = x - maxx(x) | |
| elif i == 'L': | |
| x = x - maxx(x) // 2 | |
| elif i == 'R': | |
| x = x + maxx(x) // 2 | |
| q = q - 1 | |
| print(x) | |
| ``` | |
| ### 052 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--2) | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| n, m = map(int, input().split()) | |
| door = list(map(int, input().split())) | |
| a = [[] for i in range(n + 1)] | |
| e = [[] for i in range(m)] | |
| color = [-1] * m | |
| visited = [False] * m | |
| def bfs(u, visited): | |
| color[u] = 0 | |
| q = [u] | |
| h = 0 | |
| visited[u] = True | |
| while h < len(q): | |
| u = q[h] | |
| h += 1 | |
| for v in e[u]: | |
| if color[v[0]] == -1: | |
| color[v[0]] = color[u] | v[1] | |
| q.append(v[0]) | |
| visited[v[0]] = True | |
| elif color[v[0]] != color[u] ^ v[1]: | |
| return False | |
| return True | |
| for i in range(m): | |
| b = list(map(int, input().split())) | |
| for j in range(1, len(b)): | |
| k = b[j] | |
| a[k].append(i) | |
| if len(a[k]) == 2: | |
| e[a[k][0]].append((a[k][1], 1 - door[k - 1])) | |
| e[a[k][1]].append((a[k][0], 1 - door[k - 1])) | |
| flag = True | |
| for i in range(m): | |
| if color[i] == -1: | |
| if bfs(i, visited) == False: | |
| flag = False | |
| break | |
| if flag: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 054 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [1 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 1 - 1].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 055 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[4] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 056 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty - 1] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 057 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 2 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 058 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[1]] = True | |
| queue.append(i[0]) | |
| if i[1] == 1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == 1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 059 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 060 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == -2: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 061 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-0) | |
| ``` | |
| ### 062 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1 - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i])) | |
| print(ans) | |
| ``` | |
| ### 063 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result -= sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 064 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 065 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p <= inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 066 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 2, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 067 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[2]) | |
| if not k < m: | |
| maxDist = edgeList[min(m, k) - 1][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 068 | |
| ```python | |
| from math import * | |
| n, q = list(map(int, input().split())) | |
| depth = int(log2(n + 1)) | |
| def get_layer(u): | |
| for layer in range(depth): | |
| if u % 2 ** (layer + 1) == 2 ** layer: | |
| return layer | |
| def is_right_child(layer, u): | |
| return u % 2 ** (layer + 1) % 2 == 0 | |
| for x in range(q): | |
| u = int(input()) | |
| layer = get_layer(u) | |
| steps = input() | |
| for s in steps: | |
| if s == 'U': | |
| if layer == depth - 1: | |
| continue | |
| k = u // 2 ** (layer + 1) // 2 | |
| layer += 1 | |
| u = 2 ** (layer + 1) * k + 3 ** layer | |
| elif layer == 0: | |
| continue | |
| elif s == 'R': | |
| k = u // 2 ** (layer + 1) * 2 + 1 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif s == 'L': | |
| k = u // 2 ** (layer + 1) * 2 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| print(u) | |
| ``` | |
| ### 069 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[-1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 070 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += -2 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 071 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N - 1)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 072 | |
| ```python | |
| import sys | |
| n, m = map(int, input().split()) | |
| door = list(map(int, input().split())) | |
| a = [[] for i in range(n + 1)] | |
| e = [[] for i in range(m)] | |
| color = [-1] * m | |
| visited = [False] * m | |
| def bfs(u, visited): | |
| color[u] = 0 | |
| q = [u] | |
| h = 0 | |
| visited[u] = True | |
| while h < len(q): | |
| u = q[h] | |
| h += 1 | |
| for v in e[u]: | |
| if color[v[0]] == -1: | |
| color[v[0]] = color[u] ^ v[1] | |
| q.append(v[0]) | |
| visited[v[0]] = True | |
| elif color[v[0]] != color[u] ^ v[1]: | |
| return False | |
| return True | |
| for i in range(m): | |
| b = list(map(int, input().split())) | |
| for j in range(1, len(b)): | |
| k = b[j] | |
| a[k].append(i) | |
| if len(a[k]) == 2: | |
| e[a[k][0]].append((a[k][1], 1 + door[k - 1])) | |
| e[a[k][1]].append((a[k][0], 1 - door[k - 1])) | |
| flag = True | |
| for i in range(m): | |
| if color[i] == -1: | |
| if bfs(i, visited) == False: | |
| flag = False | |
| break | |
| if flag: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 073 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(-2, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(-1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 075 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= -1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 076 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 077 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx - 1] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 078 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][-1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 079 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i - 1] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 080 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(2, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 081 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 2 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 082 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 2 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 083 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[2]) | |
| if k < m: | |
| maxDist = edgeList[min(m, k) - 1][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 084 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| for i in range(min(m, k)): | |
| x, y, c = EDGE[i] | |
| EDGE[i] = (c, x, y) | |
| USED_SET = set() | |
| ANS = [-1 << 50] * k | |
| import heapq | |
| while EDGE: | |
| c, x, y = heapq.heappop(EDGE) | |
| if (x, y) in USED_SET or c >= -ANS[0]: | |
| continue | |
| else: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -c) | |
| USED_SET.add((x, y)) | |
| USED_SET.add((y, x)) | |
| for to, cost in COST_vertex[x]: | |
| if to != y and (not (y, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c * cost, y, to)) | |
| for to, cost in COST_vertex[y]: | |
| if to != x and (not (x, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, x, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((-1 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 086 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == -1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 087 | |
| ```python | |
| def _vertex(lit): | |
| if lit > 1: | |
| return 2 * (lit - 1) | |
| else: | |
| return 2 * (-lit - 1) + 1 | |
| def tarjan(graph): | |
| n = len(graph) | |
| dfs_num = [None] * n | |
| dfs_min = [n] * n | |
| waiting = [] | |
| waits = [False] * n | |
| sccp = [] | |
| dfs_time = 0 | |
| times_seen = [-1] * n | |
| for start in range(n): | |
| if times_seen[start] == -1: | |
| times_seen[start] = 0 | |
| to_visit = [start] | |
| while to_visit: | |
| node = to_visit[-1] | |
| if times_seen[node] == 0: | |
| dfs_num[node] = dfs_time | |
| dfs_min[node] = dfs_time | |
| dfs_time += 1 | |
| waiting.append(node) | |
| waits[node] = True | |
| children = graph[node] | |
| if times_seen[node] == len(children): | |
| to_visit.pop() | |
| dfs_min[node] = dfs_num[node] | |
| for child in children: | |
| if waits[child] and dfs_min[child] < dfs_min[node]: | |
| dfs_min[node] = dfs_min[child] | |
| if dfs_min[node] == dfs_num[node]: | |
| component = [] | |
| while True: | |
| u = waiting.pop() | |
| waits[u] = False | |
| component.append(u) | |
| if u == node: | |
| break | |
| sccp.append(component) | |
| else: | |
| child = children[times_seen[node]] | |
| times_seen[node] += 1 | |
| if times_seen[child] == -1: | |
| times_seen[child] = 0 | |
| to_visit.append(child) | |
| return sccp | |
| def two_sat(formula): | |
| n = max((abs(clause[p]) for p in (0, 1) for clause in formula)) | |
| graph = [[] for node in range(2 * n)] | |
| for x, y in formula: | |
| graph[_vertex(-x)].append(_vertex(y)) | |
| graph[_vertex(-y)].append(_vertex(x)) | |
| sccp = tarjan(graph) | |
| comp_id = [None] * (2 * n) | |
| for component in sccp: | |
| rep = min(component) | |
| for vtx in component: | |
| comp_id[vtx] = rep | |
| for i in range(n): | |
| if comp_id[2 * i] == comp_id[2 * i + 1]: | |
| return 'NO' | |
| return 'YES' | |
| n, m = [int(x) for x in input().split()] | |
| doors_status = [int(x) for x in input().split()] | |
| switches = [list(map(int, input().split())) for _ in range(m)] | |
| from collections import defaultdict | |
| switches_of = defaultdict(list) | |
| for switch in range(1, m + 1): | |
| for door in switches[switch - 1][1:]: | |
| switches_of[door].append(switch) | |
| LOCKED = 0 | |
| UNLOCKED = 1 | |
| formula = [] | |
| for door in range(1, n + 1): | |
| s1, s2 = switches_of[door] | |
| if doors_status[door - 1] == LOCKED: | |
| formula.append((s1, s2)) | |
| formula.append((-s1, -s2)) | |
| else: | |
| formula.append((s1, -s2)) | |
| formula.append((-s1, s2)) | |
| print(two_sat(formula)) | |
| ``` | |
| ### 088 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= -1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 089 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(1) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 090 | |
| ```python | |
| import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time | |
| sys.setrecursionlimit(10 ** 7) | |
| inf = 10 ** 20 | |
| mod = 10 ** 9 + 7 | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def LF(): | |
| return [float(x) for x in sys.stdin.readline().split()] | |
| def LS(): | |
| return sys.stdin.readline().split() | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def F(): | |
| return float(sys.stdin.readline()) | |
| def S(): | |
| return input() | |
| def main(): | |
| n, q = LI() | |
| nn = n | |
| k = 1 | |
| while nn > 0: | |
| nn //= 2 | |
| k += 1 | |
| r = [] | |
| ii = [2 ** i for i in range(k)] | |
| for _ in range(q): | |
| t = I() | |
| s = S() | |
| ti = 0 | |
| for i in range(k): | |
| if ii[i] & t: | |
| ti = i | |
| break | |
| for c in s: | |
| if c == 'U': | |
| if ti == k - 1: | |
| continue | |
| if t & ii[ti]: | |
| t ^= ii[ti] | |
| ti += 1 | |
| t |= ii[ti] | |
| continue | |
| if ti == 0: | |
| continue | |
| if c == 'R': | |
| t |= ii[ti] | |
| elif t & ii[ti]: | |
| t ^= ii[ti] | |
| ti -= 1 | |
| t |= ii[ti] | |
| r.append(t) | |
| return '\n'.join(map(str, r)) | |
| print(main()) | |
| ``` | |
| ### 091 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[2]) | |
| if k < m: | |
| maxDist = edgeList[min(m, k) - 5][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 092 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 093 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1 - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 094 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] >= k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 095 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == -2: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 096 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key + 1] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 097 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i - 1][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 098 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 099 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b - 1].append((a, c)) | |
| for i in range(min(m, k)): | |
| x, y, c = EDGE[i] | |
| EDGE[i] = (c, x, y) | |
| USED_SET = set() | |
| ANS = [-1 << 50] * k | |
| import heapq | |
| while EDGE: | |
| c, x, y = heapq.heappop(EDGE) | |
| if (x, y) in USED_SET or c >= -ANS[0]: | |
| continue | |
| else: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -c) | |
| USED_SET.add((x, y)) | |
| USED_SET.add((y, x)) | |
| for to, cost in COST_vertex[x]: | |
| if to != y and (not (y, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, y, to)) | |
| for to, cost in COST_vertex[y]: | |
| if to != x and (not (x, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, x, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 100 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-3) | |
| ``` | |
| ### 101 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[-1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[0]] = True | |
| queue.append(i[0]) | |
| if i[1] == 1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == 1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 102 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-3] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 103 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1 - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 104 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i - 1]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 105 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1 - 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 106 | |
| ```python | |
| import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time | |
| sys.setrecursionlimit(10 ** 7) | |
| inf = 10 ** 20 | |
| mod = 10 ** 9 + 7 | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def LF(): | |
| return [float(x) for x in sys.stdin.readline().split()] | |
| def LS(): | |
| return sys.stdin.readline().split() | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def F(): | |
| return float(sys.stdin.readline()) | |
| def S(): | |
| return input() | |
| def main(): | |
| n, q = LI() | |
| nn = n | |
| k = 0 | |
| while nn > 0: | |
| nn //= 2 | |
| k += 2 | |
| r = [] | |
| ii = [2 ** i for i in range(k)] | |
| for _ in range(q): | |
| t = I() | |
| s = S() | |
| ti = 0 | |
| for i in range(k): | |
| if ii[i] & t: | |
| ti = i | |
| break | |
| for c in s: | |
| if c == 'U': | |
| if ti == k - 1: | |
| continue | |
| if t & ii[ti]: | |
| t ^= ii[ti] | |
| ti += 1 | |
| t |= ii[ti] | |
| continue | |
| if ti == 0: | |
| continue | |
| if c == 'R': | |
| t |= ii[ti] | |
| elif t & ii[ti]: | |
| t ^= ii[ti] | |
| ti -= 1 | |
| t |= ii[ti] | |
| r.append(t) | |
| return '\n'.join(map(str, r)) | |
| print(main()) | |
| ``` | |
| ### 107 | |
| ```python | |
| def _vertex(lit): | |
| if lit > 0: | |
| return 2 * (lit - 1) | |
| else: | |
| return 2 * (-lit - 1) + 1 | |
| def tarjan(graph): | |
| n = len(graph) | |
| dfs_num = [None] * n | |
| dfs_min = [n] * n | |
| waiting = [] | |
| waits = [False] * n | |
| sccp = [] | |
| dfs_time = 0 | |
| times_seen = [-1] * n | |
| for start in range(n): | |
| if times_seen[start] == -1: | |
| times_seen[start] = 0 | |
| to_visit = [start] | |
| while to_visit: | |
| node = to_visit[-1] | |
| if times_seen[node] == 0: | |
| dfs_num[node] = dfs_time | |
| dfs_min[node] = dfs_time | |
| dfs_time += 1 | |
| waiting.append(node) | |
| waits[node] = True | |
| children = graph[node] | |
| if times_seen[node] == len(children): | |
| to_visit.pop() | |
| dfs_min[node] = dfs_num[node] | |
| for child in children: | |
| if waits[child] and dfs_min[child] < dfs_min[node]: | |
| dfs_min[node] = dfs_min[child] | |
| if dfs_min[node] == dfs_num[node]: | |
| component = [] | |
| while True: | |
| u = waiting.pop() | |
| waits[u] = False | |
| component.append(u) | |
| if u == node: | |
| break | |
| sccp.append(component) | |
| else: | |
| child = children[times_seen[node]] | |
| times_seen[node] += 1 | |
| if times_seen[child] == -1: | |
| times_seen[child] = 0 | |
| to_visit.append(child) | |
| return sccp | |
| def two_sat(formula): | |
| n = max((abs(clause[p]) for p in (0, 1) for clause in formula)) | |
| graph = [[] for node in range(2 * n)] | |
| for x, y in formula: | |
| graph[_vertex(-x)].append(_vertex(y)) | |
| graph[_vertex(-y)].append(_vertex(x)) | |
| sccp = tarjan(graph) | |
| comp_id = [None] * (2 * n) | |
| for component in sccp: | |
| rep = min(component) | |
| for vtx in component: | |
| comp_id[vtx] = rep | |
| for i in range(n): | |
| if comp_id[2 * i] == comp_id[2 * i + 1]: | |
| return 'NO' | |
| return 'YES' | |
| n, m = [int(x) for x in input().split()] | |
| doors_status = [int(x) for x in input().split()] | |
| switches = [list(map(int, input().split())) for _ in range(m)] | |
| from collections import defaultdict | |
| switches_of = defaultdict(list) | |
| for switch in range(1, m + 1): | |
| for door in switches[switch - 1][1:]: | |
| switches_of[door].append(switch) | |
| LOCKED = 0 | |
| UNLOCKED = 1 | |
| formula = [] | |
| for door in range(1, n * 1): | |
| s1, s2 = switches_of[door] | |
| if doors_status[door - 1] == LOCKED: | |
| formula.append((s1, s2)) | |
| formula.append((-s1, -s2)) | |
| else: | |
| formula.append((s1, -s2)) | |
| formula.append((-s1, s2)) | |
| print(two_sat(formula)) | |
| ``` | |
| ### 108 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[0]] = True | |
| queue.append(i[0]) | |
| if i[1] == 1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == 1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 109 | |
| ```python | |
| import collections | |
| from collections import defaultdict | |
| n = int(input()) | |
| par = [int(i) for i in input().split() if i != '\n'] | |
| suma = [int(i) for i in input().split() if i != '\n'] | |
| graph = defaultdict(list) | |
| for i in range(n - 1): | |
| graph[i + 2].append(par[i]) | |
| graph[par[i]].append(i + 2) | |
| weight = [0] * (n + 1) | |
| weight[1] = suma[0] | |
| queue = collections.deque([1]) | |
| visited = set() | |
| visited.add(1) | |
| ok = True | |
| while queue: | |
| vertex = queue.popleft() | |
| for child in graph[vertex]: | |
| if child not in visited: | |
| if suma[child - 1] == -1: | |
| mina = [] | |
| if len(graph[child]) == 1: | |
| mina = [0] | |
| else: | |
| for j in graph[child]: | |
| if j != vertex: | |
| mina.append(suma[j - 1] - suma[vertex - 1]) | |
| ans = suma[vertex - 1] + min(mina) | |
| suma[child - 1] = ans | |
| if suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| elif suma[child - 1] < suma[vertex - 1]: | |
| ok = False | |
| else: | |
| weight[child] = suma[child - 1] - suma[vertex - 1] | |
| queue.append(child) | |
| visited.add(child) | |
| if ok == True: | |
| print(sum(weight)) | |
| else: | |
| print(--1) | |
| ``` | |
| ### 110 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 3 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 111 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 1 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 112 | |
| ```python | |
| def maxx(n): | |
| return n & -n | |
| n, q = map(int, input().split()) | |
| root = n // 6 + 1 | |
| while q > 0: | |
| x = int(input()) | |
| s = input() | |
| for i in s: | |
| if i == 'U' and x != root: | |
| p = x + maxx(x) | |
| if x == p - maxx(p) // 2: | |
| x = p | |
| else: | |
| x = x - maxx(x) | |
| elif i == 'L': | |
| x = x - maxx(x) // 2 | |
| elif i == 'R': | |
| x = x + maxx(x) // 2 | |
| q = q - 1 | |
| print(x) | |
| ``` | |
| ### 113 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x + 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 114 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 3: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 115 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [1 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 3].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 116 | |
| ```python | |
| from math import * | |
| n, q = list(map(int, input().split())) | |
| depth = int(log2(n + 1)) | |
| def get_layer(u): | |
| for layer in range(depth): | |
| if u % 2 ** (layer + 1) == 2 ** layer: | |
| return layer | |
| def is_right_child(layer, u): | |
| return u % 2 ** (layer + 1) % 2 == 0 | |
| for x in range(q): | |
| u = int(input()) | |
| layer = get_layer(u) | |
| steps = input() | |
| for s in steps: | |
| if s == 'U': | |
| if layer == depth - 1: | |
| continue | |
| k = u // 2 ** (layer + 1) // 2 | |
| layer += 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif layer == 0: | |
| continue | |
| elif s == 'R': | |
| k = u // 2 ** (layer + 1) * 2 + 1 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k - 2 ** layer | |
| elif s == 'L': | |
| k = u // 2 ** (layer + 1) * 2 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| print(u) | |
| ``` | |
| ### 117 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| VERLIST = [] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| VERLIST.append(a) | |
| VERLIST.append(b) | |
| VERLIST = sorted(set(VERLIST)) | |
| import heapq | |
| ANS = [-1 << 50] * k | |
| for start in VERLIST: | |
| MINCOST = [1 << 50] * (n + 1) | |
| checking = [(0, start)] | |
| MINCOST[start] = 0 | |
| j = 0 | |
| while j < k: | |
| if not checking: | |
| break | |
| cost, checktown = heapq.heappop(checking) | |
| if cost >= -ANS[0]: | |
| break | |
| if MINCOST[checktown] < cost: | |
| continue | |
| if cost != 0 and checktown > start: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -cost) | |
| j += 1 | |
| for to, co in COST_vertex[checktown]: | |
| if MINCOST[to] >= cost + co: | |
| MINCOST[to] = cost + co | |
| heapq.heappush(checking, (cost + co, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 118 | |
| ```python | |
| def _vertex(lit): | |
| if lit > 0: | |
| return -1 * (lit - 1) | |
| else: | |
| return 2 * (-lit - 1) + 1 | |
| def tarjan(graph): | |
| n = len(graph) | |
| dfs_num = [None] * n | |
| dfs_min = [n] * n | |
| waiting = [] | |
| waits = [False] * n | |
| sccp = [] | |
| dfs_time = 0 | |
| times_seen = [-1] * n | |
| for start in range(n): | |
| if times_seen[start] == -1: | |
| times_seen[start] = 0 | |
| to_visit = [start] | |
| while to_visit: | |
| node = to_visit[-1] | |
| if times_seen[node] == 0: | |
| dfs_num[node] = dfs_time | |
| dfs_min[node] = dfs_time | |
| dfs_time += 1 | |
| waiting.append(node) | |
| waits[node] = True | |
| children = graph[node] | |
| if times_seen[node] == len(children): | |
| to_visit.pop() | |
| dfs_min[node] = dfs_num[node] | |
| for child in children: | |
| if waits[child] and dfs_min[child] < dfs_min[node]: | |
| dfs_min[node] = dfs_min[child] | |
| if dfs_min[node] == dfs_num[node]: | |
| component = [] | |
| while True: | |
| u = waiting.pop() | |
| waits[u] = False | |
| component.append(u) | |
| if u == node: | |
| break | |
| sccp.append(component) | |
| else: | |
| child = children[times_seen[node]] | |
| times_seen[node] += 1 | |
| if times_seen[child] == -1: | |
| times_seen[child] = 0 | |
| to_visit.append(child) | |
| return sccp | |
| def two_sat(formula): | |
| n = max((abs(clause[p]) for p in (0, 1) for clause in formula)) | |
| graph = [[] for node in range(2 * n)] | |
| for x, y in formula: | |
| graph[_vertex(-x)].append(_vertex(y)) | |
| graph[_vertex(-y)].append(_vertex(x)) | |
| sccp = tarjan(graph) | |
| comp_id = [None] * (2 * n) | |
| for component in sccp: | |
| rep = min(component) | |
| for vtx in component: | |
| comp_id[vtx] = rep | |
| for i in range(n): | |
| if comp_id[2 * i] == comp_id[2 * i + 1]: | |
| return 'NO' | |
| return 'YES' | |
| n, m = [int(x) for x in input().split()] | |
| doors_status = [int(x) for x in input().split()] | |
| switches = [list(map(int, input().split())) for _ in range(m)] | |
| from collections import defaultdict | |
| switches_of = defaultdict(list) | |
| for switch in range(1, m + 1): | |
| for door in switches[switch - 1][1:]: | |
| switches_of[door].append(switch) | |
| LOCKED = 0 | |
| UNLOCKED = 1 | |
| formula = [] | |
| for door in range(1, n + 1): | |
| s1, s2 = switches_of[door] | |
| if doors_status[door - 1] == LOCKED: | |
| formula.append((s1, s2)) | |
| formula.append((-s1, -s2)) | |
| else: | |
| formula.append((s1, -s2)) | |
| formula.append((-s1, s2)) | |
| print(two_sat(formula)) | |
| ``` | |
| ### 119 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - -1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 120 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| VERLIST = [] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| VERLIST.append(a) | |
| VERLIST.append(b) | |
| VERLIST = sorted(set(VERLIST)) | |
| import heapq | |
| ANS = [-1 << 50] * k | |
| for start in VERLIST: | |
| MINCOST = [1 << 50] * (n + 1) | |
| checking = [(0, start)] | |
| MINCOST[start] = 0 | |
| j = 1 | |
| while j < k: | |
| if not checking: | |
| break | |
| cost, checktown = heapq.heappop(checking) | |
| if cost >= -ANS[0]: | |
| break | |
| if MINCOST[checktown] < cost: | |
| continue | |
| if cost != 0 and checktown > start: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -cost) | |
| j += 1 | |
| for to, co in COST_vertex[checktown]: | |
| if MINCOST[to] > cost + co: | |
| MINCOST[to] = cost + co | |
| heapq.heappush(checking, (cost + co, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 121 | |
| ```python | |
| def maxx(n): | |
| return n & -n | |
| n, q = map(int, input().split()) | |
| root = n // 2 + 1 | |
| while q > 0: | |
| x = int(input()) | |
| s = input() | |
| for i in s: | |
| if i == 'U' and x != root: | |
| p = x * maxx(x) | |
| if x == p - maxx(p) // 2: | |
| x = p | |
| else: | |
| x = x - maxx(x) | |
| elif i == 'L': | |
| x = x - maxx(x) // 2 | |
| elif i == 'R': | |
| x = x + maxx(x) // 2 | |
| q = q - 1 | |
| print(x) | |
| ``` | |
| ### 122 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i - 1] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 123 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[2]) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1 + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (min(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 124 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] + 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 125 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(1, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 126 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2], reverse=True) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| for i in range(min(m, k)): | |
| x, y, c = EDGE[i] | |
| EDGE[i] = (c, x, y) | |
| USED_SET = set() | |
| ANS = [-1 << 50] * k | |
| import heapq | |
| while EDGE: | |
| c, x, y = heapq.heappop(EDGE) | |
| if (x, y) in USED_SET or c >= -ANS[0]: | |
| continue | |
| else: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -c) | |
| USED_SET.add((x, y)) | |
| USED_SET.add((y, x)) | |
| for to, cost in COST_vertex[x]: | |
| if to != y and (not (y, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, y, to)) | |
| for to, cost in COST_vertex[y]: | |
| if to != x and (not (x, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, x, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 127 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[2]) | |
| if k < m: | |
| maxDist = edgeList[min(m, k) - 1][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1 - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 128 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(0, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 129 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 130 | |
| ```python | |
| import math | |
| n, q = map(int, input().split()) | |
| maxh = int(math.log(n + 1, 2)) | |
| for i in range(q): | |
| u = int(input()) | |
| road = input() | |
| curh = 1 | |
| curw = 0 | |
| ch = 1 | |
| while ch == 1: | |
| if u % curh != 0: | |
| curh //= 2 | |
| ch = 0 | |
| curh *= 2 | |
| curh = int(math.log(curh, 2)) | |
| a = 2 << curh - 1 | |
| b = 1 << curh | |
| curw = (u - a) // b + 1 | |
| for move in road: | |
| if move == 'L': | |
| if curh != 1: | |
| curh -= 1 | |
| curw = curw * 2 - 1 | |
| u -= 1 << curh - 1 | |
| elif move == 'R': | |
| if curh != 1: | |
| curh -= 1 | |
| curw *= 2 | |
| u += 1 << curh - 1 | |
| elif curh != maxh: | |
| if curw % 2 == 1: | |
| u = (u + u + (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 + 1 | |
| else: | |
| u = (u + u - (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 | |
| print(u) | |
| ``` | |
| ### 131 | |
| ```python | |
| import heapq | |
| n, m, k = list(map(int, input().split())) | |
| connectionList = [] | |
| for _ in range(n): | |
| connectionList.append([]) | |
| edgeList = [] | |
| for _ in range(m): | |
| x, y, w = list(map(int, input().split())) | |
| edgeList.append((x, y, w)) | |
| edgeList.sort(key=lambda x: x[2]) | |
| if k < m: | |
| maxDist = edgeList[min(m, k) - 1 - 1][2] | |
| else: | |
| maxDist = sum([x[2] for x in edgeList]) | |
| colorList = {} | |
| colorVertex = [] | |
| for i in range(n): | |
| colorList[i] = [i] | |
| colorVertex.append(i) | |
| for i in range(min(m, k)): | |
| x, y, w = edgeList[i] | |
| connectionList[x - 1].append((y - 1, w)) | |
| connectionList[y - 1].append((x - 1, w)) | |
| if colorVertex[x - 1] != colorVertex[y - 1]: | |
| if len(colorList[colorVertex[x - 1]]) >= len(colorList[colorVertex[y - 1]]): | |
| prevColor = colorVertex[y - 1] | |
| for elem in colorList[colorVertex[y - 1]]: | |
| colorVertex[elem] = colorVertex[x - 1] | |
| colorList[colorVertex[x - 1]].append(elem) | |
| del colorList[prevColor] | |
| else: | |
| prevColor = colorVertex[x - 1] | |
| for elem in colorList[colorVertex[x - 1]]: | |
| colorVertex[elem] = colorVertex[y - 1] | |
| colorList[colorVertex[y - 1]].append(elem) | |
| del colorList[prevColor] | |
| pathList = [] | |
| for key in colorList: | |
| vertexList = colorList[key] | |
| for mainVertex in vertexList: | |
| vertexPQueue = [] | |
| isCovered = {} | |
| distanceDic = {} | |
| for elem in vertexList: | |
| isCovered[elem] = False | |
| distanceDic[elem] = maxDist | |
| isCovered[mainVertex] = True | |
| for elem in connectionList[mainVertex]: | |
| heapq.heappush(vertexPQueue, (elem[1], elem[0])) | |
| distanceDic[elem[0]] = elem[1] | |
| while vertexPQueue: | |
| distance, curVertex = heapq.heappop(vertexPQueue) | |
| if isCovered[curVertex]: | |
| continue | |
| elif distance >= maxDist: | |
| break | |
| for elem in connectionList[curVertex]: | |
| if distance + elem[1] < distanceDic[elem[0]]: | |
| heapq.heappush(vertexPQueue, (distance + elem[1], elem[0])) | |
| distanceDic[elem[0]] = distance + elem[1] | |
| for key in distanceDic: | |
| if distanceDic[key] <= maxDist and key > mainVertex: | |
| pathList.append(distanceDic[key]) | |
| if len(pathList) > k: | |
| pathList.sort() | |
| pathList = pathList[0:k] | |
| if pathList[-1] < maxDist: | |
| maxDist = pathList[-1] | |
| pathList.sort() | |
| print(pathList[k - 1]) | |
| ``` | |
| ### 132 | |
| ```python | |
| import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time | |
| sys.setrecursionlimit(10 ** 7) | |
| inf = 10 ** 20 | |
| mod = 10 ** 9 + 7 | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def LF(): | |
| return [float(x) for x in sys.stdin.readline().split()] | |
| def LS(): | |
| return sys.stdin.readline().split() | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def F(): | |
| return float(sys.stdin.readline()) | |
| def S(): | |
| return input() | |
| def main(): | |
| n, q = LI() | |
| nn = n | |
| k = 2 | |
| while nn > 0: | |
| nn //= 2 | |
| k += 1 | |
| r = [] | |
| ii = [2 ** i for i in range(k)] | |
| for _ in range(q): | |
| t = I() | |
| s = S() | |
| ti = 0 | |
| for i in range(k): | |
| if ii[i] & t: | |
| ti = i | |
| break | |
| for c in s: | |
| if c == 'U': | |
| if ti == k - 1: | |
| continue | |
| if t & ii[ti]: | |
| t ^= ii[ti] | |
| ti += 1 | |
| t |= ii[ti] | |
| continue | |
| if ti == 0: | |
| continue | |
| if c == 'R': | |
| t |= ii[ti] | |
| elif t & ii[ti]: | |
| t ^= ii[ti] | |
| ti -= 1 | |
| t |= ii[ti] | |
| r.append(t) | |
| return '\n'.join(map(str, r)) | |
| print(main()) | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r + 1])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 134 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0], reverse=True) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 135 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(3, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 136 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - -1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 137 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(-1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 138 | |
| ```python | |
| def maxx(n): | |
| return n & -n | |
| n, q = map(int, input().split()) | |
| root = n // 3 + 1 | |
| while q > 0: | |
| x = int(input()) | |
| s = input() | |
| for i in s: | |
| if i == 'U' and x != root: | |
| p = x + maxx(x) | |
| if x == p - maxx(p) // 2: | |
| x = p | |
| else: | |
| x = x - maxx(x) | |
| elif i == 'L': | |
| x = x - maxx(x) // 2 | |
| elif i == 'R': | |
| x = x + maxx(x) // 2 | |
| q = q - 1 | |
| print(x) | |
| ``` | |
| ### 139 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[-1] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 140 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-1, W)) | |
| ``` | |
| ### 141 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| VERLIST = [] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| VERLIST.append(a) | |
| VERLIST.append(b) | |
| VERLIST = sorted(set(VERLIST)) | |
| import heapq | |
| ANS = [-1 << 50] * k | |
| for start in VERLIST: | |
| MINCOST = [1 << 50] * (n + 1) | |
| checking = [(0, start)] | |
| MINCOST[start] = 0 | |
| j = 0 | |
| while j < k: | |
| if not checking: | |
| break | |
| cost, checktown = heapq.heappop(checking) | |
| if cost >= -ANS[0]: | |
| break | |
| if MINCOST[checktown] < cost: | |
| continue | |
| if cost != 0 and checktown > start: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -cost) | |
| j += 1 | |
| for to, co in COST_vertex[checktown]: | |
| if MINCOST[to] > cost - co: | |
| MINCOST[to] = cost + co | |
| heapq.heappush(checking, (cost + co, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 142 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-5) | |
| ``` | |
| ### 143 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[0]] = True | |
| queue.append(i[0]) | |
| if i[0] == 1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == 1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 144 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 3: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 145 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1 - 1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 146 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(1) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(0) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 147 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else --1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 148 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if not z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 149 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(4) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 150 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i - 1] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 151 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] >= upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 152 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1 - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 153 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount -= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 154 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (-1, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(1) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 156 | |
| ```python | |
| from collections import defaultdict, deque | |
| n = int(input()) | |
| adj = [[] for _ in range(n)] | |
| v = [0] * n | |
| l = list(map(int, input().split())) | |
| for i, f in enumerate(l): | |
| adj[f - 1].append(i + 1) | |
| s = list(map(int, input().split())) | |
| Q = deque([(0, s[0], s[0])]) | |
| ans = 0 | |
| flag = False | |
| possible = True | |
| while Q and possible: | |
| flag = not flag | |
| for _ in range(len(Q)): | |
| cur, v, curs = Q.popleft() | |
| if v < 0: | |
| possible = False | |
| ans = -1 | |
| break | |
| ans += v | |
| if flag: | |
| for i in adj[cur]: | |
| if len(adj[i]) <= 1: | |
| Q.append((i, 0, curs)) | |
| else: | |
| temp = min([s[k] for k in adj[i]]) | |
| Q.append((i, temp - curs, temp)) | |
| else: | |
| for i in adj[cur]: | |
| Q.append((i, s[i] - curs, s[i - 1])) | |
| print(ans) | |
| ``` | |
| ### 157 | |
| ```python | |
| from collections import defaultdict, deque | |
| n, m = list(map(int, input().split())) | |
| state = list(map(int, input().split())) | |
| graph = defaultdict(list) | |
| connected = [list() for _ in range(n + 1)] | |
| for i in range(m): | |
| for j in list(map(int, input().split()))[1:]: | |
| connected[j].append(i) | |
| for index, i in enumerate(connected[1:]): | |
| graph[i[0]].append((i[1], state[index])) | |
| graph[i[1]].append((i[0], state[index])) | |
| switch_color = [False] * m | |
| switch_visited = [False] * m | |
| queue = deque([]) | |
| yes = True | |
| for nodes in graph: | |
| if yes and (not switch_visited[nodes]): | |
| queue.append(nodes) | |
| while queue and yes: | |
| now = queue.popleft() | |
| switch_visited[now] = True | |
| for i in graph[now]: | |
| if not switch_visited[i[0]]: | |
| switch_visited[i[0]] = True | |
| queue.append(i[0]) | |
| if i[1] == 1: | |
| switch_color[i[0]] = switch_color[now] | |
| else: | |
| switch_color[i[0]] = not switch_color[now] | |
| elif i[1] == -1 and switch_color[i[0]] != switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| elif i[1] == 0 and switch_color[i[0]] == switch_color[now]: | |
| print('NO') | |
| yes = False | |
| break | |
| if yes: | |
| print('YES') | |
| ``` | |
| ### 158 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N, M, K = list(map(int, input().split())) | |
| X = [] | |
| for _ in range(M): | |
| x, y, w = list(map(int, input().split())) | |
| X.append([min(x, y), max(x, y), w]) | |
| X = (sorted(X, key=lambda x: x[0]) + [[0, 0, 10 ** 20] for _ in range(K)])[:K] | |
| D = {} | |
| for x, y, w in X: | |
| if x: | |
| D[x * 10 ** 6 + y] = w | |
| flg = 1 | |
| while flg: | |
| flg = 0 | |
| for i in range(len(X)): | |
| x1, y1, w1 = X[i] | |
| for j in range(i + 1, len(X)): | |
| x2, y2, w2 = X[j] | |
| if x1 == x2: | |
| a, b = (min(y1, y2), max(y1, y2)) | |
| elif y1 == y2: | |
| a, b = (min(x1, x2), max(x1, x2)) | |
| elif x1 == y2: | |
| a, b = (x2, y1) | |
| elif x2 == y1: | |
| a, b = (x1, y2) | |
| else: | |
| a, b = (0, 0) | |
| if a: | |
| if a * 10 ** 6 + b in D and w1 + w2 < D[a * 10 ** 6 + b] or (a * 10 ** 6 + b not in D and w1 + w2 < X[-1][2]): | |
| if a * 10 ** 6 + b in D: | |
| for k in range(len(X)): | |
| if X[k][0] == a and X[k][1] == b: | |
| X[k][2] = w1 + w2 | |
| else: | |
| x, y, w = X.pop() | |
| if x: | |
| D.pop(x * 10 ** 6 + y) | |
| X.append([a, b, w1 + w2]) | |
| D[a * 10 ** 6 + b] = w1 + w2 | |
| X = sorted(X, key=lambda x: x[2]) | |
| flg = 1 | |
| print(X[-1][2]) | |
| ``` | |
| ### 159 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count *= numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 160 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [-1 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 1].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| n, m, k = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGE.sort(key=lambda x: x[2]) | |
| EDGE = EDGE[:k] | |
| COST_vertex = [[] for i in range(n + 1)] | |
| for a, b, c in EDGE: | |
| COST_vertex[a].append((b, c)) | |
| COST_vertex[b].append((a, c)) | |
| for i in range(min(m, k)): | |
| x, y, c = EDGE[i] | |
| EDGE[i] = (c, x, y) | |
| USED_SET = set() | |
| ANS = [-1 << 50] * k | |
| import heapq | |
| while EDGE: | |
| c, x, y = heapq.heappop(EDGE) | |
| if (x, y) in USED_SET or c >= -ANS[0]: | |
| continue | |
| else: | |
| heapq.heappop(ANS) | |
| heapq.heappush(ANS, -c) | |
| USED_SET.add((x, y)) | |
| USED_SET.add((y, x)) | |
| for to, cost in COST_vertex[x]: | |
| if to != y and (not (y, to) in USED_SET) and (c + cost < -ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, y, to)) | |
| for to, cost in COST_vertex[y]: | |
| if to != x and (not (x, to) in USED_SET) and (c + cost < +ANS[0]): | |
| heapq.heappush(EDGE, (c + cost, x, to)) | |
| print(-ANS[0]) | |
| ``` | |
| ### 162 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i - 1] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 163 | |
| ```python | |
| from math import * | |
| n, q = list(map(int, input().split())) | |
| depth = int(log2(n + 1)) | |
| def get_layer(u): | |
| for layer in range(depth): | |
| if u % 2 ** (layer + 1) == 2 ** layer: | |
| return layer | |
| def is_right_child(layer, u): | |
| return u % 2 ** (layer + 1) % 2 == 0 | |
| for x in range(q): | |
| u = int(input()) | |
| layer = get_layer(u) | |
| steps = input() | |
| for s in steps: | |
| if s == 'U': | |
| if layer == depth - 1: | |
| continue | |
| k = u // 2 ** (layer + 1) // 2 | |
| layer += 1 | |
| u = 2 ** (layer + 1) * k - 2 ** layer | |
| elif layer == 0: | |
| continue | |
| elif s == 'R': | |
| k = u // 2 ** (layer + 1) * 2 + 1 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif s == 'L': | |
| k = u // 2 ** (layer + 1) * 2 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| print(u) | |
| ``` | |
| ### 164 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 3: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 165 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1 - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 166 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += -1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 167 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-3) | |
| ``` | |
| ### 168 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in reversed(range(len(ops))): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 169 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 4 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 170 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key + 1] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 171 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == -3: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 172 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + -1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 173 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1 + 1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 174 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = -1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 175 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += -1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 176 | |
| ```python | |
| import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time | |
| sys.setrecursionlimit(10 ** 7) | |
| inf = 10 ** 20 | |
| mod = 10 ** 9 + 7 | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def LF(): | |
| return [float(x) for x in sys.stdin.readline().split()] | |
| def LS(): | |
| return sys.stdin.readline().split() | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def F(): | |
| return float(sys.stdin.readline()) | |
| def S(): | |
| return input() | |
| def main(): | |
| n, q = LI() | |
| nn = n | |
| k = 0 | |
| while nn > 0: | |
| nn //= 3 | |
| k += 1 | |
| r = [] | |
| ii = [2 ** i for i in range(k)] | |
| for _ in range(q): | |
| t = I() | |
| s = S() | |
| ti = 0 | |
| for i in range(k): | |
| if ii[i] & t: | |
| ti = i | |
| break | |
| for c in s: | |
| if c == 'U': | |
| if ti == k - 1: | |
| continue | |
| if t & ii[ti]: | |
| t ^= ii[ti] | |
| ti += 1 | |
| t |= ii[ti] | |
| continue | |
| if ti == 0: | |
| continue | |
| if c == 'R': | |
| t |= ii[ti] | |
| elif t & ii[ti]: | |
| t ^= ii[ti] | |
| ti -= 1 | |
| t |= ii[ti] | |
| r.append(t) | |
| return '\n'.join(map(str, r)) | |
| print(main()) | |
| ``` | |
| ### 177 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i * 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 178 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i - 1]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 179 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 4, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 180 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(2, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 181 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key - 1] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 182 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(2, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 183 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -4): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 184 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-2) | |
| ``` | |
| ### 185 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 2 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 186 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 0) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 187 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1 - 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 188 | |
| ```python | |
| import sys | |
| from collections import deque | |
| def debug(x, table): | |
| for name, val in table.items(): | |
| if x is val: | |
| print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) | |
| return None | |
| def solve(): | |
| n, m = map(int, sys.stdin.readline().split()) | |
| r = [0 - int(i) for i in sys.stdin.readline().split()] | |
| es = [[] for i in range(n)] | |
| for i in range(m): | |
| line = [int(j) for j in sys.stdin.readline().split()] | |
| for u in line[1:]: | |
| es[u - 1].append(i) | |
| Adj = [[] for i in range(m)] | |
| for u, v in es: | |
| Adj[u].append(v) | |
| Adj[v].append(u) | |
| edges = dict() | |
| for i, e in enumerate(es): | |
| e.sort() | |
| if tuple(e) not in edges: | |
| edges[tuple(e)] = r[i] | |
| elif edges[tuple(e)] != r[i]: | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| cols = [None] * m | |
| for u in range(m): | |
| if cols[u] is None: | |
| if not bfs(Adj, edges, cols, u): | |
| print('NO') | |
| return None | |
| else: | |
| pass | |
| print('YES') | |
| def bfs(Adj, edges, cols, u): | |
| nxts = deque([u]) | |
| cols[u] = 0 | |
| while nxts: | |
| v = nxts.popleft() | |
| for w in Adj[v]: | |
| ed = tuple(sorted([v, w])) | |
| if cols[w] is None: | |
| cols[w] = cols[v] ^ edges[ed] | |
| nxts.append(w) | |
| elif cols[w] ^ cols[v] != edges[ed]: | |
| return False | |
| return True | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 189 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = min(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key] + 1] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 190 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1 - 1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 191 | |
| ```python | |
| n = int(input()) | |
| class Tree: | |
| height = 1 | |
| def __init__(self, parent=None): | |
| self.nodes = [] | |
| self.s = -1 | |
| self.a = -1 | |
| self.parent = parent | |
| if parent: | |
| self.height = parent.height + 1 | |
| parent.nodes.append(self) | |
| trs = [Tree()] | |
| for i in map(int, input().split()): | |
| trs.append(Tree(trs[i - 1])) | |
| for i, s in enumerate(map(int, input().split())): | |
| trs[i].s = s | |
| trs.sort(key=lambda x: x.height) | |
| trs[0].a = trs[0].s | |
| res = trs[0].a | |
| try: | |
| for t in trs[1:]: | |
| if t.s == -1: | |
| t.a = min([x.s - t.parent.s for x in t.nodes], default=0) | |
| t.s = t.a + t.parent.s | |
| else: | |
| t.a = t.s - t.parent.s | |
| if t.a < 0: | |
| raise | |
| res += t.a | |
| print(res) | |
| except: | |
| print(-4) | |
| ``` | |
| ### 192 | |
| ```python | |
| import math | |
| n, q = map(int, input().split()) | |
| maxh = int(math.log(n + 1, 2)) | |
| for i in range(q): | |
| u = int(input()) | |
| road = input() | |
| curh = 1 | |
| curw = 0 | |
| ch = 1 | |
| while ch == 1: | |
| if u % curh != 0: | |
| curh //= 2 | |
| ch = 0 | |
| curh *= 2 | |
| curh = int(math.log(curh, 2)) | |
| a = -1 << curh - 1 | |
| b = 1 << curh | |
| curw = (u - a) // b + 1 | |
| for move in road: | |
| if move == 'L': | |
| if curh != 1: | |
| curh -= 1 | |
| curw = curw * 2 - 1 | |
| u -= 1 << curh - 1 | |
| elif move == 'R': | |
| if curh != 1: | |
| curh -= 1 | |
| curw *= 2 | |
| u += 1 << curh - 1 | |
| elif curh != maxh: | |
| if curw % 2 == 1: | |
| u = (u + u + (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 + 1 | |
| else: | |
| u = (u + u - (1 << curh)) // 2 | |
| curh += 1 | |
| curw = curw // 2 | |
| print(u) | |
| ``` | |
| ### 193 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key - 1] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 194 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {2: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 195 | |
| ```python | |
| from collections import deque | |
| def solve(p, s): | |
| children = {} | |
| parents = {} | |
| for i, e in enumerate(p): | |
| if e not in children: | |
| children[e] = [] | |
| children[e].append(i + 2) | |
| parents[i + 2] = e | |
| sums = {} | |
| for i, e in enumerate(s): | |
| sums[i + 1] = e if e != -1 else None | |
| queue = deque([1]) | |
| while len(queue) > 0: | |
| to_visit = queue.popleft() | |
| if to_visit in children: | |
| for child in children[to_visit]: | |
| queue.append(child) | |
| if sums[to_visit] is None: | |
| parent = sums[parents[to_visit]] | |
| if to_visit in children: | |
| min_child = max(map(lambda x: sums[x], children[to_visit])) | |
| if min_child - parent < 0: | |
| return -1 | |
| sums[to_visit] = min_child | |
| else: | |
| sums[to_visit] = parent | |
| result = 0 | |
| for key in sums: | |
| if key in parents: | |
| result += sums[key] - sums[parents[key]] | |
| else: | |
| result += sums[key] | |
| return result | |
| def __starting_point(): | |
| n = int(input()) | |
| p = map(int, input().split()) | |
| s = map(int, input().split()) | |
| print(solve(p, s)) | |
| __starting_point() | |
| ``` | |
| ### 196 | |
| ```python | |
| import sys | |
| n, m = map(int, input().split()) | |
| door = list(map(int, input().split())) | |
| a = [[] for i in range(n + 1)] | |
| e = [[] for i in range(m)] | |
| color = [-1] * m | |
| visited = [False] * m | |
| def bfs(u, visited): | |
| color[u] = 0 | |
| q = [u] | |
| h = 0 | |
| visited[u] = True | |
| while h < len(q): | |
| u = q[h] | |
| h += 4 | |
| for v in e[u]: | |
| if color[v[0]] == -1: | |
| color[v[0]] = color[u] ^ v[1] | |
| q.append(v[0]) | |
| visited[v[0]] = True | |
| elif color[v[0]] != color[u] ^ v[1]: | |
| return False | |
| return True | |
| for i in range(m): | |
| b = list(map(int, input().split())) | |
| for j in range(1, len(b)): | |
| k = b[j] | |
| a[k].append(i) | |
| if len(a[k]) == 2: | |
| e[a[k][0]].append((a[k][1], 1 - door[k - 1])) | |
| e[a[k][1]].append((a[k][0], 1 - door[k - 1])) | |
| flag = True | |
| for i in range(m): | |
| if color[i] == -1: | |
| if bfs(i, visited) == False: | |
| flag = False | |
| break | |
| if flag: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 197 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag - 1] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 198 | |
| ```python | |
| from math import * | |
| n, q = list(map(int, input().split())) | |
| depth = int(log2(n + 1)) | |
| def get_layer(u): | |
| for layer in range(depth): | |
| if u % 2 ** (layer + 1) == 2 ** layer: | |
| return layer | |
| def is_right_child(layer, u): | |
| return u % 2 ** (layer + 1) % 2 == 0 | |
| for x in range(q): | |
| u = int(input()) | |
| layer = get_layer(u) | |
| steps = input() | |
| for s in steps: | |
| if s == 'U': | |
| if layer == depth - 2: | |
| continue | |
| k = u // 2 ** (layer + 1) // 2 | |
| layer += 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif layer == 0: | |
| continue | |
| elif s == 'R': | |
| k = u // 2 ** (layer + 1) * 2 + 1 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| elif s == 'L': | |
| k = u // 2 ** (layer + 1) * 2 | |
| layer -= 1 | |
| u = 2 ** (layer + 1) * k + 2 ** layer | |
| print(u) | |
| ``` | |
| ### 199 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 200 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m - 1): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |