|
Download tasks/equivalent-program-pair-search/contexts/sample-00042.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 253 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00042.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00042.md
-
curl -L -o sample-00042.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00042.md
253 kB
| ### 001 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 002 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 003 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y > 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 004 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[-2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 005 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = -2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 006 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += -2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 007 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 4): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 008 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 009 | |
| ```python | |
| import math | |
| from collections import Counter, defaultdict | |
| from itertools import accumulate | |
| R = lambda: map(int, input().split()) | |
| n = int(input()) | |
| spl = [math.inf] | |
| ot_forbid = 0 | |
| sp = 0 | |
| res = 0 | |
| for i in range(n): | |
| arr = list(R()) | |
| tp = arr[0] | |
| if tp == 1: | |
| sp = arr[1] | |
| while sp > spl[-1]: | |
| spl.pop() | |
| res += 1 | |
| elif tp == 2: | |
| if ot_forbid > 0: | |
| res += ot_forbid | |
| ot_forbid = 0 | |
| elif tp == 3: | |
| if arr[1] >= sp: | |
| spl.append(arr[1]) | |
| else: | |
| res += 1 | |
| elif tp == 4: | |
| ot_forbid = -2 | |
| elif tp == 5: | |
| spl = [math.inf] | |
| else: | |
| ot_forbid += 1 | |
| print(res) | |
| ``` | |
| ### 010 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m - 1] != -1 else print(n) | |
| ``` | |
| ### 011 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X // Y | |
| print(ans) | |
| ``` | |
| ### 012 | |
| ```python | |
| import math | |
| from collections import Counter, defaultdict | |
| from itertools import accumulate | |
| R = lambda: map(int, input().split()) | |
| n = int(input()) | |
| spl = [math.inf] | |
| ot_forbid = 0 | |
| sp = 0 | |
| res = 0 | |
| for i in range(n): | |
| arr = list(R()) | |
| tp = arr[0] | |
| if tp == 1: | |
| sp = arr[1] | |
| while sp > spl[-1]: | |
| spl.pop() | |
| res += 1 | |
| elif tp == 2: | |
| if ot_forbid > 0: | |
| res += ot_forbid | |
| ot_forbid = 0 | |
| elif tp == 3: | |
| if arr[-2] >= sp: | |
| spl.append(arr[1]) | |
| else: | |
| res += 1 | |
| elif tp == 4: | |
| ot_forbid = 0 | |
| elif tp == 5: | |
| spl = [math.inf] | |
| else: | |
| ot_forbid += 1 | |
| print(res) | |
| ``` | |
| ### 013 | |
| ```python | |
| MAX_SPEED = 300 | |
| n = int(input()) | |
| actions = [list(map(int, input().split())) for _ in range(n)] | |
| speed = [0 for i in range(n)] | |
| for i in range(n): | |
| if actions[i][0] == 1: | |
| lastSpeed = actions[i][1] | |
| speed[i] = lastSpeed | |
| ignored = 0 | |
| overtook = False | |
| maxSpeed = -1 | |
| for i in range(n - 1, -1, -1): | |
| maxSpeed = max(maxSpeed, speed[i]) | |
| if actions[i][0] == 1: | |
| pass | |
| elif actions[i][0] == 3: | |
| overtook = True | |
| elif actions[i][0] == 3: | |
| if maxSpeed > actions[i][1]: | |
| ignored += 1 | |
| else: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 4: | |
| overtook = False | |
| elif actions[i][0] == 5: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 6: | |
| if overtook: | |
| ignored += 1 | |
| print(ignored) | |
| ``` | |
| ### 014 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0: | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 015 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 * tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 016 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 1 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 017 | |
| ```python | |
| l = input().split() | |
| n = int(l[1]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 018 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and -2 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 019 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not (not stack or arr[i] > stack[-1]): | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 020 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {2: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 021 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if not dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 022 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 4 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 023 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26 - 1): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 024 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[-1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 025 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 3 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 026 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 5) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 027 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 3: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 028 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 029 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps // n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 030 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [5]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 031 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-3, W)) | |
| ``` | |
| ### 032 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 4] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 033 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {3: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 034 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k + cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 035 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 5]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 036 | |
| ```python | |
| def Task485D(): | |
| cur_speed = 0 | |
| is_outstrip = 0 | |
| err_count = 0 | |
| event_speed_limit = [] | |
| event_count = int(input()) | |
| for a in range(event_count): | |
| ev = list(map(int, input().split())) | |
| if ev[0] == 1: | |
| cur_speed = ev[1] | |
| while len(event_speed_limit) > 0: | |
| if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: | |
| event_speed_limit.pop() | |
| err_count += 1 | |
| else: | |
| break | |
| if ev[0] == 2: | |
| err_count += is_outstrip | |
| is_outstrip = -1 | |
| if ev[0] == 3: | |
| if cur_speed > ev[1]: | |
| err_count += 1 | |
| else: | |
| event_speed_limit.append(ev[1]) | |
| if ev[0] == 4: | |
| is_outstrip = 0 | |
| if ev[0] == 5: | |
| event_speed_limit.clear() | |
| if ev[0] == 6: | |
| is_outstrip += 1 | |
| print(err_count) | |
| def __starting_point(): | |
| Task485D() | |
| __starting_point() | |
| ``` | |
| ### 037 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 4 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 038 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 2 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 039 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] and 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 040 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [2] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 041 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum <= 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 042 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(-4, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 043 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 0 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 044 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 2 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 045 | |
| ```python | |
| n = int(input()) | |
| ps = None | |
| seen_sl = [float('inf')] | |
| seen_ot = [True] | |
| answer = 0 | |
| for i in range(n): | |
| line = list(map(int, input().split())) | |
| t = line[0] | |
| if t == 1: | |
| ps = line[1] | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == -2: | |
| while not seen_ot[-1]: | |
| answer += 1 | |
| seen_ot.pop(-1) | |
| elif t == 3: | |
| seen_sl.append(line[1]) | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 4: | |
| seen_ot.append(True) | |
| elif t == 5: | |
| seen_sl.append(float('inf')) | |
| elif t == 6: | |
| seen_ot.append(False) | |
| print(answer) | |
| ``` | |
| ### 046 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 1 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 047 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 048 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 2: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 049 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N - 1)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 050 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) <= k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 051 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 * tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 052 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 3]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 053 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(2, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 054 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i * 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 055 | |
| ```python | |
| import math | |
| from collections import Counter, defaultdict | |
| from itertools import accumulate | |
| R = lambda: map(int, input().split()) | |
| n = int(input()) | |
| spl = [math.inf] | |
| ot_forbid = 0 | |
| sp = 0 | |
| res = 0 | |
| for i in range(n): | |
| arr = list(R()) | |
| tp = arr[0] | |
| if tp == 1: | |
| sp = arr[1] | |
| while sp > spl[-1]: | |
| spl.pop() | |
| res += 1 | |
| elif tp == 2: | |
| if ot_forbid > 0: | |
| res += ot_forbid | |
| ot_forbid = 0 | |
| elif tp == -1: | |
| if arr[1] >= sp: | |
| spl.append(arr[1]) | |
| else: | |
| res += 1 | |
| elif tp == 4: | |
| ot_forbid = 0 | |
| elif tp == 5: | |
| spl = [math.inf] | |
| else: | |
| ot_forbid += 1 | |
| print(res) | |
| ``` | |
| ### 056 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] not in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 057 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1 - 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 058 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if not pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 059 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 060 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 2 <= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 061 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 062 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 8 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 063 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key - 1] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 064 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * -2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 065 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i >= 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 066 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return -2 <= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 067 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(0, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 068 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[3] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 069 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 3: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 070 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + -2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 071 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[-1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 072 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) + inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 073 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 3] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 074 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 3 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 075 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] not in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 076 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 077 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, -1, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 078 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1 - 1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 079 | |
| ```python | |
| def Task485D(): | |
| cur_speed = 0 | |
| is_outstrip = 0 | |
| err_count = 0 | |
| event_speed_limit = [] | |
| event_count = int(input()) | |
| for a in range(event_count): | |
| ev = list(map(int, input().split())) | |
| if ev[0] == 1: | |
| cur_speed = ev[1] | |
| while len(event_speed_limit) > 0: | |
| if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: | |
| event_speed_limit.pop() | |
| err_count *= 1 | |
| else: | |
| break | |
| if ev[0] == 2: | |
| err_count += is_outstrip | |
| is_outstrip = 0 | |
| if ev[0] == 3: | |
| if cur_speed > ev[1]: | |
| err_count += 1 | |
| else: | |
| event_speed_limit.append(ev[1]) | |
| if ev[0] == 4: | |
| is_outstrip = 0 | |
| if ev[0] == 5: | |
| event_speed_limit.clear() | |
| if ev[0] == 6: | |
| is_outstrip += 1 | |
| print(err_count) | |
| def __starting_point(): | |
| Task485D() | |
| __starting_point() | |
| ``` | |
| ### 080 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi *= ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 081 | |
| ```python | |
| n = int(input()) | |
| ps = None | |
| seen_sl = [float('inf')] | |
| seen_ot = [True] | |
| answer = 0 | |
| for i in range(n): | |
| line = list(map(int, input().split())) | |
| t = line[0] | |
| if t == 1: | |
| ps = line[1] | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(--1) | |
| elif t == 2: | |
| while not seen_ot[-1]: | |
| answer += 1 | |
| seen_ot.pop(-1) | |
| elif t == 3: | |
| seen_sl.append(line[1]) | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 4: | |
| seen_ot.append(True) | |
| elif t == 5: | |
| seen_sl.append(float('inf')) | |
| elif t == 6: | |
| seen_ot.append(False) | |
| print(answer) | |
| ``` | |
| ### 082 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 4) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 083 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans //= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 084 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 085 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 086 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 1 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 087 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y // m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 088 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p >= q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 089 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 1 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 090 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n & i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 091 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1 - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 092 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [4] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 093 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 4 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 094 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], -1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 095 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 2 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 096 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 097 | |
| ```python | |
| MAX_SPEED = 300 | |
| n = int(input()) | |
| actions = [list(map(int, input().split())) for _ in range(n)] | |
| speed = [0 for i in range(n)] | |
| for i in range(n): | |
| if actions[i][0] == 1: | |
| lastSpeed = actions[i][1] | |
| speed[i] = lastSpeed | |
| ignored = 0 | |
| overtook = False | |
| maxSpeed = -1 | |
| for i in range(n - 1, --1, -1): | |
| maxSpeed = max(maxSpeed, speed[i]) | |
| if actions[i][0] == 1: | |
| pass | |
| elif actions[i][0] == 2: | |
| overtook = True | |
| elif actions[i][0] == 3: | |
| if maxSpeed > actions[i][1]: | |
| ignored += 1 | |
| else: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 4: | |
| overtook = False | |
| elif actions[i][0] == 5: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 6: | |
| if overtook: | |
| ignored += 1 | |
| print(ignored) | |
| ``` | |
| ### 098 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(1, W)) | |
| ``` | |
| ### 099 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[0]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 100 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(5, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 101 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(1) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 102 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 1 * rem_k | |
| print(cost) | |
| ``` | |
| ### 103 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps // n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 104 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1 - 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 105 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 106 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [3] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 107 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N - 1): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 108 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n > k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 109 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1 + 1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 110 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m >= n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 111 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 112 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1 + 1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 113 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 1: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c - 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 114 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[2]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 115 | |
| ```python | |
| n = int(input()) | |
| ps = None | |
| seen_sl = [float('inf')] | |
| seen_ot = [True] | |
| answer = 0 | |
| for i in range(n): | |
| line = list(map(int, input().split())) | |
| t = line[0] | |
| if t == 1: | |
| ps = line[1] | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 2: | |
| while not seen_ot[-1]: | |
| answer += -1 | |
| seen_ot.pop(-1) | |
| elif t == 3: | |
| seen_sl.append(line[1]) | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 4: | |
| seen_ot.append(True) | |
| elif t == 5: | |
| seen_sl.append(float('inf')) | |
| elif t == 6: | |
| seen_ot.append(False) | |
| print(answer) | |
| ``` | |
| ### 116 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 1 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 117 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 118 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1 - 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 119 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N - 1): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 120 | |
| ```python | |
| def Task485D(): | |
| cur_speed = 0 | |
| is_outstrip = 0 | |
| err_count = 0 | |
| event_speed_limit = [] | |
| event_count = int(input()) | |
| for a in range(event_count): | |
| ev = list(map(int, input().split())) | |
| if ev[0] == 1: | |
| cur_speed = ev[1] | |
| while len(event_speed_limit) > 0: | |
| if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: | |
| event_speed_limit.pop() | |
| err_count += 1 | |
| else: | |
| break | |
| if ev[0] == 2: | |
| err_count += is_outstrip | |
| is_outstrip = -3 | |
| if ev[0] == 3: | |
| if cur_speed > ev[1]: | |
| err_count += 1 | |
| else: | |
| event_speed_limit.append(ev[1]) | |
| if ev[0] == 4: | |
| is_outstrip = 0 | |
| if ev[0] == 5: | |
| event_speed_limit.clear() | |
| if ev[0] == 6: | |
| is_outstrip += 1 | |
| print(err_count) | |
| def __starting_point(): | |
| Task485D() | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 5: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 122 | |
| ```python | |
| n = int(input()) | |
| ps = None | |
| seen_sl = [float('inf')] | |
| seen_ot = [True] | |
| answer = 0 | |
| for i in range(n): | |
| line = list(map(int, input().split())) | |
| t = line[0] | |
| if t == 1: | |
| ps = line[1] | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 2: | |
| while not seen_ot[-1]: | |
| answer += 1 | |
| seen_ot.pop(-1) | |
| elif t == 3: | |
| seen_sl.append(line[1]) | |
| while ps > seen_sl[-1]: | |
| answer += -1 | |
| seen_sl.pop(-1) | |
| elif t == 4: | |
| seen_ot.append(True) | |
| elif t == 5: | |
| seen_sl.append(float('inf')) | |
| elif t == 6: | |
| seen_ot.append(False) | |
| print(answer) | |
| ``` | |
| ### 123 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[1]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 124 | |
| ```python | |
| MAX_SPEED = 300 | |
| n = int(input()) | |
| actions = [list(map(int, input().split())) for _ in range(n)] | |
| speed = [0 for i in range(n)] | |
| for i in range(n): | |
| if actions[i][0] == 1: | |
| lastSpeed = actions[i][1] | |
| speed[i] = lastSpeed | |
| ignored = 0 | |
| overtook = False | |
| maxSpeed = -1 | |
| for i in range(n - 1, -1, -1): | |
| maxSpeed = max(maxSpeed, speed[i]) | |
| if actions[i][0] == 1: | |
| pass | |
| elif actions[i][0] == 5: | |
| overtook = True | |
| elif actions[i][0] == 3: | |
| if maxSpeed > actions[i][1]: | |
| ignored += 1 | |
| else: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 4: | |
| overtook = False | |
| elif actions[i][0] == 5: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 6: | |
| if overtook: | |
| ignored += 1 | |
| print(ignored) | |
| ``` | |
| ### 125 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -3 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 126 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244352 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 127 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return --1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 128 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n - 1): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 129 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [2, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 130 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 4 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 131 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key + 1] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 132 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 2) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 1 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 134 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 0] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 135 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1 - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 136 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 137 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 138 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = (end - start) * 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 139 | |
| ```python | |
| MAX_SPEED = 300 | |
| n = int(input()) | |
| actions = [list(map(int, input().split())) for _ in range(n)] | |
| speed = [0 for i in range(n)] | |
| for i in range(n): | |
| if actions[i][0] == 1: | |
| lastSpeed = actions[i][1] | |
| speed[i] = lastSpeed | |
| ignored = 0 | |
| overtook = False | |
| maxSpeed = -1 | |
| for i in range(n - 1, -1, -1): | |
| maxSpeed = max(maxSpeed, speed[i]) | |
| if actions[i][0] == 1: | |
| pass | |
| elif actions[i][0] == 4: | |
| overtook = True | |
| elif actions[i][0] == 3: | |
| if maxSpeed > actions[i][1]: | |
| ignored += 1 | |
| else: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 4: | |
| overtook = False | |
| elif actions[i][0] == 5: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 6: | |
| if overtook: | |
| ignored += 1 | |
| print(ignored) | |
| ``` | |
| ### 140 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j + w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 141 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 142 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if not dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 143 | |
| ```python | |
| def Task485D(): | |
| cur_speed = 0 | |
| is_outstrip = 0 | |
| err_count = 0 | |
| event_speed_limit = [] | |
| event_count = int(input()) | |
| for a in range(event_count): | |
| ev = list(map(int, input().split())) | |
| if ev[0] == 1: | |
| cur_speed = ev[1] | |
| while len(event_speed_limit) > 0: | |
| if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: | |
| event_speed_limit.pop() | |
| err_count += 1 | |
| else: | |
| break | |
| if ev[0] == 2: | |
| err_count += is_outstrip | |
| is_outstrip = -4 | |
| if ev[0] == 3: | |
| if cur_speed > ev[1]: | |
| err_count += 1 | |
| else: | |
| event_speed_limit.append(ev[1]) | |
| if ev[0] == 4: | |
| is_outstrip = 0 | |
| if ev[0] == 5: | |
| event_speed_limit.clear() | |
| if ev[0] == 6: | |
| is_outstrip += 1 | |
| print(err_count) | |
| def __starting_point(): | |
| Task485D() | |
| __starting_point() | |
| ``` | |
| ### 144 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] + i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 145 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] not in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 146 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[0]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 147 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244357 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 148 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(-4) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 149 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif not (x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1])): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 150 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K * d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 151 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + -1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 152 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-1, W)) | |
| ``` | |
| ### 153 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[-1] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 154 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += -1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1 + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 156 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] == 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 157 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if not pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 158 | |
| ```python | |
| n = int(input()) | |
| ps = None | |
| seen_sl = [float('inf')] | |
| seen_ot = [True] | |
| answer = 0 | |
| for i in range(n): | |
| line = list(map(int, input().split())) | |
| t = line[0] | |
| if t == 1: | |
| ps = line[1] | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 2: | |
| while not seen_ot[-1]: | |
| answer += 1 | |
| seen_ot.pop(--1) | |
| elif t == 3: | |
| seen_sl.append(line[1]) | |
| while ps > seen_sl[-1]: | |
| answer += 1 | |
| seen_sl.pop(-1) | |
| elif t == 4: | |
| seen_ot.append(True) | |
| elif t == 5: | |
| seen_sl.append(float('inf')) | |
| elif t == 6: | |
| seen_ot.append(False) | |
| print(answer) | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 4]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 160 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 2 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 4 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 162 | |
| ```python | |
| def Task485D(): | |
| cur_speed = 0 | |
| is_outstrip = 0 | |
| err_count = 0 | |
| event_speed_limit = [] | |
| event_count = int(input()) | |
| for a in range(event_count): | |
| ev = list(map(int, input().split())) | |
| if ev[0] == 1: | |
| cur_speed = ev[1] | |
| while len(event_speed_limit) > 0: | |
| if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: | |
| event_speed_limit.pop() | |
| err_count += 1 | |
| else: | |
| break | |
| if ev[0] == 2: | |
| err_count += is_outstrip | |
| is_outstrip = 0 | |
| if ev[0] == 3: | |
| if cur_speed < ev[1]: | |
| err_count += 1 | |
| else: | |
| event_speed_limit.append(ev[1]) | |
| if ev[0] == 4: | |
| is_outstrip = 0 | |
| if ev[0] == 5: | |
| event_speed_limit.clear() | |
| if ev[0] == 6: | |
| is_outstrip += 1 | |
| print(err_count) | |
| def __starting_point(): | |
| Task485D() | |
| __starting_point() | |
| ``` | |
| ### 163 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 4: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 164 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244349 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 165 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i - 1] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 166 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 167 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -1 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 168 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 >= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 169 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 170 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[4] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 171 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += -1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 172 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and 3 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 173 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244355 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 174 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 175 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 3) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 176 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total * (i * k) | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 177 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1 + 1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 178 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 5: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 179 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(-1) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 180 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(-1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 181 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif not ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 182 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry - 1] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 183 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] != l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 184 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] not in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 185 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 3: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 186 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [4, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 187 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * -2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 188 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i - 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 189 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 2 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 190 | |
| ```python | |
| MAX_SPEED = 300 | |
| n = int(input()) | |
| actions = [list(map(int, input().split())) for _ in range(n)] | |
| speed = [0 for i in range(n)] | |
| for i in range(n): | |
| if actions[i][0] == 1: | |
| lastSpeed = actions[i][1] | |
| speed[i] = lastSpeed | |
| ignored = 0 | |
| overtook = False | |
| maxSpeed = -1 | |
| for i in range(n - 1 - 1, -1, -1): | |
| maxSpeed = max(maxSpeed, speed[i]) | |
| if actions[i][0] == 1: | |
| pass | |
| elif actions[i][0] == 2: | |
| overtook = True | |
| elif actions[i][0] == 3: | |
| if maxSpeed > actions[i][1]: | |
| ignored += 1 | |
| else: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 4: | |
| overtook = False | |
| elif actions[i][0] == 5: | |
| maxSpeed = speed[i - 1] | |
| elif actions[i][0] == 6: | |
| if overtook: | |
| ignored += 1 | |
| print(ignored) | |
| ``` | |
| ### 191 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 2 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 192 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 4 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 193 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 194 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] != l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 195 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 3 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 196 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = min(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 197 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 198 | |
| ```python | |
| import math | |
| from collections import Counter, defaultdict | |
| from itertools import accumulate | |
| R = lambda: map(int, input().split()) | |
| n = int(input()) | |
| spl = [math.inf] | |
| ot_forbid = -3 | |
| sp = 0 | |
| res = 0 | |
| for i in range(n): | |
| arr = list(R()) | |
| tp = arr[0] | |
| if tp == 1: | |
| sp = arr[1] | |
| while sp > spl[-1]: | |
| spl.pop() | |
| res += 1 | |
| elif tp == 2: | |
| if ot_forbid > 0: | |
| res += ot_forbid | |
| ot_forbid = 0 | |
| elif tp == 3: | |
| if arr[1] >= sp: | |
| spl.append(arr[1]) | |
| else: | |
| res += 1 | |
| elif tp == 4: | |
| ot_forbid = 0 | |
| elif tp == 5: | |
| spl = [math.inf] | |
| else: | |
| ot_forbid += 1 | |
| print(res) | |
| ``` | |
| ### 199 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if not -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 200 | |
| ```python | |
| import math | |
| from collections import Counter, defaultdict | |
| from itertools import accumulate | |
| R = lambda: map(int, input().split()) | |
| n = int(input()) | |
| spl = [math.inf] | |
| ot_forbid = 0 | |
| sp = 0 | |
| res = 0 | |
| for i in range(n): | |
| arr = list(R()) | |
| tp = arr[0] | |
| if tp == 1: | |
| sp = arr[1] | |
| while sp > spl[-1]: | |
| spl.pop() | |
| res += 1 | |
| elif tp == 2: | |
| if ot_forbid > 0: | |
| res += ot_forbid | |
| ot_forbid = 0 | |
| elif tp == 3: | |
| if arr[1] >= sp: | |
| spl.append(arr[1]) | |
| else: | |
| res += 1 | |
| elif tp == 4: | |
| ot_forbid = -1 | |
| elif tp == 5: | |
| spl = [math.inf] | |
| else: | |
| ot_forbid += 1 | |
| print(res) | |
| ``` | |
| ### 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. | |