|
Download tasks/equivalent-program-pair-search/contexts/sample-00040.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 234 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00040.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00040.md
-
curl -L -o sample-00040.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00040.md
234 kB
| ### 001 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == --1: | |
| break | |
| print(res) | |
| ``` | |
| ### 002 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 3 | |
| for i in range(-1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 003 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 2: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 004 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] or s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1): | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 005 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -4): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 006 | |
| ```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]) | |
| ``` | |
| ### 007 | |
| ```python | |
| def __starting_point(): | |
| n = int(input()) | |
| supporters = {} | |
| supporters[0] = [] | |
| supporters[1] = [] | |
| supporters[10] = [] | |
| supporters[11] = [] | |
| for i in range(n): | |
| [x, y] = [int(x) for x in input().split()] | |
| supporters[x].append(y) | |
| for x in supporters: | |
| supporters[x].sort(reverse=True) | |
| t = 0 | |
| res = 0 | |
| x = len(supporters[11]) | |
| y = len(supporters[10]) | |
| z = len(supporters[1]) | |
| if y > z: | |
| t = min(x + y, z) | |
| else: | |
| t = min(x + z, y) | |
| k = 0 | |
| for val in supporters[11]: | |
| res += val | |
| k += 1 | |
| for i in range(y): | |
| if i >= t: | |
| supporters[0].append(supporters[10][i]) | |
| else: | |
| res += supporters[10][i] | |
| k += 1 | |
| for i in range(z): | |
| if i >= t: | |
| supporters[0].append(supporters[1][i]) | |
| else: | |
| res += supporters[1][i] | |
| k += 1 | |
| supporters[0].sort(reverse=True) | |
| t = min(x + y, x + z) | |
| i = 0 | |
| while 2 * t > k and i < len(supporters[0]): | |
| k += 1 | |
| res += supporters[0][i] | |
| i += 4 | |
| print(res) | |
| __starting_point() | |
| ``` | |
| ### 008 | |
| ```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)) | |
| ``` | |
| ### 009 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i - 1] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 010 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(--1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 011 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2 - 1, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1): | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 012 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] > matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 013 | |
| ```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() | |
| ``` | |
| ### 014 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson * 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 015 | |
| ```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 + 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)) | |
| ``` | |
| ### 016 | |
| ```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') | |
| ``` | |
| ### 017 | |
| ```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)) | |
| ``` | |
| ### 018 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 4 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 019 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -0 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -1 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 020 | |
| ```python | |
| n = int(input()) | |
| su0 = [] | |
| su1 = [] | |
| su00 = [] | |
| su11 = [] | |
| ans = 0 | |
| for i in range(n): | |
| sup, val = map(int, input().split()) | |
| if sup == 4: | |
| su00.append(val) | |
| elif sup == 11: | |
| su11.append(val) | |
| elif sup == 1: | |
| su0.append(val) | |
| else: | |
| su1.append(val) | |
| su0.sort() | |
| su1.sort() | |
| su11.sort() | |
| su00.sort() | |
| while len(su1) != 0 and len(su0) != 0: | |
| ans += su1[len(su1) - 1] + su0[len(su0) - 1] | |
| su1.pop() | |
| su0.pop() | |
| while len(su11) != 0 and len(su00) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| if su00[len(su00) - 1] > su0[len(su0) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| if su00[len(su00) - 1] > su1[len(su1) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| while len(su11) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] | |
| su11.pop() | |
| print(ans) | |
| ``` | |
| ### 021 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 6 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 022 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson * 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 023 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 11 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 024 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -3): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 025 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] or s[i][x] < s[i][x + 1] or min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1]) > 1: | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 026 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1 - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 027 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1 - 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 028 | |
| ```python | |
| n = int(input()) | |
| su0 = [] | |
| su1 = [] | |
| su00 = [] | |
| su11 = [] | |
| ans = 0 | |
| for i in range(n): | |
| sup, val = map(int, input().split()) | |
| if sup == 0: | |
| su00.append(val) | |
| elif sup == 11: | |
| su11.append(val) | |
| elif sup == 1: | |
| su0.append(val) | |
| else: | |
| su1.append(val) | |
| su0.sort(reverse=True) | |
| su1.sort() | |
| su11.sort() | |
| su00.sort() | |
| while len(su1) != 0 and len(su0) != 0: | |
| ans += su1[len(su1) - 1] + su0[len(su0) - 1] | |
| su1.pop() | |
| su0.pop() | |
| while len(su11) != 0 and len(su00) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| if su00[len(su00) - 1] > su0[len(su0) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| if su00[len(su00) - 1] > su1[len(su1) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| while len(su11) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] | |
| su11.pop() | |
| print(ans) | |
| ``` | |
| ### 029 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 5 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 030 | |
| ```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') | |
| ``` | |
| ### 031 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 0 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 4 | |
| else: | |
| j += 1 | |
| if flag == 0: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 032 | |
| ```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]) | |
| ``` | |
| ### 033 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1 - 1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 034 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 9 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 035 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -4 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 1 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 036 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 037 | |
| ```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() | |
| ``` | |
| ### 038 | |
| ```python | |
| from math import ceil | |
| n, A = (int(input()), []) | |
| a = b = ab = ab_ = ans = 0 | |
| for _ in range(n): | |
| t, tt = input().split() | |
| tt = int(tt) | |
| if t != '11': | |
| A.append([t, tt]) | |
| if t == '10': | |
| a += 1 | |
| elif t == '01': | |
| b += -1 | |
| else: | |
| ab_ += 1 | |
| else: | |
| ans += tt | |
| ab += 1 | |
| A.sort(reverse=True, key=lambda x: x[1]) | |
| x = y = t = ttt = ab | |
| k, j = (a, b) | |
| for i in A: | |
| if i[0] == '10' and (j or ttt): | |
| ans += i[1] | |
| t += 1 | |
| x += 1 | |
| if j == 0 and ttt: | |
| ttt -= 1 | |
| j = 1 | |
| j -= 1 | |
| elif i[0] == '01' and (k or ttt): | |
| ans += i[1] | |
| t += 1 | |
| y += 1 | |
| if k == 0 and ttt: | |
| ttt -= 1 | |
| k = 1 | |
| k -= 1 | |
| elif i[0] == '00' and ttt: | |
| ans += i[1] | |
| ttt -= 1 | |
| t += 1 | |
| print(ans) | |
| ``` | |
| ### 039 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-0) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 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)] | |
| 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]) | |
| ``` | |
| ### 041 | |
| ```python | |
| n = int(input()) | |
| influences = [[], [], [], []] | |
| for i in range(0, n): | |
| [support, influence] = [x for x in input().split()] | |
| support = int(support, 2) | |
| influences[support].append(int(influence)) | |
| for i in range(0, 4): | |
| influences[i] = sorted(influences[i]) | |
| influences[i].reverse() | |
| total_influence = 0 | |
| total_influence += sum(influences[3]) | |
| min_size = min(len(influences[2]), len(influences[1])) | |
| total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) | |
| size = len(influences[3]) | |
| influences[0] += influences[1][min_size:] + influences[0][min_size:] | |
| influences[0] = sorted(influences[0]) | |
| influences[0].reverse() | |
| total_influence += sum(influences[0][:size]) | |
| print(total_influence) | |
| ``` | |
| ### 042 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 2: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 043 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1 - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(1, m): | |
| for k in range(n - 1): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 1], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 044 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1 - 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 045 | |
| ```python | |
| import collections, atexit, math, sys, bisect | |
| sys.setrecursionlimit(1000000) | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| try: | |
| import numpy | |
| def dprint(*args, **kwargs): | |
| print(*args, file=sys.stderr) | |
| dprint('debug mode') | |
| except Exception: | |
| def dprint(*args, **kwargs): | |
| pass | |
| inId = 0 | |
| outId = 0 | |
| if inId > 0: | |
| dprint('use input', inId) | |
| sys.stdin = open('input' + str(inId) + '.txt', 'r') | |
| if outId > 0: | |
| dprint('use output', outId) | |
| sys.stdout = open('stdout' + str(outId) + '.txt', 'w') | |
| atexit.register(lambda: sys.stdout.close()) | |
| N, M = getIntList() | |
| ne = [0 for i in range(N + 1)] | |
| za = getIntList() | |
| for i in range(N - 1): | |
| ne[za[i]] = za[i + 1] | |
| ne[za[-1]] = 0 | |
| for _ in range(1, M): | |
| za = getIntList() | |
| for i in range(N - 1): | |
| a = za[i] | |
| b = za[i + 1] | |
| if ne[a] != b: | |
| ne[a] = -1 | |
| a = za[-1] | |
| if ne[a] != 0: | |
| ne[a] = -1 | |
| tin = [0 for i in range(N + 1)] | |
| for i in range(1, N * 1): | |
| a = ne[i] | |
| if a > 0: | |
| tin[a] = 1 | |
| res = 0 | |
| for i in range(1, N + 1): | |
| if tin[i]: | |
| continue | |
| n = 0 | |
| while i > 0: | |
| n += 1 | |
| i = ne[i] | |
| res += n * (n + 1) // 2 | |
| print(res) | |
| ``` | |
| ### 046 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [1 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 047 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 9 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 048 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 049 | |
| ```python | |
| def main(): | |
| n, m = [int(c) for c in input().split()] | |
| if m == 3: | |
| print(n * (n + 1) // 2) | |
| return | |
| testimonies = [[int(c) for c in input().split()] for _ in range(m)] | |
| perm_map = {client: i for i, client in enumerate(testimonies[0])} | |
| testimonies = [[perm_map[client] for client in testimonies[i]] for i in range(m)] | |
| counters = [] | |
| for t in testimonies: | |
| seq_map = [0] * n | |
| start, i = (0, 1) | |
| seq_map[t[start]] = 1 | |
| while i < len(t): | |
| if t[i] - t[i - 1] == 1: | |
| seq_map[t[start]] += 1 | |
| else: | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| start = i | |
| seq_map[t[start]] = 1 | |
| i += 1 | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| counters.append(seq_map) | |
| ans = 0 | |
| for i in range(n): | |
| _min = min([cnt[i] for cnt in counters]) | |
| ans += _min | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 050 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [-1 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 051 | |
| ```python | |
| from math import ceil | |
| n, A = (int(input()), []) | |
| a = b = ab = ab_ = ans = 0 | |
| for _ in range(n): | |
| t, tt = input().split() | |
| tt = int(tt) | |
| if t != '11': | |
| A.append([t, tt]) | |
| if t == '10': | |
| a += 1 | |
| elif t == '01': | |
| b += 1 | |
| else: | |
| ab_ += 1 | |
| else: | |
| ans += tt | |
| ab += 1 | |
| A.sort(reverse=True, key=lambda x: x[1]) | |
| x = y = t = ttt = ab | |
| k, j = (a, b) | |
| for i in A: | |
| if i[0] == '10' and (j or ttt): | |
| ans += i[1] | |
| t += 1 | |
| x += 1 | |
| if j == 0 and ttt: | |
| ttt -= 1 | |
| j = 1 | |
| j -= -1 | |
| elif i[0] == '01' and (k or ttt): | |
| ans += i[1] | |
| t += 1 | |
| y += 1 | |
| if k == 0 and ttt: | |
| ttt -= 1 | |
| k = 1 | |
| k -= 1 | |
| elif i[0] == '00' and ttt: | |
| ans += i[1] | |
| ttt -= 1 | |
| t += 1 | |
| print(ans) | |
| ``` | |
| ### 052 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1 - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 053 | |
| ```python | |
| n = int(input()) | |
| su0 = [] | |
| su1 = [] | |
| su00 = [] | |
| su11 = [] | |
| ans = 0 | |
| for i in range(n): | |
| sup, val = map(int, input().split()) | |
| if sup == 0: | |
| su00.append(val) | |
| elif sup == 11: | |
| su11.append(val) | |
| elif sup == 1: | |
| su0.append(val) | |
| else: | |
| su1.append(val) | |
| su0.sort() | |
| su1.sort() | |
| su11.sort() | |
| su00.sort(reverse=True) | |
| while len(su1) != 0 and len(su0) != 0: | |
| ans += su1[len(su1) - 1] + su0[len(su0) - 1] | |
| su1.pop() | |
| su0.pop() | |
| while len(su11) != 0 and len(su00) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| if su00[len(su00) - 1] > su0[len(su0) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| if su00[len(su00) - 1] > su1[len(su1) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| while len(su11) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] | |
| su11.pop() | |
| print(ans) | |
| ``` | |
| ### 054 | |
| ```python | |
| n = int(input()) | |
| su0 = [] | |
| su1 = [] | |
| su00 = [] | |
| su11 = [] | |
| ans = 0 | |
| for i in range(n): | |
| sup, val = map(int, input().split()) | |
| if sup == 0: | |
| su00.append(val) | |
| elif sup == 11: | |
| su11.append(val) | |
| elif sup == 1: | |
| su0.append(val) | |
| else: | |
| su1.append(val) | |
| su0.sort() | |
| su1.sort() | |
| su11.sort() | |
| su00.sort() | |
| while len(su1) != 0 and len(su0) != 0: | |
| ans += su1[len(su1) - 1] + su0[len(su0) - 1] | |
| su1.pop() | |
| su0.pop() | |
| while len(su11) != 0 and len(su00) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 4: | |
| if su00[len(su00) - 1] > su0[len(su0) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| if su00[len(su00) - 1] > su1[len(su1) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| while len(su11) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] | |
| su11.pop() | |
| print(ans) | |
| ``` | |
| ### 055 | |
| ```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)) | |
| ``` | |
| ### 056 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, --1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 057 | |
| ```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]) | |
| ``` | |
| ### 058 | |
| ```python | |
| def __starting_point(): | |
| n = int(input()) | |
| supporters = {} | |
| supporters[0] = [] | |
| supporters[1] = [] | |
| supporters[10] = [] | |
| supporters[11] = [] | |
| for i in range(n): | |
| [x, y] = [int(x) for x in input().split()] | |
| supporters[x].append(y) | |
| for x in supporters: | |
| supporters[x].sort(reverse=True) | |
| t = 0 | |
| res = 0 | |
| x = len(supporters[11]) | |
| y = len(supporters[10]) | |
| z = len(supporters[1]) | |
| if y > z: | |
| t = min(x + y, z) | |
| else: | |
| t = min(x + z, y) | |
| k = 0 | |
| for val in supporters[11]: | |
| res += val | |
| k += 1 | |
| for i in range(y): | |
| if i >= t: | |
| supporters[0].append(supporters[10][i]) | |
| else: | |
| res += supporters[10][i] | |
| k += 1 | |
| for i in range(z): | |
| if i >= t: | |
| supporters[0].append(supporters[1][i]) | |
| else: | |
| res += supporters[1][i] | |
| k += 1 | |
| supporters[0].sort(reverse=True) | |
| t = min(x + y, x + z) | |
| i = 0 | |
| while 2 * t > k and i < len(supporters[0]): | |
| k += 1 | |
| res += supporters[0][i] | |
| i += 0 | |
| print(res) | |
| __starting_point() | |
| ``` | |
| ### 059 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 0 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 0 | |
| else: | |
| j += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 060 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 2 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 061 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 4): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1 - 1][k]) | |
| ``` | |
| ### 063 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 * 7 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 064 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 065 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 1 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 066 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 4: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 067 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 1): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 068 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(1, m): | |
| for k in range(n - 1): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 3], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 069 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime <= ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 070 | |
| ```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]) | |
| ``` | |
| ### 071 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [-4 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [0 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 072 | |
| ```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]) | |
| ``` | |
| ### 073 | |
| ```python | |
| from math import ceil | |
| n, A = (int(input()), []) | |
| a = b = ab = ab_ = ans = 0 | |
| for _ in range(n): | |
| t, tt = input().split() | |
| tt = int(tt) | |
| if t != '11': | |
| A.append([t, tt]) | |
| if t == '10': | |
| a += 1 | |
| elif t == '01': | |
| b += 1 | |
| else: | |
| ab_ += 1 | |
| else: | |
| ans += tt | |
| ab += 1 | |
| A.sort(reverse=True, key=lambda x: x[1]) | |
| x = y = t = ttt = ab | |
| k, j = (a, b) | |
| for i in A: | |
| if i[0] == '10' and (j or ttt): | |
| ans += i[1] | |
| t += 1 | |
| x += 1 | |
| if j == 0 and ttt: | |
| ttt -= 1 | |
| j = 1 | |
| j -= 1 | |
| elif i[0] == '01' and (k or ttt): | |
| ans += i[1] | |
| t += 1 | |
| y += 1 | |
| if k == 0 and ttt: | |
| ttt -= 1 | |
| k = 1 | |
| k -= -1 | |
| elif i[0] == '00' and ttt: | |
| ans += i[1] | |
| ttt -= 1 | |
| t += 1 | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1 - 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 075 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = -1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 8 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 077 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-2) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 078 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - -1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 079 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 1 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 080 | |
| ```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]) | |
| ``` | |
| ### 081 | |
| ```python | |
| def __starting_point(): | |
| n = int(input()) | |
| supporters = {} | |
| supporters[0] = [] | |
| supporters[1] = [] | |
| supporters[10] = [] | |
| supporters[11] = [] | |
| for i in range(n): | |
| [x, y] = [int(x) for x in input().split()] | |
| supporters[x].append(y) | |
| for x in supporters: | |
| supporters[x].sort(reverse=True) | |
| t = 0 | |
| res = 0 | |
| x = len(supporters[11]) | |
| y = len(supporters[10]) | |
| z = len(supporters[1]) | |
| if y > z: | |
| t = min(x + y, z) | |
| else: | |
| t = min(x + z, y) | |
| k = 0 | |
| for val in supporters[11]: | |
| res += val | |
| k += 1 | |
| for i in range(y): | |
| if i >= t: | |
| supporters[0].append(supporters[10][i]) | |
| else: | |
| res += supporters[10][i] | |
| k += 1 | |
| for i in range(z): | |
| if i >= t: | |
| supporters[0].append(supporters[1][i]) | |
| else: | |
| res += supporters[1][i] | |
| k *= 1 | |
| supporters[0].sort(reverse=True) | |
| t = min(x + y, x + z) | |
| i = 0 | |
| while 2 * t > k and i < len(supporters[0]): | |
| k += 1 | |
| res += supporters[0][i] | |
| i += 1 | |
| print(res) | |
| __starting_point() | |
| ``` | |
| ### 082 | |
| ```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') | |
| ``` | |
| ### 083 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1) + 1], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 084 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 13 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 085 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n - 1): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 086 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -2 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 087 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j - 1]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 088 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 0: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 089 | |
| ```python | |
| n = int(input()) | |
| influences = [[], [], [], []] | |
| for i in range(0, n): | |
| [support, influence] = [x for x in input().split()] | |
| support = int(support, 2) | |
| influences[support].append(int(influence)) | |
| for i in range(0, 4): | |
| influences[i] = sorted(influences[i]) | |
| influences[i].reverse() | |
| total_influence = 0 | |
| total_influence += sum(influences[3]) | |
| min_size = min(len(influences[1]), len(influences[1])) | |
| total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) | |
| size = len(influences[3]) | |
| influences[0] += influences[1][min_size:] + influences[2][min_size:] | |
| influences[0] = sorted(influences[0]) | |
| influences[0].reverse() | |
| total_influence += sum(influences[0][:size]) | |
| print(total_influence) | |
| ``` | |
| ### 090 | |
| ```python | |
| def __starting_point(): | |
| n = int(input()) | |
| supporters = {} | |
| supporters[0] = [] | |
| supporters[1] = [] | |
| supporters[10] = [] | |
| supporters[11] = [] | |
| for i in range(n): | |
| [x, y] = [int(x) for x in input().split()] | |
| supporters[x].append(y) | |
| for x in supporters: | |
| supporters[x].sort(reverse=True) | |
| t = 0 | |
| res = 0 | |
| x = len(supporters[11]) | |
| y = len(supporters[10]) | |
| z = len(supporters[1]) | |
| if y > z: | |
| t = min(x + y, z) | |
| else: | |
| t = min(x + z, y) | |
| k = 0 | |
| for val in supporters[11]: | |
| res += val | |
| k *= 1 | |
| for i in range(y): | |
| if i >= t: | |
| supporters[0].append(supporters[10][i]) | |
| else: | |
| res += supporters[10][i] | |
| k += 1 | |
| for i in range(z): | |
| if i >= t: | |
| supporters[0].append(supporters[1][i]) | |
| else: | |
| res += supporters[1][i] | |
| k += 1 | |
| supporters[0].sort(reverse=True) | |
| t = min(x + y, x + z) | |
| i = 0 | |
| while 2 * t > k and i < len(supporters[0]): | |
| k += 1 | |
| res += supporters[0][i] | |
| i += 1 | |
| print(res) | |
| __starting_point() | |
| ``` | |
| ### 091 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r >= t: | |
| print(-1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 092 | |
| ```python | |
| n = int(input()) | |
| su0 = [] | |
| su1 = [] | |
| su00 = [] | |
| su11 = [] | |
| ans = 0 | |
| for i in range(n): | |
| sup, val = map(int, input().split()) | |
| if sup == 0: | |
| su00.append(val) | |
| elif sup == 11: | |
| su11.append(val) | |
| elif sup == 1: | |
| su0.append(val) | |
| else: | |
| su1.append(val) | |
| su0.sort() | |
| su1.sort(reverse=True) | |
| su11.sort() | |
| su00.sort() | |
| while len(su1) != 0 and len(su0) != 0: | |
| ans += su1[len(su1) - 1] + su0[len(su0) - 1] | |
| su1.pop() | |
| su0.pop() | |
| while len(su11) != 0 and len(su00) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| if su00[len(su00) - 1] > su0[len(su0) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| if su00[len(su00) - 1] > su1[len(su1) - 1]: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] + su00[len(su00) - 1] | |
| su11.pop() | |
| su00.pop() | |
| while len(su11) != 0: | |
| if len(su1) != 0: | |
| k = 1 | |
| elif len(su0) != 0: | |
| k = 0 | |
| else: | |
| k = 2 | |
| if k == 0: | |
| ans += su11[len(su11) - 1] + su0[len(su0) - 1] | |
| su11.pop() | |
| su0.pop() | |
| elif k == 1: | |
| ans += su11[len(su11) - 1] + su1[len(su1) - 1] | |
| su11.pop() | |
| su1.pop() | |
| else: | |
| ans += su11[len(su11) - 1] | |
| su11.pop() | |
| print(ans) | |
| ``` | |
| ### 093 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 3 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 094 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 1 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 0 | |
| else: | |
| j += 1 | |
| if flag == 0: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 095 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 0 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 1 | |
| else: | |
| j += 1 | |
| if flag == 0: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 096 | |
| ```python | |
| def __starting_point(): | |
| n = int(input()) | |
| supporters = {} | |
| supporters[0] = [] | |
| supporters[1] = [] | |
| supporters[10] = [] | |
| supporters[11] = [] | |
| for i in range(n): | |
| [x, y] = [int(x) for x in input().split()] | |
| supporters[x].append(y) | |
| for x in supporters: | |
| supporters[x].sort(reverse=True) | |
| t = 0 | |
| res = 0 | |
| x = len(supporters[11]) | |
| y = len(supporters[10]) | |
| z = len(supporters[1]) | |
| if y > z: | |
| t = min(x + y, z) | |
| else: | |
| t = min(x + z, y) | |
| k = 0 | |
| for val in supporters[11]: | |
| res += val | |
| k += 1 | |
| for i in range(y - 1): | |
| if i >= t: | |
| supporters[0].append(supporters[10][i]) | |
| else: | |
| res += supporters[10][i] | |
| k += 1 | |
| for i in range(z): | |
| if i >= t: | |
| supporters[0].append(supporters[1][i]) | |
| else: | |
| res += supporters[1][i] | |
| k += 1 | |
| supporters[0].sort(reverse=True) | |
| t = min(x + y, x + z) | |
| i = 0 | |
| while 2 * t > k and i < len(supporters[0]): | |
| k += 1 | |
| res += supporters[0][i] | |
| i += 1 | |
| print(res) | |
| __starting_point() | |
| ``` | |
| ### 097 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 4: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 098 | |
| ```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]) | |
| ``` | |
| ### 099 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000008 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 100 | |
| ```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]) | |
| ``` | |
| ### 101 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -5): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 102 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix *= unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 103 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 4 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 104 | |
| ```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)) | |
| ``` | |
| ### 105 | |
| ```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 += 3 | |
| 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') | |
| ``` | |
| ### 106 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000006 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 107 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 10 ** 8 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 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, atexit, math, sys, bisect | |
| sys.setrecursionlimit(1000000) | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| try: | |
| import numpy | |
| def dprint(*args, **kwargs): | |
| print(*args, file=sys.stderr) | |
| dprint('debug mode') | |
| except Exception: | |
| def dprint(*args, **kwargs): | |
| pass | |
| inId = 0 | |
| outId = 0 | |
| if inId > 0: | |
| dprint('use input', inId) | |
| sys.stdin = open('input' + str(inId) + '.txt', 'r') | |
| if outId > 0: | |
| dprint('use output', outId) | |
| sys.stdout = open('stdout' + str(outId) + '.txt', 'w') | |
| atexit.register(lambda: sys.stdout.close()) | |
| N, M = getIntList() | |
| ne = [0 for i in range(N + 1)] | |
| za = getIntList() | |
| for i in range(N - 1): | |
| ne[za[i]] = za[i + 1] | |
| ne[za[-1] - 1] = 0 | |
| for _ in range(1, M): | |
| za = getIntList() | |
| for i in range(N - 1): | |
| a = za[i] | |
| b = za[i + 1] | |
| if ne[a] != b: | |
| ne[a] = -1 | |
| a = za[-1] | |
| if ne[a] != 0: | |
| ne[a] = -1 | |
| tin = [0 for i in range(N + 1)] | |
| for i in range(1, N + 1): | |
| a = ne[i] | |
| if a > 0: | |
| tin[a] = 1 | |
| res = 0 | |
| for i in range(1, N + 1): | |
| if tin[i]: | |
| continue | |
| n = 0 | |
| while i > 0: | |
| n += 1 | |
| i = ne[i] | |
| res += n * (n + 1) // 2 | |
| print(res) | |
| ``` | |
| ### 110 | |
| ```python | |
| n = int(input()) | |
| influences = [[], [], [], []] | |
| for i in range(0, n): | |
| [support, influence] = [x for x in input().split()] | |
| support = int(support, 2) | |
| influences[support].append(int(influence)) | |
| for i in range(0, 4): | |
| influences[i] = sorted(influences[i]) | |
| influences[i].reverse() | |
| total_influence = 0 | |
| total_influence += sum(influences[3]) | |
| min_size = min(len(influences[0]), len(influences[1])) | |
| total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) | |
| size = len(influences[3]) | |
| influences[0] += influences[1][min_size:] + influences[2][min_size:] | |
| influences[0] = sorted(influences[0]) | |
| influences[0].reverse() | |
| total_influence += sum(influences[0][:size]) | |
| print(total_influence) | |
| ``` | |
| ### 111 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson - 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 112 | |
| ```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]) | |
| ``` | |
| ### 113 | |
| ```python | |
| def ii(): | |
| return int(input()) | |
| def ss(): | |
| return [x for x in input()] | |
| def si(): | |
| return [int(x) for x in input().split()] | |
| def mi(): | |
| return map(int, input().split()) | |
| a, b = mi() | |
| s = [] | |
| ans = 0 | |
| key = True | |
| for i in range(a): | |
| i = si() | |
| s.append(i) | |
| ans += sum(i) | |
| for i in range(a - 1): | |
| if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(b - 1): | |
| if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]): | |
| ans = -1 | |
| key = False | |
| break | |
| for i in range(a - 2, 0, -1): | |
| if not key: | |
| break | |
| for x in range(b - 2, 0, -1): | |
| if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1]: | |
| if s[i][x] == 0: | |
| s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1 | |
| ans += s[i][x] | |
| else: | |
| ans = -1 | |
| key = False | |
| break | |
| print(ans) | |
| ``` | |
| ### 114 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(1, m): | |
| for k in range(n - 1): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 1], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 115 | |
| ```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]) | |
| ``` | |
| ### 116 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000004 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 117 | |
| ```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') | |
| ``` | |
| ### 118 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1 - 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 119 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1 - 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 120 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(0, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [4 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 122 | |
| ```python | |
| from math import ceil | |
| n, A = (int(input()), []) | |
| a = b = ab = ab_ = ans = 0 | |
| for _ in range(n): | |
| t, tt = input().split() | |
| tt = int(tt) | |
| if t != '11': | |
| A.append([t, tt]) | |
| if t == '10': | |
| a += 1 | |
| elif t == '01': | |
| b += 1 | |
| else: | |
| ab_ += 1 | |
| else: | |
| ans += tt | |
| ab += -1 | |
| A.sort(reverse=True, key=lambda x: x[1]) | |
| x = y = t = ttt = ab | |
| k, j = (a, b) | |
| for i in A: | |
| if i[0] == '10' and (j or ttt): | |
| ans += i[1] | |
| t += 1 | |
| x += 1 | |
| if j == 0 and ttt: | |
| ttt -= 1 | |
| j = 1 | |
| j -= 1 | |
| elif i[0] == '01' and (k or ttt): | |
| ans += i[1] | |
| t += 1 | |
| y += 1 | |
| if k == 0 and ttt: | |
| ttt -= 1 | |
| k = 1 | |
| k -= 1 | |
| elif i[0] == '00' and ttt: | |
| ans += i[1] | |
| ttt -= 1 | |
| t += 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, 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]) | |
| ``` | |
| ### 124 | |
| ```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() | |
| ``` | |
| ### 125 | |
| ```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') | |
| ``` | |
| ### 126 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb - 1): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 127 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 9 + 5 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 128 | |
| ```python | |
| import sys | |
| read = sys.stdin.read | |
| readline = sys.stdin.readline | |
| n, c = list(map(int, readline().split())) | |
| *a, = list(map(int, readline().split())) | |
| *b, = list(map(int, readline().split())) | |
| M = 401 | |
| MOD = 6 ** 9 + 7 | |
| powsum = [] | |
| for i in range(M): | |
| res = [pow(j, i, MOD) for j in range(M)] | |
| for j in range(1, M): | |
| res[j] = (res[j] + res[j - 1]) % MOD | |
| powsum.append(res) | |
| dp = [0] * (c + 1) | |
| dp[0] = 1 | |
| for ai, bi in zip(a, b): | |
| ndp = [0] * (c + 1) | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| ndp[k] += (powsum[j][bi] - powsum[j][ai - 1]) * dp[k - j] % MOD | |
| ndp[k] %= MOD | |
| dp = ndp | |
| print(dp[c] % MOD) | |
| ``` | |
| ### 129 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - -1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 130 | |
| ```python | |
| K = 998244353 | |
| def mu(a, n): | |
| if n == 0: | |
| return 1 | |
| q = mu(a, n // 2) | |
| if n % 2 == 0: | |
| return q * q % K | |
| return q * q % K * a % K | |
| MAXN = 200005 | |
| dd = [0 for i in range(MAXN)] | |
| p = [0 for i in range(MAXN)] | |
| s = [-4 for i in range(MAXN)] | |
| a = [0 for i in range(MAXN)] | |
| fen = [0 for i in range(MAXN)] | |
| def add(u, v): | |
| i = u | |
| while i <= 200000: | |
| fen[i] += v | |
| i += i & -i | |
| def get(u): | |
| res = 0 | |
| i = u | |
| while i > 0: | |
| res += fen[i] | |
| i -= i & -i | |
| return res | |
| n = int(input()) | |
| data = input().split() | |
| cnt = 0 | |
| for i in range(1, n + 1): | |
| p[i] = int(data[i - 1]) | |
| if p[i] > 0: | |
| dd[p[i]] = 1 | |
| else: | |
| cnt += 1 | |
| for i in range(1, n + 1): | |
| if dd[i] == 0: | |
| s[i] = s[i - 1] + 1 | |
| else: | |
| s[i] = s[i - 1] | |
| cnt1 = 0 | |
| P = 0 | |
| den = mu(cnt, K - 2) | |
| for i in range(1, n + 1): | |
| if p[i] == -1: | |
| cnt1 += 1 | |
| else: | |
| u = cnt - cnt1 | |
| P = (P + u * s[p[i]] % K * den % K) % K | |
| P = (P + cnt1 * (cnt - s[p[i]]) % K * den % K) % K | |
| P = (P + cnt * (cnt - 1) * mu(4, K - 2)) % K | |
| m = 0 | |
| for i in range(1, n + 1): | |
| if p[i] > 0: | |
| m += 1 | |
| a[m] = p[i] | |
| P1 = 0 | |
| for i in range(m, 0, -1): | |
| P1 = (P1 + get(a[i])) % K | |
| add(a[i], 1) | |
| P = (P + P1) % K | |
| print(P) | |
| ``` | |
| ### 131 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(2, m): | |
| for k in range(n - 1): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 1], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 132 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(1, m): | |
| for k in range(n - 4): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 1], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 133 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + -1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 134 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 0 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 0 | |
| else: | |
| j += 1 | |
| if flag == 0: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 135 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1 - 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 136 | |
| ```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') | |
| ``` | |
| ### 137 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 - 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 138 | |
| ```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') | |
| ``` | |
| ### 139 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 140 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + -1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 141 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 10 ** 5 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 142 | |
| ```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() | |
| ``` | |
| ### 143 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 4 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 144 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + -1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 145 | |
| ```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[1]) | |
| 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]) | |
| ``` | |
| ### 146 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 147 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 10 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] += xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 148 | |
| ```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]) | |
| ``` | |
| ### 149 | |
| ```python | |
| n = int(input()) | |
| influences = [[], [], [], []] | |
| for i in range(0, n): | |
| [support, influence] = [x for x in input().split()] | |
| support = int(support, 2) | |
| influences[support].append(int(influence)) | |
| for i in range(0, 4): | |
| influences[i] = sorted(influences[i]) | |
| influences[i].reverse() | |
| total_influence = 0 | |
| total_influence += sum(influences[3]) | |
| min_size = min(len(influences[2]), len(influences[1])) | |
| total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) | |
| size = len(influences[3]) | |
| influences[0] += influences[0][min_size:] + influences[2][min_size:] | |
| influences[0] = sorted(influences[0]) | |
| influences[0].reverse() | |
| total_influence += sum(influences[0][:size]) | |
| print(total_influence) | |
| ``` | |
| ### 150 | |
| ```python | |
| import collections, atexit, math, sys, bisect | |
| sys.setrecursionlimit(1000000) | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| try: | |
| import numpy | |
| def dprint(*args, **kwargs): | |
| print(*args, file=sys.stderr) | |
| dprint('debug mode') | |
| except Exception: | |
| def dprint(*args, **kwargs): | |
| pass | |
| inId = 0 | |
| outId = 0 | |
| if inId > 0: | |
| dprint('use input', inId) | |
| sys.stdin = open('input' + str(inId) + '.txt', 'r') | |
| if outId > 0: | |
| dprint('use output', outId) | |
| sys.stdout = open('stdout' + str(outId) + '.txt', 'w') | |
| atexit.register(lambda: sys.stdout.close()) | |
| N, M = getIntList() | |
| ne = [0 for i in range(N + 1)] | |
| za = getIntList() | |
| for i in range(N - 1): | |
| ne[za[i]] = za[i + 1] | |
| ne[za[-1]] = 0 | |
| for _ in range(1, M): | |
| za = getIntList() | |
| for i in range(N - 1): | |
| a = za[i] | |
| b = za[i + 1] | |
| if ne[a] != b: | |
| ne[a] = -1 | |
| a = za[-1] | |
| if ne[a] != 0: | |
| ne[a] = -1 | |
| tin = [0 for i in range(N + 1)] | |
| for i in range(1, N - 1): | |
| a = ne[i] | |
| if a > 0: | |
| tin[a] = 1 | |
| res = 0 | |
| for i in range(1, N + 1): | |
| if tin[i]: | |
| continue | |
| n = 0 | |
| while i > 0: | |
| n += 1 | |
| i = ne[i] | |
| res += n * (n + 1) // 2 | |
| print(res) | |
| ``` | |
| ### 151 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - -1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 152 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 3 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 153 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 0 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 154 | |
| ```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]) | |
| ``` | |
| ### 155 | |
| ```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 += 5 | |
| 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]) | |
| ``` | |
| ### 156 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 2 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 157 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1 + 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 158 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return --2 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni - 1): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 160 | |
| ```python | |
| n = int(input()) | |
| influences = [[], [], [], []] | |
| for i in range(0, n): | |
| [support, influence] = [x for x in input().split()] | |
| support = int(support, 2) | |
| influences[support].append(int(influence)) | |
| for i in range(0, 4): | |
| influences[i] = sorted(influences[i]) | |
| influences[i].reverse() | |
| total_influence = 0 | |
| total_influence += sum(influences[3]) | |
| min_size = min(len(influences[2]), len(influences[1])) | |
| total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) | |
| size = len(influences[3]) | |
| influences[0] += influences[1][min_size:] + influences[1][min_size:] | |
| influences[0] = sorted(influences[0]) | |
| influences[0].reverse() | |
| total_influence += sum(influences[0][:size]) | |
| print(total_influence) | |
| ``` | |
| ### 161 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| def result(a, n, m): | |
| r = 0 | |
| for i in range(-1, -m, -1): | |
| if a[-1][i] == 0: | |
| a[-1][i] = a[-1][i + 1] - 1 | |
| if a[-1][i] <= a[-1][i - 1] or a[-1][i] <= a[-2][i]: | |
| return -1 | |
| r = r + a[-1][i] | |
| if a[-1][-m] == 0: | |
| a[-1][-m] = a[-1][-m + 1] - 1 | |
| if a[-1][-m] <= a[-2][-m]: | |
| return -1 | |
| r = r + a[-1][-m] | |
| for i in range(-2, -n, -1): | |
| for j in range(-1, -m, -1): | |
| if j == -1: | |
| if a[i][j] == 0: | |
| a[i][j] == a[i + 1][j] - 1 | |
| if a[i][j] <= a[i][j - 1] or a[i][j] <= a[i - 1][j]: | |
| return -1 | |
| r = r + a[i][j] | |
| continue | |
| if a[i][j] == 0: | |
| a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1) | |
| if a[i][j] <= a[i - 1][j] or a[i][j] <= a[i][j - 1]: | |
| return -1 | |
| r = r + a[i][j] | |
| if a[i][-m] == 0: | |
| a[i][-m] = min(a[i + 1][-m] - 1, a[i][-m + 1] - 1) | |
| if a[i][-m] <= a[i - 1][-m]: | |
| return -1 | |
| r = r + a[i][-m] | |
| if a[0][-1] == 0: | |
| a[0][-1] = a[1][-1] - 1 | |
| if a[0][-1] <= a[0][-2]: | |
| return -1 | |
| r = r + a[0][-1] | |
| for i in range(-2, -m, -1): | |
| if a[0][i] == 0: | |
| a[0][i] = min(a[0][i + 1] - 1, a[1][i] - 1) | |
| if a[0][i] <= a[0][i - 1]: | |
| return -1 | |
| r = r + a[0][i] | |
| if a[0][0] == 1: | |
| a[0][0] = min(a[0][1] - 1, a[1][0] - 1) | |
| r = r + a[0][0] | |
| return r | |
| print(result(a, n, m)) | |
| ``` | |
| ### 162 | |
| ```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') | |
| ``` | |
| ### 163 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| def int_array(): | |
| return list(map(int, input().strip().split())) | |
| def float_array(): | |
| return list(map(float, input().strip().split())) | |
| def str_array(): | |
| return input().strip().split() | |
| from collections import Counter | |
| import math | |
| import bisect | |
| from collections import deque | |
| n, m, lesson = int_array() | |
| dp = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| days = [[] for i in range(n)] | |
| for i in range(n): | |
| s = input() | |
| for j in range(m): | |
| if s[j] == '1': | |
| days[i].append(j + 1) | |
| m = [[250005 for i in range(lesson + 2)] for j in range(n + 1)] | |
| for i in range(n): | |
| for j in range(lesson + 1): | |
| if j <= len(days[i]): | |
| if j == len(days[i]): | |
| m[i][j] = 0 | |
| continue | |
| else: | |
| for k in range(0, j + 1): | |
| var = days[i][0 + k] | |
| var1 = days[i][-1 * max(1, 1 + (j - k))] | |
| m[i][j] = min(m[i][j], var1 - var + 1) | |
| for i in range(lesson + 1): | |
| dp[0][i] = m[0][i] | |
| for i in range(1, n): | |
| for j in range(lesson + 1 + 1): | |
| for k in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][j - k] + m[i][k]) | |
| print(min(dp[n - 1])) | |
| ``` | |
| ### 164 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-3) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 165 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 3 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 166 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m - 1): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 167 | |
| ```python | |
| base = 998244353 | |
| def power(x, y): | |
| if y == 0: | |
| return 1 | |
| t = power(x, y // 2) | |
| t = t * t % base | |
| if y % 2: | |
| t = t * x % base | |
| return t | |
| def inverse(x): | |
| return power(x, base - 2) | |
| ft = [0] | |
| for i in range(0, 200000): | |
| ft.append(0) | |
| def get(i): | |
| res = 0 | |
| while i <= 200000: | |
| res += ft[i] | |
| i += i & -i | |
| return res | |
| def update(i, x): | |
| while i: | |
| ft[i] += x | |
| i -= i & -i | |
| n = int(input()) | |
| a = [0] | |
| a += list(map(int, input().split())) | |
| neg = [0] | |
| non = [0] | |
| for i in range(1, n + 1): | |
| non.append(0) | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| non[a[i]] += 1 | |
| for i in range(1, n + 1): | |
| non[i] += non[i - 1] | |
| for i in range(1, n + 1): | |
| if a[i] == -1: | |
| neg.append(neg[i - 1] + 1) | |
| else: | |
| neg.append(neg[i - 1]) | |
| m = neg[n] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| ans += get(a[i]) | |
| update(a[i], 1) | |
| fm = 1 | |
| fs = fm | |
| for i in range(1, m + 1 - 1): | |
| fs = fm | |
| fm = fm * i % base | |
| fs = fs * inverse(fm) % base | |
| for i in range(1, n + 1): | |
| if a[i] != -1: | |
| less = a[i] - non[a[i]] | |
| more = m - less | |
| ans = (ans + neg[i] * more * fs) % base | |
| ans = (ans + (m - neg[i]) * less * fs) % base | |
| ans = (ans + m * (m - 1) * inverse(4)) % base | |
| print(ans) | |
| ``` | |
| ### 168 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| arr = [] | |
| arr1 = [] | |
| for i in range(m): | |
| arrx = list(map(int, input().split())) | |
| arry = [0] * n | |
| for j in range(n): | |
| arry[arrx[j] - 1] = j + 1 | |
| arr.append(arrx) | |
| arr1.append(arry) | |
| ans = n | |
| i = 0 | |
| j = 1 | |
| flag = 2 | |
| while i < n - 1 and j < n: | |
| k1 = arr[0][i] | |
| k2 = arr[0][j] | |
| l = 0 | |
| while l < m: | |
| if arr1[l][arr[0][j - 1] - 1] != arr1[l][arr[0][j] - 1] - 1: | |
| flag = 1 | |
| break | |
| if flag == 1: | |
| break | |
| l += 1 | |
| if flag == 1: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| i = j | |
| j += 1 | |
| flag = 0 | |
| else: | |
| j += 1 | |
| if flag == 0: | |
| ans += (j - i) * (j - i + 1) // 2 | |
| ans -= j - i | |
| print(ans) | |
| ``` | |
| ### 169 | |
| ```python | |
| def main(): | |
| n, m = [int(c) for c in input().split()] | |
| if m == 1: | |
| print(n * (n - 1) // 2) | |
| return | |
| testimonies = [[int(c) for c in input().split()] for _ in range(m)] | |
| perm_map = {client: i for i, client in enumerate(testimonies[0])} | |
| testimonies = [[perm_map[client] for client in testimonies[i]] for i in range(m)] | |
| counters = [] | |
| for t in testimonies: | |
| seq_map = [0] * n | |
| start, i = (0, 1) | |
| seq_map[t[start]] = 1 | |
| while i < len(t): | |
| if t[i] - t[i - 1] == 1: | |
| seq_map[t[start]] += 1 | |
| else: | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| start = i | |
| seq_map[t[start]] = 1 | |
| i += 1 | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| counters.append(seq_map) | |
| ans = 0 | |
| for i in range(n): | |
| _min = min([cnt[i] for cnt in counters]) | |
| ans += _min | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 170 | |
| ```python | |
| N, C = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| MOD = 10 ** 9 + 7 | |
| def gen_poly(a, b, c): | |
| poly = [0 for _ in range(c + 1)] | |
| for x in range(a, b + 1): | |
| xpow = 1 | |
| for i in range(c + 1): | |
| poly[i] -= xpow | |
| xpow = xpow * x % MOD | |
| for i in range(c + 1): | |
| poly[i] %= MOD | |
| return poly | |
| def mult_polys(p1, p2, dmax): | |
| ans = [0 for _ in range(dmax + 1)] | |
| for i, c in enumerate(p1): | |
| for i2, c2 in enumerate(p2): | |
| if i + i2 <= dmax: | |
| ans[i + i2] += c * c2 | |
| else: | |
| break | |
| ans = [x % MOD for x in ans] | |
| return ans | |
| polys = [gen_poly(a, b, C) for a, b in zip(A, B)] | |
| prodpoly = [1] | |
| for poly in polys: | |
| prodpoly = mult_polys(prodpoly, poly, C) | |
| print(prodpoly[C]) | |
| ``` | |
| ### 171 | |
| ```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]) | |
| ``` | |
| ### 172 | |
| ```python | |
| from collections import defaultdict, deque | |
| from heapq import heappush, heappop | |
| import sys | |
| import math | |
| import bisect | |
| import random | |
| def LI(): | |
| return [int(x) for x in sys.stdin.readline().split()] | |
| def I(): | |
| return int(sys.stdin.readline()) | |
| def LS(): | |
| return [list(x) for x in sys.stdin.readline().split()] | |
| def S(): | |
| res = list(sys.stdin.readline()) | |
| if res[-1] == '\n': | |
| return res[:-1] | |
| return res | |
| def IR(n): | |
| return [I() for i in range(n)] | |
| def LIR(n): | |
| return [LI() for i in range(n)] | |
| def SR(n): | |
| return [S() for i in range(n)] | |
| def LSR(n): | |
| return [LS() for i in range(n)] | |
| sys.setrecursionlimit(1000000) | |
| mod = 1000000007 | |
| def A(): | |
| return | |
| def B(): | |
| return | |
| def C(): | |
| n, c = LI() | |
| a = LI() | |
| b = LI() | |
| s = [[0] * (c + 1) for i in range(n)] | |
| for i in range(n): | |
| ai, bi = (a[i], b[i]) | |
| for x in range(ai, bi + 1): | |
| e = 1 | |
| for k in range(c + 1): | |
| s[i][k] += e | |
| if s[i][k] >= mod: | |
| s[i][k] %= mod | |
| e *= x | |
| if e >= mod: | |
| e %= mod | |
| dp = [[0] * (c + 1) for i in range(n)] | |
| for k in range(c + 1): | |
| dp[0][k] = s[0][k] | |
| for i in range(1, n): | |
| for k in range(c + 1): | |
| for j in range(k + 1): | |
| dp[i][k] += dp[i - 1][j] * s[i][k - j] | |
| if dp[i][k] >= mod: | |
| dp[i][k] %= mod | |
| print(dp[-1][c]) | |
| return | |
| def D(): | |
| return | |
| def E(): | |
| return | |
| def F(): | |
| return | |
| def __starting_point(): | |
| C() | |
| __starting_point() | |
| ``` | |
| ### 173 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 0 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 174 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 3 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 175 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + -1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 176 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb + 1): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 177 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1 - 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 178 | |
| ```python | |
| def main(): | |
| n, m = [int(c) for c in input().split()] | |
| if m == 1: | |
| print(n * (n * 1) // 2) | |
| return | |
| testimonies = [[int(c) for c in input().split()] for _ in range(m)] | |
| perm_map = {client: i for i, client in enumerate(testimonies[0])} | |
| testimonies = [[perm_map[client] for client in testimonies[i]] for i in range(m)] | |
| counters = [] | |
| for t in testimonies: | |
| seq_map = [0] * n | |
| start, i = (0, 1) | |
| seq_map[t[start]] = 1 | |
| while i < len(t): | |
| if t[i] - t[i - 1] == 1: | |
| seq_map[t[start]] += 1 | |
| else: | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| start = i | |
| seq_map[t[start]] = 1 | |
| i += 1 | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| counters.append(seq_map) | |
| ans = 0 | |
| for i in range(n): | |
| _min = min([cnt[i] for cnt in counters]) | |
| ans += _min | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 179 | |
| ```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() | |
| ``` | |
| ### 180 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1 - 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k]) | |
| ``` | |
| ### 181 | |
| ```python | |
| def main(): | |
| n, m = [int(c) for c in input().split()] | |
| if m == 1: | |
| print(n * (n + 1) // 2) | |
| return | |
| testimonies = [[int(c) for c in input().split()] for _ in range(m)] | |
| perm_map = {client: i for i, client in enumerate(testimonies[0])} | |
| testimonies = [[perm_map[client] for client in testimonies[i]] for i in range(m)] | |
| counters = [] | |
| for t in testimonies: | |
| seq_map = [0] * n | |
| start, i = (0, 1) | |
| seq_map[t[start]] = 1 | |
| while i < len(t): | |
| if t[i] - t[i - 1] == 1: | |
| seq_map[t[start]] += 1 | |
| else: | |
| for k, j in enumerate(list(range(start + 1, i)), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| start = i | |
| seq_map[t[start]] = 1 | |
| i += 1 | |
| for k, j in enumerate(list(reversed(range(start + 1, i))), 1): | |
| seq_map[t[j]] = seq_map[t[start]] - k | |
| counters.append(seq_map) | |
| ans = 0 | |
| for i in range(n): | |
| _min = min([cnt[i] for cnt in counters]) | |
| ans += _min | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 182 | |
| ```python | |
| n, m = map(int, input().split()) | |
| matrix = [list(map(int, input().split())) for _ in range(n)] | |
| res = 0 | |
| for i in range(n - 2, 0, -1): | |
| for j in range(m - 1, 0, -1): | |
| if matrix[i][j] == 0: | |
| mn = min(matrix[i + 1][j], matrix[i][j + 1]) | |
| if mn != 1: | |
| matrix[i][j] = mn - 1 | |
| else: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| else: | |
| for i in range(n): | |
| if [matrix[i][j] for j in range(m - 1) if matrix[i][j] >= matrix[i][j + 1]]: | |
| res = -1 | |
| break | |
| else: | |
| for j in range(m): | |
| if [matrix[i][j] for i in range(n - 1) if matrix[i][j] >= matrix[i + 1][j]]: | |
| res = -1 | |
| break | |
| else: | |
| res = sum((sum(matrix[i]) for i in range(n))) | |
| print(res) | |
| ``` | |
| ### 183 | |
| ```python | |
| from math import ceil | |
| n, A = (int(input()), []) | |
| a = b = ab = ab_ = ans = 0 | |
| for _ in range(n): | |
| t, tt = input().split() | |
| tt = int(tt) | |
| if t != '11': | |
| A.append([t, tt]) | |
| if t == '10': | |
| a += -1 | |
| elif t == '01': | |
| b += 1 | |
| else: | |
| ab_ += 1 | |
| else: | |
| ans += tt | |
| ab += 1 | |
| A.sort(reverse=True, key=lambda x: x[1]) | |
| x = y = t = ttt = ab | |
| k, j = (a, b) | |
| for i in A: | |
| if i[0] == '10' and (j or ttt): | |
| ans += i[1] | |
| t += 1 | |
| x += 1 | |
| if j == 0 and ttt: | |
| ttt -= 1 | |
| j = 1 | |
| j -= 1 | |
| elif i[0] == '01' and (k or ttt): | |
| ans += i[1] | |
| t += 1 | |
| y += 1 | |
| if k == 0 and ttt: | |
| ttt -= 1 | |
| k = 1 | |
| k -= 1 | |
| elif i[0] == '00' and ttt: | |
| ans += i[1] | |
| ttt -= 1 | |
| t += 1 | |
| print(ans) | |
| ``` | |
| ### 184 | |
| ```python | |
| import collections, atexit, math, sys, bisect | |
| sys.setrecursionlimit(1000000) | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| try: | |
| import numpy | |
| def dprint(*args, **kwargs): | |
| print(*args, file=sys.stderr) | |
| dprint('debug mode') | |
| except Exception: | |
| def dprint(*args, **kwargs): | |
| pass | |
| inId = 0 | |
| outId = 0 | |
| if inId > 0: | |
| dprint('use input', inId) | |
| sys.stdin = open('input' + str(inId) + '.txt', 'r') | |
| if outId > 0: | |
| dprint('use output', outId) | |
| sys.stdout = open('stdout' + str(outId) + '.txt', 'w') | |
| atexit.register(lambda: sys.stdout.close()) | |
| N, M = getIntList() | |
| ne = [0 for i in range(N + 1)] | |
| za = getIntList() | |
| for i in range(N - 1): | |
| ne[za[i]] = za[i + 1] | |
| ne[za[-1]] = 0 | |
| for _ in range(1, M - 1): | |
| za = getIntList() | |
| for i in range(N - 1): | |
| a = za[i] | |
| b = za[i + 1] | |
| if ne[a] != b: | |
| ne[a] = -1 | |
| a = za[-1] | |
| if ne[a] != 0: | |
| ne[a] = -1 | |
| tin = [0 for i in range(N + 1)] | |
| for i in range(1, N + 1): | |
| a = ne[i] | |
| if a > 0: | |
| tin[a] = 1 | |
| res = 0 | |
| for i in range(1, N + 1): | |
| if tin[i]: | |
| continue | |
| n = 0 | |
| while i > 0: | |
| n += 1 | |
| i = ne[i] | |
| res += n * (n + 1) // 2 | |
| print(res) | |
| ``` | |
| ### 185 | |
| ```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]) | |
| ``` | |
| ### 186 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(--1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 187 | |
| ```python | |
| N, C = list(map(int, input().split())) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| mod = 14 ** 9 + 7 | |
| acc = [[0] for _ in range(401)] | |
| for i in range(401): | |
| for j in range(1, 401): | |
| acc[i].append((acc[i][-1] + pow(j, i, mod)) % mod) | |
| dp = [[0 for _ in range(C + 1)] for _ in range(N + 1)] | |
| dp[0][0] = 1 | |
| for i in range(N): | |
| for candy_cur in range(C + 1): | |
| for candy_plus in range(C - candy_cur + 1): | |
| dp[i + 1][candy_cur + candy_plus] = (dp[i + 1][candy_cur + candy_plus] + dp[i][candy_cur] * ((acc[candy_plus][B[i]] - acc[candy_plus][A[i] - 1]) % mod) % mod) % mod | |
| print(dp[-1][C]) | |
| ``` | |
| ### 188 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i - 1] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 189 | |
| ```python | |
| def solve(arr, n, k): | |
| s = 0 | |
| for i in reversed(list(range(n))): | |
| for j in reversed(list(range(k))): | |
| if arr[i][j] == 0: | |
| m = min(arr[i + 1][j], arr[i + 1][j + 1]) | |
| if m == 1: | |
| return -1 | |
| arr[i][j] = m - 1 | |
| s += arr[i][j] | |
| if j > 0 and arr[i][j - 1] >= arr[i][j]: | |
| return -1 | |
| if i > 0 and arr[i - 1][j] >= arr[i][j]: | |
| return -1 | |
| return s | |
| n, k = [int(x) for x in input().split()] | |
| arr = [] | |
| for _ in range(n): | |
| arr.append([int(x) for x in input().split()]) | |
| res = solve(arr, n, k) | |
| print(res) | |
| ``` | |
| ### 190 | |
| ```python | |
| def min_sub_array(day, k): | |
| if not day: | |
| return [0] * (k + 1) | |
| n = len(day) | |
| best = [float('inf')] * (n + 1) | |
| best[0] = 0 | |
| best[1] = 1 | |
| for size in range(2, n + 1): | |
| for i in range(n + 1 - size): | |
| best[size] = min(best[size], day[i + size - 1] - day[i] + 1) | |
| output = [0] * (k + 1) | |
| for i in range(k + 1 - 1): | |
| if n - i > 0: | |
| output[i] = best[n - i] | |
| return output | |
| N, M, K = list(map(int, input().split())) | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| best = min_sub_array(day, K) | |
| for _ in range(N - 1): | |
| day = [i for i, val in enumerate(input()) if val == '1'] | |
| new_day_best = min_sub_array(day, K) | |
| new_best = [float('inf')] * (K + 1) | |
| for i in range(K + 1): | |
| for j in range(i + 1): | |
| new_best[i] = min(new_best[i], new_day_best[j] + best[i - j]) | |
| best = new_best | |
| print(best[K]) | |
| ``` | |
| ### 191 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1 - 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 192 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 4 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 193 | |
| ```python | |
| n, m, k = input().split(' ') | |
| n = int(n) | |
| m = int(m) | |
| k = int(k) | |
| ind = [] | |
| pre = [] | |
| for _ in range(n): | |
| s = input() | |
| ind.append([]) | |
| for i, c in enumerate(s): | |
| if c == '1': | |
| ind[-1].append(i) | |
| for i in range(n): | |
| pre.append([]) | |
| for j in range(k + 1): | |
| pre[i].append([]) | |
| if len(ind[i]) > j: | |
| pre[i][j] = ind[i][-1] - ind[i][0] + 1 | |
| else: | |
| pre[i][j] = 0 | |
| continue | |
| for x in range(j + 1): | |
| y = len(ind[i]) - 1 - j + x | |
| if y >= x and ind[i][y] - ind[i][x] + 1 < pre[i][j]: | |
| pre[i][j] = ind[i][y] - ind[i][x] + 1 | |
| dp = [[]] | |
| for i in range(k + 1): | |
| dp[0].append(pre[0][i]) | |
| for i in range(1, n): | |
| dp.append([]) | |
| for j in range(0, k + 1): | |
| dp[i].append(pre[i][j] + dp[i - 1][0]) | |
| for z in range(j + 1): | |
| dp[i][j] = min(dp[i][j], dp[i - 1][z] + pre[i][j - z]) | |
| print(dp[n - 1][k - 1]) | |
| ``` | |
| ### 194 | |
| ```python | |
| from collections import defaultdict | |
| from collections import deque | |
| n, m = [int(x) for x in input().split()] | |
| messages = [] | |
| for i in range(m): | |
| temp = [int(x) for x in input().split()] | |
| messages.append(temp) | |
| seq = defaultdict(lambda: 0) | |
| pairs = {} | |
| for k in range(n - 1): | |
| pairs[messages[0][k]] = messages[0][k + 1] | |
| for i in range(1, m): | |
| for k in range(n - 1 - 1): | |
| temp = messages[i][k] | |
| if pairs.get(temp, None) != messages[i][k + 1]: | |
| pairs.pop(temp, None) | |
| pairs.pop(messages[i][n - 1], None) | |
| sequences = [] | |
| starts = set(pairs.keys()) | |
| ends = set(pairs.values()) | |
| conn = starts & ends | |
| for key, value in list(pairs.items()): | |
| if key in conn: | |
| continue | |
| val = value | |
| temp = [] | |
| temp.append(key) | |
| temp.append(value) | |
| while val in starts: | |
| val = pairs[val] | |
| temp.append(val) | |
| sequences.append(temp) | |
| variants = n | |
| for seq in sequences: | |
| l = len(seq) | |
| variants += l * (l - 1) // 2 | |
| print(variants) | |
| ``` | |
| ### 195 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix * 2 | |
| denom = 2 + negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 196 | |
| ```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]) | |
| ``` | |
| ### 197 | |
| ```python | |
| import sys | |
| n, m, k = list(map(int, input().split())) | |
| table = [input() for _ in range(n)] | |
| dp = [0] * (k + 1) | |
| for a in table: | |
| one = [] | |
| for i in range(m): | |
| if a[i] == '1': | |
| one.append(i) | |
| if not one: | |
| continue | |
| ni = len(one) | |
| subdp = [10 ** 9] * (ni + 1) | |
| subdp[-1] = 0 | |
| for i in range(ni - 1): | |
| for j in range(i, ni): | |
| subdp[ni - (j - i + 1)] = min(subdp[ni - (j - i + 1)], one[j] - one[i] + 1) | |
| next_dp = [10 ** 9] * (k + 1) | |
| for i in range(k, -1, -1): | |
| for j in range(ni + 1): | |
| if i + j > k: | |
| break | |
| next_dp[i + j] = min(next_dp[i + j], dp[i] + subdp[j]) | |
| dp = next_dp | |
| print(min(dp)) | |
| ``` | |
| ### 198 | |
| ```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]) | |
| ``` | |
| ### 199 | |
| ```python | |
| def merge(a, b): | |
| inda = 0 | |
| indb = 0 | |
| lena = len(a) | |
| lenb = len(b) | |
| d = [a[-1] + b[-1] + 1000] | |
| a += d | |
| b += d | |
| c = [] | |
| inversions = 0 | |
| for i in range(lena + lenb): | |
| if a[inda] < b[indb]: | |
| c.append(a[inda]) | |
| inda += 1 | |
| else: | |
| c.append(b[indb]) | |
| indb += 1 | |
| inversions += lena - inda | |
| return (c, inversions) | |
| def mergesort(a): | |
| if len(a) <= 1: | |
| return (a, 0) | |
| split = len(a) // 2 | |
| b = a[:split] | |
| c = a[split:] | |
| d = mergesort(b) | |
| e = mergesort(c) | |
| f = merge(d[0], e[0]) | |
| return (f[0], f[1] + d[1] + e[1]) | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = [] | |
| for guy in a: | |
| if guy != -1: | |
| b.append(guy) | |
| invs = mergesort(b)[1] | |
| negs = len(a) - len(b) | |
| pairs = negs * (negs - 1) // 2 | |
| used = [0] * n | |
| for guy in a: | |
| if guy != -1: | |
| used[guy - 1] += 1 | |
| unused = [0] | |
| for i in range(n - 1): | |
| unused.append(unused[-1] + 1 - used[i]) | |
| negsseen = 0 | |
| mix = 0 | |
| for i in range(n): | |
| if a[i] == -1: | |
| negsseen += 1 | |
| else: | |
| mix += unused[a[i] - 1] * (negs - negsseen) + negsseen * (negs - unused[a[i] - 1]) | |
| num = invs * 2 * negs + pairs * negs + mix // 2 | |
| denom = 2 * negs | |
| if negs == 0: | |
| print(invs % 998244353) | |
| else: | |
| for i in range(denom): | |
| if (998244353 * i + 1) % denom == 0: | |
| inv = (998244353 * i + 1) // denom | |
| break | |
| print(num * inv % 998244353) | |
| ``` | |
| ### 200 | |
| ```python | |
| n = int(input()) | |
| L = [int(x) for x in input().split()] | |
| D = {} | |
| J = [] | |
| S = [] | |
| T = [0] * (n + 1) | |
| for i in range(n): | |
| if L[i] > 0: | |
| D[L[i]] = i | |
| J.append(L[i]) | |
| T[i + 1 - 1] = T[i] | |
| else: | |
| T[i + 1] = T[i] + 1 | |
| def I(J): | |
| if len(J) <= 1: | |
| return (J, 0) | |
| else: | |
| a = J[:len(J) // 2] | |
| b = J[len(J) // 2:] | |
| a, ai = I(a) | |
| b, bi = I(b) | |
| c = [] | |
| i = 0 | |
| j = 0 | |
| inversions = ai + bi | |
| while i < len(a) and j < len(b): | |
| if a[i] <= b[j]: | |
| c.append(a[i]) | |
| i += 1 | |
| else: | |
| c.append(b[j]) | |
| j += 1 | |
| inversions += len(a) - i | |
| c += a[i:] | |
| c += b[j:] | |
| return (c, inversions) | |
| for i in range(1, n + 1): | |
| if not i in D: | |
| S.append(i) | |
| total = len(S) | |
| num = 1 | |
| denom = 1 | |
| if total > 0: | |
| themostimportantsum = 0 | |
| for i in J: | |
| low = 0 | |
| high = total - 1 | |
| while high - low > 1: | |
| guess = (high + low) // 2 | |
| if S[guess] > i: | |
| high = guess | |
| else: | |
| low = guess | |
| if S[low] > i: | |
| smaller = low | |
| elif S[high] > i: | |
| smaller = high | |
| else: | |
| smaller = high + 1 | |
| themostimportantsum += T[D[i]] * (total - smaller) + (total - T[D[i]]) * smaller | |
| num = themostimportantsum + total | |
| denom = total | |
| num = (denom * (total * (total - 1) // 2) + 2 * num) % 998244353 | |
| denom *= 2 | |
| if num == denom: | |
| if I(J)[1] == 0: | |
| print(0) | |
| else: | |
| print(I(J)[1] % 998244353) | |
| else: | |
| num += denom * I(J)[1] | |
| print((num - denom) * pow(denom % 998244353, 998244351, 998244353) % 998244353) | |
| ``` | |
| ### 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. | |