|
Download tasks/equivalent-program-pair-search/contexts/sample-00039.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 248 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00039.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00039.md
-
curl -L -o sample-00039.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00039.md
248 kB
| ### 001 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 1) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 002 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 2 | |
| print(cnt) | |
| ``` | |
| ### 003 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) >= 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 004 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v |= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 005 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res -= k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 006 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::+1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 007 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2 - 1] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 008 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2) + 1] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 009 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-3) | |
| ``` | |
| ### 010 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += -1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 011 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 012 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 1: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 013 | |
| ```python | |
| def prov(mass, now): | |
| check = True | |
| for i in range(n): | |
| for k in range(m): | |
| if now[i][k] == '>' and mass[i] <= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '<' and mass[i] >= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '=' and mass[i] != mass[n + k]: | |
| check = False | |
| break | |
| if not check: | |
| break | |
| return check | |
| def prog(mass, n, m): | |
| prov = True | |
| for i in range(1, m): | |
| for k in range(n): | |
| if mass[i][k] < mass[i - 1][k]: | |
| prov = False | |
| break | |
| if not prov: | |
| break | |
| if not prov: | |
| return False | |
| else: | |
| mass_new = [] | |
| for i in range(1, m): | |
| mass_n = [] | |
| for k in range(n): | |
| mass_n.append(mass[i][k] - mass[i - 1][k]) | |
| mass_new.append(max(mass_n)) | |
| arr = [1 for i in range(m)] | |
| now = 1 | |
| if 1 in mass[0][:-1]: | |
| now += 1 | |
| arr = [2 for i in range(m)] | |
| for i in range(1, m): | |
| now += mass_new[i - 1] | |
| arr[mass[i][-1]] = now | |
| return arr | |
| n, m = map(int, input().split()) | |
| if n + m <= 6: | |
| now = [] | |
| for i in range(n): | |
| now.append(input()) | |
| ppp = True | |
| for i1 in range(n + m): | |
| for i2 in range(n - m): | |
| for i3 in range(n + m): | |
| for i4 in range(n + m): | |
| for i5 in range(n + m): | |
| for i6 in range(n + m): | |
| mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m] | |
| if prov(mass, now) and ppp: | |
| print('Yes') | |
| print(*mass[:n]) | |
| print(*mass[n:]) | |
| ppp = False | |
| if ppp: | |
| print('No') | |
| else: | |
| mass = [[] for i in range(m)] | |
| mass1 = [[] for i in range(n)] | |
| for i in range(n): | |
| now = input() | |
| for k in range(m): | |
| if now[k] == '<': | |
| mass[k].append(1) | |
| mass1[i].append(-1) | |
| elif now[k] == '=': | |
| mass[k].append(0) | |
| mass1[i].append(0) | |
| else: | |
| mass[k].append(-1) | |
| mass1[i].append(1) | |
| for i in range(m): | |
| mass[i].append(i) | |
| for i in range(n): | |
| mass1[i].append(i) | |
| mass.sort() | |
| mass1.sort() | |
| arr = prog(mass, n, m) | |
| arr1 = prog(mass1, m, n) | |
| if arr == False or arr1 == False: | |
| print('No') | |
| else: | |
| print('Yes') | |
| print(*arr1) | |
| print(*arr) | |
| ``` | |
| ### 014 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(2, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 015 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i - 1]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 016 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j + len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j + len(grid))) | |
| if grid[i][j] == '>': | |
| if not leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if any((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 017 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == -1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 018 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-0) | |
| ``` | |
| ### 019 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil(y * 1 / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 020 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 021 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 5 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 022 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) != True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 023 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[0] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 024 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 3 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 025 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return -2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 026 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if not i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 027 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][0] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 028 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i - 1] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 029 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 4) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 030 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += -1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 1 | |
| print(cnt) | |
| ``` | |
| ### 031 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(3, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 032 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i - 1]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 033 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2 - 1] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 034 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 0 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 035 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| marks1[i][1] += 1.0 | |
| elif test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -0 | |
| lastItem = [0, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 036 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(2000) | |
| def dfs1(v, mintime): | |
| localtime = mintime | |
| vis1[v] = 1 | |
| for v2 in range(m): | |
| if a[v][v2] == '>': | |
| if not vis2[v2]: | |
| dfs2(v2, 1) | |
| localtime = max(localtime, time2[v2] + 1) | |
| for v2 in range(m): | |
| if not a[v][v2] == '=': | |
| if not vis2[v2]: | |
| dfs2(v2, localtime) | |
| localtime = max(localtime, time2[v2]) | |
| time1[v] = localtime | |
| def dfs2(v, mintime): | |
| localtime = mintime | |
| vis2[v] = 1 | |
| for v2 in range(n): | |
| if a[v2][v] == '<': | |
| if not vis1[v2]: | |
| dfs1(v2, 1) | |
| localtime = max(localtime, time1[v2] + 1) | |
| for v2 in range(n): | |
| if a[v2][v] == '=': | |
| if not vis1[v2]: | |
| dfs1(v2, localtime) | |
| localtime = max(localtime, time1[v2]) | |
| time2[v] = localtime | |
| n, m = list(map(int, input().split())) | |
| a = [input() for i in range(n)] | |
| time1 = [0] * n | |
| time2 = [0] * m | |
| vis1 = [0] * n | |
| vis2 = [0] * m | |
| time = 0 | |
| try: | |
| for v in range(n): | |
| if not time1[v]: | |
| dfs1(v, 1) | |
| for v in range(m): | |
| if not time2[v]: | |
| dfs2(v, 1) | |
| correct = True | |
| for v1 in range(n): | |
| for v2 in range(m): | |
| if a[v1][v2] == '=' and time1[v1] != time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '>' and time1[v1] <= time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '<' and time1[v1] >= time2[v2]: | |
| correct = False | |
| if correct: | |
| print('Yes') | |
| print(*time1) | |
| print(*time2) | |
| else: | |
| print('No') | |
| except RecursionError: | |
| print('No') | |
| ``` | |
| ### 037 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(2000) | |
| def dfs1(v, mintime): | |
| localtime = mintime | |
| vis1[v] = 1 | |
| for v2 in range(m): | |
| if a[v][v2] == '>': | |
| if not vis2[v2]: | |
| dfs2(v2, 1) | |
| localtime = max(localtime, time2[v2] + 1) | |
| for v2 in range(m): | |
| if a[v][v2] == '=': | |
| if not vis2[v2]: | |
| dfs2(v2, localtime) | |
| localtime = max(localtime, time2[v2]) | |
| time1[v] = localtime | |
| def dfs2(v, mintime): | |
| localtime = mintime | |
| vis2[v] = 1 | |
| for v2 in range(n): | |
| if a[v2][v] == '<': | |
| if not vis1[v2]: | |
| dfs1(v2, 1) | |
| localtime = max(localtime, time1[v2] + 1) | |
| for v2 in range(n): | |
| if not a[v2][v] == '=': | |
| if not vis1[v2]: | |
| dfs1(v2, localtime) | |
| localtime = max(localtime, time1[v2]) | |
| time2[v] = localtime | |
| n, m = list(map(int, input().split())) | |
| a = [input() for i in range(n)] | |
| time1 = [0] * n | |
| time2 = [0] * m | |
| vis1 = [0] * n | |
| vis2 = [0] * m | |
| time = 0 | |
| try: | |
| for v in range(n): | |
| if not time1[v]: | |
| dfs1(v, 1) | |
| for v in range(m): | |
| if not time2[v]: | |
| dfs2(v, 1) | |
| correct = True | |
| for v1 in range(n): | |
| for v2 in range(m): | |
| if a[v1][v2] == '=' and time1[v1] != time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '>' and time1[v1] <= time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '<' and time1[v1] >= time2[v2]: | |
| correct = False | |
| if correct: | |
| print('Yes') | |
| print(*time1) | |
| print(*time2) | |
| else: | |
| print('No') | |
| except RecursionError: | |
| print('No') | |
| ``` | |
| ### 038 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 039 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 3 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 040 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(-1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 041 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i + 1] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 042 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = max(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 043 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p - 1] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 044 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(4, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 045 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j + 1] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 046 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 or t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 047 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[(i ^ 1) + 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 048 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i + 1] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 049 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 2: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 050 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i - 1] ^ self.tree[i ^ 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 051 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in reversed(range(n - 1, -1, -1)): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 052 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 7) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 053 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(2): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 054 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 + 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 055 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 4 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 056 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - -2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 057 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 058 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] += 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 059 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] += 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 060 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1 - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 061 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if (A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1]))) and (A_[1][1] and A_[4][1] and A_[7][1]) and (A_[2][1] and A_[5][1] and A_[8][1]) and (A_[3][1] and A_[4][1] and A_[5][1]) and (A_[6][1] and A_[7][1] and A_[8][1]) and (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j + len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j + len(grid))) | |
| if grid[i][j] == '>': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if any((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if not l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 063 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v * plucked[i] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 064 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[i | 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 065 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v + plucked[i] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 066 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i] ^ self.tree[(i ^ 1) - 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 067 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1 - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 068 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j - len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j + len(grid))) | |
| if grid[i][j] == '>': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if any((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = -3 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 070 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 071 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][0] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 072 | |
| ```python | |
| def prov(mass, now): | |
| check = True | |
| for i in range(n): | |
| for k in range(m): | |
| if now[i][k] == '>' and mass[i] <= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '<' and mass[i] >= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '=' and mass[i] != mass[n + k]: | |
| check = False | |
| break | |
| if not check: | |
| break | |
| return check | |
| def prog(mass, n, m): | |
| prov = True | |
| for i in range(1, m): | |
| for k in range(n): | |
| if mass[i][k] < mass[i - 1][k]: | |
| prov = False | |
| break | |
| if not prov: | |
| break | |
| if not prov: | |
| return False | |
| else: | |
| mass_new = [] | |
| for i in range(1, m): | |
| mass_n = [] | |
| for k in range(n): | |
| mass_n.append(mass[i][k] - mass[i - 1][k]) | |
| mass_new.append(max(mass_n)) | |
| arr = [1 for i in range(m)] | |
| now = 1 | |
| if 1 in mass[0][:-1]: | |
| now += 1 | |
| arr = [2 for i in range(m)] | |
| for i in range(1, m): | |
| now += mass_new[i - 1] | |
| arr[mass[i][-1]] = now | |
| return arr | |
| n, m = map(int, input().split()) | |
| if n + m <= 6: | |
| now = [] | |
| for i in range(n): | |
| now.append(input()) | |
| ppp = True | |
| for i1 in range(n + m): | |
| for i2 in reversed(range(n + m)): | |
| for i3 in range(n + m): | |
| for i4 in range(n + m): | |
| for i5 in range(n + m): | |
| for i6 in range(n + m): | |
| mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m] | |
| if prov(mass, now) and ppp: | |
| print('Yes') | |
| print(*mass[:n]) | |
| print(*mass[n:]) | |
| ppp = False | |
| if ppp: | |
| print('No') | |
| else: | |
| mass = [[] for i in range(m)] | |
| mass1 = [[] for i in range(n)] | |
| for i in range(n): | |
| now = input() | |
| for k in range(m): | |
| if now[k] == '<': | |
| mass[k].append(1) | |
| mass1[i].append(-1) | |
| elif now[k] == '=': | |
| mass[k].append(0) | |
| mass1[i].append(0) | |
| else: | |
| mass[k].append(-1) | |
| mass1[i].append(1) | |
| for i in range(m): | |
| mass[i].append(i) | |
| for i in range(n): | |
| mass1[i].append(i) | |
| mass.sort() | |
| mass1.sort() | |
| arr = prog(mass, n, m) | |
| arr1 = prog(mass1, m, n) | |
| if arr == False or arr1 == False: | |
| print('No') | |
| else: | |
| print('Yes') | |
| print(*arr1) | |
| print(*arr) | |
| ``` | |
| ### 073 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] * d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 074 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| marks1[i][1] += 1.0 | |
| elif not test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -1 | |
| lastItem = [0, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 075 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 3): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 076 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 3 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 077 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(1, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 078 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 079 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = (a_3_3[0][index] + a_3_3[1][index]) * a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 080 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1 - 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 081 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in reversed(range(N))] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 082 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 083 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k + 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 084 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 085 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= -1 | |
| print(cnt) | |
| ``` | |
| ### 086 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 4 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 087 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][0] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 088 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(1, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(0, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 089 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j + len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j + len(grid))) | |
| if grid[i][j] == '>': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if any((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 090 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 0 | |
| print(cnt) | |
| ``` | |
| ### 091 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i - 1] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 092 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 093 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif not z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 094 | |
| ```python | |
| class PalArray: | |
| P = 998244355 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 095 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 2 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 096 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 097 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] == 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 098 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 099 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v - plucked[i] else v - plucked[i + 1] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 100 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if not test == '>': | |
| marks1[i][1] += 1.0 | |
| elif test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -1 | |
| lastItem = [0, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 101 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and ((A_[1][1] and A_[2][1]) and (A_[3][1] and A_[6][1]) and (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res -= val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 103 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 104 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][1] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 105 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1 - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 106 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 107 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if n * y % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 108 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-2) | |
| ``` | |
| ### 109 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][0] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 110 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 1: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 111 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] & tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[1]) | |
| ``` | |
| ### 112 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if y - 1 & d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 113 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(0): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 114 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == -1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 115 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 1 | |
| print(cnt) | |
| ``` | |
| ### 116 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][0] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 117 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop(0) | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 118 | |
| ```python | |
| import sys | |
| class SegmTree: | |
| def __init__(self, size): | |
| N = 1 | |
| while N < size: | |
| N <<= 1 | |
| self.N = N | |
| self.tree = [0] * (2 * N) | |
| def modify(self, i, value): | |
| i += self.N | |
| self.tree[i] = value | |
| toXOR = False | |
| while i > 1: | |
| if toXOR: | |
| self.tree[i >> 1] = self.tree[i + 1] ^ self.tree[i ^ 1] | |
| else: | |
| self.tree[i >> 1] = self.tree[i] | self.tree[i ^ 1] | |
| toXOR = not toXOR | |
| i >>= 1 | |
| reader = (line.rstrip() for line in sys.stdin) | |
| input = reader.__next__ | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| st = SegmTree(1 << n) | |
| for i, value in enumerate(a): | |
| st.modify(i, value) | |
| for _ in range(m): | |
| p, b = list(map(int, input().split())) | |
| st.modify(p - 1, b) | |
| print(st.tree[1]) | |
| ``` | |
| ### 119 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 5 | |
| print(cnt) | |
| ``` | |
| ### 120 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(-2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 121 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::-2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 122 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j + len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j + len(grid))) | |
| if grid[i][j] == '>': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if all((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 123 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = -3 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 124 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == -1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 125 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244355 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 126 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & -1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 127 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - max(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 128 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::--1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 129 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 1: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 130 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 131 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = max(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 132 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 2 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 133 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if not ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 134 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| marks1[i][1] += 1.0 | |
| elif test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -1 | |
| lastItem = [2, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 135 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v * d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 136 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 3 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 137 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 - 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 138 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1 + 1]))) | |
| ``` | |
| ### 139 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[-3] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 140 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1 - 1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 141 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 3: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 142 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a < b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 143 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i - 1] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 144 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 1): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 145 | |
| ```python | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k == 0: | |
| best = 0 | |
| curr = sum(a) | |
| for i in range(n): | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| print(best) | |
| elif k == 1: | |
| best = sum(a[:-1 + 1]) - min(d[:-1]) | |
| other = sum(a) | |
| other -= sorted(d)[0] | |
| other -= sorted(d)[1] | |
| curr = sum(a) | |
| for i in range(n): | |
| if i: | |
| best = max(best, curr - d[i]) | |
| curr -= a[i] | |
| o2 = sum(a) - min(a[1:]) - d[0] | |
| print(max((best, other, 0, o2))) | |
| else: | |
| print(max((sum(a) - min(d[:-1]), 0, a[-1] - d[-1]))) | |
| ``` | |
| ### 146 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + -1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 147 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == -1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 148 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 3 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 149 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) != True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 150 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 5: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 151 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i - 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 152 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| marks1[i][1] += 1.0 | |
| elif test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -1 | |
| lastItem = [0, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 153 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil(n * y / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 154 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 7) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 155 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + -1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 156 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(0): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 157 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - -1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 158 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i + 1] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 0] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 160 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244350 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 161 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = -1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 162 | |
| ```python | |
| from sys import stdin, stderr | |
| import random | |
| import cProfile | |
| def readInts(): | |
| return map(int, stdin.readline().strip().split()) | |
| def print_err(*args, **kwargs): | |
| print(*args, file=stderr, **kwargs) | |
| def solve(vs): | |
| return None | |
| def generate_tree(n, ns): | |
| out = [0 for _ in range(2 ** (n + 1))] | |
| def gt(nix, left, right, op): | |
| if left + 1 == right: | |
| out[nix] = ns[left] | |
| return out[nix] | |
| mid = (left + right) // 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = gt(nL, left, mid, not op) | |
| vR = gt(nR, mid, right, not op) | |
| if not op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| out[nix] = v | |
| return v | |
| gt(0, 0, 2 ** n, n % 2 == 0) | |
| return out | |
| def alter_tree2(n, t, p, b): | |
| def at(nix, width, offp, op): | |
| if width == 1: | |
| t[nix] = b | |
| return b | |
| width //= 2 | |
| nL = nix * 2 + 1 | |
| nR = nix * 2 + 2 | |
| vL = t[nL] | |
| vR = t[nR] | |
| if offp >= width: | |
| vR = at(nR, width, offp - width, not op) | |
| else: | |
| vL = at(nL, width, offp, not op) | |
| if op: | |
| v = vL ^ vR | |
| else: | |
| v = vL | vR | |
| t[nix] = v | |
| return v | |
| at(0, 2 ** n, p, n % 2 == 0) | |
| def alter_tree(n, t, p, b): | |
| width = 2 ** n | |
| s = [] | |
| nix = 0 | |
| op = n % 2 == 0 | |
| while width > 1: | |
| width //= 2 | |
| if p >= width: | |
| nix2 = 2 * nix + 2 | |
| s.append((nix, nix2 - 1)) | |
| p -= width | |
| else: | |
| nix2 = 2 * nix + 1 | |
| s.append((nix, nix2 + 1)) | |
| nix = nix2 | |
| op = not op | |
| t[nix] = b | |
| v = b | |
| while s: | |
| nix, nixO = s.pop() | |
| if op: | |
| v |= t[nixO] | |
| else: | |
| v ^= t[nixO] | |
| t[nix] = v | |
| op = not op | |
| return | |
| def run(): | |
| n, m = readInts() | |
| axs = list(readInts()) | |
| t = generate_tree(n, axs) | |
| for _ in range(m): | |
| p, b = readInts() | |
| alter_tree(n, t, p - 1, b) | |
| print(t[0]) | |
| def test(): | |
| n = 17 | |
| ns = [] | |
| vs100 = list(range(100)) | |
| for _ in range(2 ** 17): | |
| ns.append(random.choice(vs100)) | |
| t = generate_tree(n, ns) | |
| t2 = generate_tree(n, ns) | |
| for _ in range(100000): | |
| v1 = random.choice(vs100) | |
| v2 = random.choice(vs100) | |
| alter_tree(n, t, v1, v2) | |
| alter_tree2(n, t2, v1, v2) | |
| print(all(map(lambda x: x[0] == x[1], zip(t, t2)))) | |
| print(t[0] == t2[0]) | |
| run() | |
| ``` | |
| ### 163 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 2 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 0 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 164 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][-2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 165 | |
| ```python | |
| def prov(mass, now): | |
| check = True | |
| for i in range(n): | |
| for k in range(m): | |
| if now[i][k] == '>' and mass[i] <= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '<' and mass[i] >= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '=' and mass[i] != mass[n + k]: | |
| check = False | |
| break | |
| if not check: | |
| break | |
| return check | |
| def prog(mass, n, m): | |
| prov = True | |
| for i in range(1, m): | |
| for k in range(n): | |
| if mass[i][k] < mass[i - 1][k]: | |
| prov = False | |
| break | |
| if not prov: | |
| break | |
| if not prov: | |
| return False | |
| else: | |
| mass_new = [] | |
| for i in range(1, m): | |
| mass_n = [] | |
| for k in range(n): | |
| mass_n.append(mass[i][k] - mass[i - 1][k]) | |
| mass_new.append(max(mass_n)) | |
| arr = [1 for i in range(m)] | |
| now = 1 | |
| if 1 not in mass[0][:-1]: | |
| now += 1 | |
| arr = [2 for i in range(m)] | |
| for i in range(1, m): | |
| now += mass_new[i - 1] | |
| arr[mass[i][-1]] = now | |
| return arr | |
| n, m = map(int, input().split()) | |
| if n + m <= 6: | |
| now = [] | |
| for i in range(n): | |
| now.append(input()) | |
| ppp = True | |
| for i1 in range(n + m): | |
| for i2 in range(n + m): | |
| for i3 in range(n + m): | |
| for i4 in range(n + m): | |
| for i5 in range(n + m): | |
| for i6 in range(n + m): | |
| mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m] | |
| if prov(mass, now) and ppp: | |
| print('Yes') | |
| print(*mass[:n]) | |
| print(*mass[n:]) | |
| ppp = False | |
| if ppp: | |
| print('No') | |
| else: | |
| mass = [[] for i in range(m)] | |
| mass1 = [[] for i in range(n)] | |
| for i in range(n): | |
| now = input() | |
| for k in range(m): | |
| if now[k] == '<': | |
| mass[k].append(1) | |
| mass1[i].append(-1) | |
| elif now[k] == '=': | |
| mass[k].append(0) | |
| mass1[i].append(0) | |
| else: | |
| mass[k].append(-1) | |
| mass1[i].append(1) | |
| for i in range(m): | |
| mass[i].append(i) | |
| for i in range(n): | |
| mass1[i].append(i) | |
| mass.sort() | |
| mass1.sort() | |
| arr = prog(mass, n, m) | |
| arr1 = prog(mass1, m, n) | |
| if arr == False or arr1 == False: | |
| print('No') | |
| else: | |
| print('Yes') | |
| print(*arr1) | |
| print(*arr) | |
| ``` | |
| ### 166 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 167 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-4) | |
| ``` | |
| ### 168 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == -2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 169 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-5) | |
| ``` | |
| ### 170 | |
| ```python | |
| from math import log | |
| import sys | |
| def buildTree(arr): | |
| n = len(arr) | |
| tree = [0] * n + arr | |
| for i in range(n - 1, 0, -1): | |
| z = int(log(i, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| elif z % 2 == 0: | |
| tree[i] = tree[2 * i] | tree[2 * i + 1] | |
| else: | |
| tree[i] = tree[2 * i] ^ tree[2 * i + 1] | |
| return tree | |
| def updateTree(tree, ind, value, n): | |
| ind += n | |
| tree[ind] = value | |
| while ind > 1: | |
| ind //= 2 | |
| z = int(log(ind, 2)) | |
| if N % 2 == 0: | |
| if z % 2 == 0: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| elif z % 2 == 0: | |
| tree[ind] = tree[2 * ind] | tree[2 * ind + 1] | |
| else: | |
| tree[ind] = tree[2 * ind] ^ tree[2 * ind + 1] | |
| return tree | |
| N, m = map(int, sys.stdin.readline().strip().split()) | |
| arr = list(map(int, sys.stdin.readline().strip().split())) | |
| tree = buildTree(arr) | |
| for i in range(m): | |
| ind, val = map(int, sys.stdin.readline().strip().split()) | |
| tree = updateTree(tree, ind - 1, val, len(arr)) | |
| print(tree[-3]) | |
| ``` | |
| ### 171 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 2: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 172 | |
| ```python | |
| import heapq | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| s = sum(a) | |
| pre = [0] | |
| j = 0 | |
| while j < n: | |
| pre.append(a[j] + pre[-1]) | |
| j += 1 | |
| ab = [] | |
| af = [] | |
| af1 = [] | |
| j = n - 1 | |
| while j >= 0: | |
| heapq.heappush(af, -(s - pre[j] - b[j])) | |
| af1.append(-af[0]) | |
| j += -1 | |
| af1.reverse() | |
| if k == 0: | |
| j = 0 | |
| ans = 0 | |
| while j < n: | |
| ans = max(ans, s - pre[j] - b[j]) | |
| j += 1 | |
| print(ans) | |
| elif k == 1: | |
| j = 0 | |
| m = float('inf') | |
| ans = 2 | |
| while j < n - 1: | |
| m = min(m, b[j]) | |
| if j < n - 2: | |
| ans = max(ans, max(0, pre[j + 1] - m) + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1], s - b[0] - a[j + 1]) | |
| else: | |
| ans = max(ans, pre[j + 1] - m + max(0, af1[j + 1]), s - pre[j] - b[j] - a[j + 1]) | |
| j += 1 | |
| j = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - a[-1] - b[j] + max(0, a[-1] - b[-1])) | |
| j += 1 | |
| print(ans) | |
| else: | |
| j = 0 | |
| ans = 0 | |
| while j < n - 1: | |
| ans = max(ans, s - b[j]) | |
| j += 1 | |
| ans = max(ans, a[-1] - b[-1]) | |
| print(ans) | |
| ``` | |
| ### 173 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 2 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 174 | |
| ```python | |
| def prov(mass, now): | |
| check = True | |
| for i in range(n): | |
| for k in range(m): | |
| if now[i][k] == '>' and mass[i] <= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '<' and mass[i] >= mass[n + k]: | |
| check = False | |
| break | |
| elif now[i][k] == '=' and mass[i] != mass[n + k]: | |
| check = False | |
| break | |
| if not check: | |
| break | |
| return check | |
| def prog(mass, n, m): | |
| prov = True | |
| for i in range(1, m): | |
| for k in range(n): | |
| if mass[i][k] < mass[i - 1][k]: | |
| prov = False | |
| break | |
| if not prov: | |
| break | |
| if not prov: | |
| return False | |
| else: | |
| mass_new = [] | |
| for i in range(1, m): | |
| mass_n = [] | |
| for k in range(n): | |
| mass_n.append(mass[i][k] - mass[i - 1][k]) | |
| mass_new.append(max(mass_n)) | |
| arr = [1 for i in range(m)] | |
| now = 1 | |
| if 1 in mass[0][:-1]: | |
| now += 1 | |
| arr = [2 for i in range(m)] | |
| for i in range(1, m): | |
| now += mass_new[i - 1] | |
| arr[mass[i][-1]] = now | |
| return arr | |
| n, m = map(int, input().split()) | |
| if n + m <= 6: | |
| now = [] | |
| for i in range(n): | |
| now.append(input()) | |
| ppp = True | |
| for i1 in range(n + m): | |
| for i2 in range(n * m): | |
| for i3 in range(n + m): | |
| for i4 in range(n + m): | |
| for i5 in range(n + m): | |
| for i6 in range(n + m): | |
| mass = [i1 + 1, i2 + 1, i3 + 1, i4 + 1, i5 + 1, i6 + 1][:n + m] | |
| if prov(mass, now) and ppp: | |
| print('Yes') | |
| print(*mass[:n]) | |
| print(*mass[n:]) | |
| ppp = False | |
| if ppp: | |
| print('No') | |
| else: | |
| mass = [[] for i in range(m)] | |
| mass1 = [[] for i in range(n)] | |
| for i in range(n): | |
| now = input() | |
| for k in range(m): | |
| if now[k] == '<': | |
| mass[k].append(1) | |
| mass1[i].append(-1) | |
| elif now[k] == '=': | |
| mass[k].append(0) | |
| mass1[i].append(0) | |
| else: | |
| mass[k].append(-1) | |
| mass1[i].append(1) | |
| for i in range(m): | |
| mass[i].append(i) | |
| for i in range(n): | |
| mass1[i].append(i) | |
| mass.sort() | |
| mass1.sort() | |
| arr = prog(mass, n, m) | |
| arr1 = prog(mass1, m, n) | |
| if arr == False or arr1 == False: | |
| print('No') | |
| else: | |
| print('Yes') | |
| print(*arr1) | |
| print(*arr) | |
| ``` | |
| ### 175 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] >= day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 176 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j - 1][int(p * 2)] ^ t[j][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 177 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(2): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 178 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in reversed(range(1, l)): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 179 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, -1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 180 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = (a_3_3[0][0] + a_3_3[1][1]) * a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 181 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v - plucked[i + 1] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 182 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + -1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 183 | |
| ```python | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| d = list(map(int, input().split())) | |
| if k >= 2: | |
| m = min(d[:-1]) | |
| print(max(sum(a) - m, a[-1] - d[-1], 0)) | |
| elif k == 0: | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| print(max(rightans)) | |
| else: | |
| leftd = int(1000000000.0) | |
| leftsum = 0 | |
| rightans = [0] * n | |
| cursum = 0 | |
| ans = 0 | |
| for i in range(n - 1, -1, -1): | |
| cursum += a[i] | |
| rightans[i] = max(cursum - d[i], 0) | |
| for i in range(n - 1, 0, -1): | |
| rightans[i - 1] = max(rightans[i - 1], rightans[i]) | |
| for i in range(n - 1): | |
| leftd = min(leftd, d[i]) | |
| leftsum += a[i] | |
| curans = max(leftsum - leftd, 0) + rightans[i + 1] | |
| ans = max(ans, curans) | |
| cursum = 0 | |
| rightmin = int(1000000000.0) | |
| for i in reversed(range(n - 1, -1, -1)): | |
| cursum += a[i] | |
| ans = max(ans, cursum - rightmin - d[i]) | |
| rightmin = min(rightmin, a[i]) | |
| print(ans) | |
| ``` | |
| ### 184 | |
| ```python | |
| import sys | |
| n, k = list(map(int, input().split(' '))) | |
| activate = list(map(int, input().split(' '))) | |
| deactivate = list(map(int, input().split(' '))) | |
| presum = [0 for _ in range(n + 2)] | |
| for i in range(1, n + 1): | |
| presum[i] = presum[i - 1] + activate[i - 1 - 1] | |
| ret = 0 | |
| if k == 0: | |
| for i in range(1, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| elif k == 1: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n - 1] - deactivate[i - 1]) | |
| if deactivate[n - 1] < activate[n - 1]: | |
| ret += activate[n - 1] - deactivate[n - 1] | |
| deac = sorted(deactivate[1:n - 1]) | |
| ac = sorted(activate[1:n - 1]) | |
| ret = max(ret, presum[n] - deactivate[0] - ac[0]) | |
| ret = max(ret, presum[n] - deactivate[0] - deac[0]) | |
| ret = max(ret, presum[n] - sorted(deactivate[0:n - 1])[0] - sorted(deactivate[0:n - 1])[1]) | |
| for i in range(2, n + 1): | |
| ret = max(ret, presum[n] - presum[i - 1] - deactivate[i - 1]) | |
| else: | |
| for i in range(1, n): | |
| ret = max(ret, presum[n] - deactivate[i - 1]) | |
| ret = max(ret, activate[n - 1] - deactivate[n - 1]) | |
| print(ret) | |
| ``` | |
| ### 185 | |
| ```python | |
| import time | |
| debug = False | |
| n1, m2 = list(map(int, input().split())) | |
| tests = [] | |
| for i in range(n1): | |
| tests.append(list(input())) | |
| if debug: | |
| print(tests) | |
| begin = time.time() | |
| if debug: | |
| print('---') | |
| marks1 = [] | |
| result1 = [] | |
| for i in range(n1): | |
| marks1.append([i, 0.0]) | |
| result1.append(0) | |
| marks2 = [] | |
| result2 = [] | |
| for j in range(m2): | |
| marks2.append([j, 0.0]) | |
| result2.append(0) | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| marks1[i][1] -= 1.0 | |
| elif test == '<': | |
| marks2[j][1] += 1.0 | |
| else: | |
| marks1[i][1] += 0.0001 | |
| marks2[j][1] += 0.0001 | |
| marks1.sort(key=lambda val: val[1]) | |
| marks2.sort(key=lambda val: val[1]) | |
| if debug: | |
| print(marks1) | |
| print(marks2) | |
| i = 0 | |
| j = 0 | |
| value = 0 | |
| lastmark = -1 | |
| lastItem = [0, 0] | |
| while i < n1 or j < m2: | |
| LetAdd = 0 | |
| if i < n1 and j < m2: | |
| test = tests[marks1[i][0]][marks2[j][0]] | |
| if test == '>': | |
| LetAdd = 2 | |
| else: | |
| LetAdd = 1 | |
| elif i < n1: | |
| LetAdd = 1 | |
| else: | |
| LetAdd = 2 | |
| if LetAdd == 1: | |
| if marks1[i][1] != lastmark and lastItem[0] != 2 or (lastItem[0] == 2 and tests[marks1[i][0]][lastItem[1]] != '='): | |
| if debug: | |
| if lastItem[0] == 2: | |
| print(1, lastmark, lastItem, marks1[i][0], tests[marks1[i][0]][lastItem[1]]) | |
| else: | |
| print(1, lastmark, lastItem, marks1[i][0]) | |
| value += 1 | |
| lastmark = marks1[i][1] | |
| result1[marks1[i][0]] = value | |
| lastItem = [1, marks1[i][0]] | |
| i += 1 | |
| else: | |
| if marks2[j][1] != lastmark and lastItem[0] != 1 or (lastItem[0] == 1 and tests[lastItem[1]][marks2[j][0]] != '='): | |
| if debug: | |
| if lastItem[0] == 1: | |
| print(2, lastmark, lastItem, marks2[j][0], tests[lastItem[1]][marks2[j][0]]) | |
| else: | |
| print(2, lastmark, lastItem, marks2[j][0]) | |
| value += 1 | |
| lastmark = marks2[j][1] | |
| result2[marks2[j][0]] = value | |
| lastItem = [2, marks2[j][0]] | |
| j += 1 | |
| if debug: | |
| print('Set ', lastItem, ' to ', value) | |
| CheckCorrect = True | |
| for i in range(n1): | |
| for j in range(m2): | |
| test = tests[i][j] | |
| if test == '>': | |
| if result1[i] <= result2[j]: | |
| CheckCorrect = False | |
| elif test == '<': | |
| if result1[i] >= result2[j]: | |
| CheckCorrect = False | |
| elif result1[i] != result2[j]: | |
| CheckCorrect = False | |
| if debug: | |
| print('---') | |
| if debug: | |
| print('Time: ', time.time() - begin) | |
| if CheckCorrect: | |
| print('Yes') | |
| else: | |
| print('No') | |
| if CheckCorrect or debug: | |
| print(*result1) | |
| print(*result2) | |
| ``` | |
| ### 186 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k * 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 187 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti > ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 188 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == -1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 189 | |
| ```python | |
| from operator import or_, xor | |
| import sys | |
| n, m = map(int, input().split()) | |
| t = [list(map(int, input().split()))] | |
| for i in range(n): | |
| if i & 1 == 0: | |
| t += [[t[i][j] | t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| else: | |
| t += [[t[i][j] ^ t[i][j + 1] for j in range(0, len(t[i]), 2)]] | |
| for s in sys.stdin: | |
| p, b = s.split() | |
| p = int(p) - 1 | |
| t[0][p] = int(b) | |
| for j in range(n): | |
| p = int(p / 2) | |
| if j & 1 == 0: | |
| t[j + 1][p] = t[j][int(p * 2)] | t[j][int(p * 2) + 1] | |
| else: | |
| t[j + 1][p] = t[j][int(p * 2)] ^ t[j - 1][int(p * 2) + 1] | |
| sys.stdout.write(str(t[-1][0]) + '\n') | |
| ``` | |
| ### 190 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(2000) | |
| def dfs1(v, mintime): | |
| localtime = mintime | |
| vis1[v] = 1 | |
| for v2 in range(m): | |
| if a[v][v2] == '>': | |
| if not vis2[v2]: | |
| dfs2(v2, 1) | |
| localtime = max(localtime, time2[v2] + 1) | |
| for v2 in range(m): | |
| if a[v][v2] == '=': | |
| if not vis2[v2]: | |
| dfs2(v2, localtime) | |
| localtime = max(localtime, time2[v2]) | |
| time1[v] = localtime | |
| def dfs2(v, mintime): | |
| localtime = mintime | |
| vis2[v] = 1 | |
| for v2 in range(n): | |
| if a[v2][v] == '<': | |
| if not vis1[v2]: | |
| dfs1(v2, 1) | |
| localtime = max(localtime, time1[v2] + 1) | |
| for v2 in range(n): | |
| if a[v2][v] == '=': | |
| if not vis1[v2]: | |
| dfs1(v2, localtime) | |
| localtime = max(localtime, time1[v2]) | |
| time2[v] = localtime | |
| n, m = list(map(int, input().split())) | |
| a = [input() for i in range(n)] | |
| time1 = [0] * n | |
| time2 = [0] * m | |
| vis1 = [4] * n | |
| vis2 = [0] * m | |
| time = 0 | |
| try: | |
| for v in range(n): | |
| if not time1[v]: | |
| dfs1(v, 1) | |
| for v in range(m): | |
| if not time2[v]: | |
| dfs2(v, 1) | |
| correct = True | |
| for v1 in range(n): | |
| for v2 in range(m): | |
| if a[v1][v2] == '=' and time1[v1] != time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '>' and time1[v1] <= time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '<' and time1[v1] >= time2[v2]: | |
| correct = False | |
| if correct: | |
| print('Yes') | |
| print(*time1) | |
| print(*time2) | |
| else: | |
| print('No') | |
| except RecursionError: | |
| print('No') | |
| ``` | |
| ### 191 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 192 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(2000) | |
| def dfs1(v, mintime): | |
| localtime = mintime | |
| vis1[v] = 1 | |
| for v2 in range(m): | |
| if a[v][v2] == '>': | |
| if not vis2[v2 - 1]: | |
| dfs2(v2, 1) | |
| localtime = max(localtime, time2[v2] + 1) | |
| for v2 in range(m): | |
| if a[v][v2] == '=': | |
| if not vis2[v2]: | |
| dfs2(v2, localtime) | |
| localtime = max(localtime, time2[v2]) | |
| time1[v] = localtime | |
| def dfs2(v, mintime): | |
| localtime = mintime | |
| vis2[v] = 1 | |
| for v2 in range(n): | |
| if a[v2][v] == '<': | |
| if not vis1[v2]: | |
| dfs1(v2, 1) | |
| localtime = max(localtime, time1[v2] + 1) | |
| for v2 in range(n): | |
| if a[v2][v] == '=': | |
| if not vis1[v2]: | |
| dfs1(v2, localtime) | |
| localtime = max(localtime, time1[v2]) | |
| time2[v] = localtime | |
| n, m = list(map(int, input().split())) | |
| a = [input() for i in range(n)] | |
| time1 = [0] * n | |
| time2 = [0] * m | |
| vis1 = [0] * n | |
| vis2 = [0] * m | |
| time = 0 | |
| try: | |
| for v in range(n): | |
| if not time1[v]: | |
| dfs1(v, 1) | |
| for v in range(m): | |
| if not time2[v]: | |
| dfs2(v, 1) | |
| correct = True | |
| for v1 in range(n): | |
| for v2 in range(m): | |
| if a[v1][v2] == '=' and time1[v1] != time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '>' and time1[v1] <= time2[v2]: | |
| correct = False | |
| if a[v1][v2] == '<' and time1[v1] >= time2[v2]: | |
| correct = False | |
| if correct: | |
| print('Yes') | |
| print(*time1) | |
| print(*time2) | |
| else: | |
| print('No') | |
| except RecursionError: | |
| print('No') | |
| ``` | |
| ### 193 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k < n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 194 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 195 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 1: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 196 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -5): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 197 | |
| ```python | |
| import collections | |
| import heapq | |
| import bisect | |
| import math | |
| import time | |
| class Solution2: | |
| def solve(self, A1, A2): | |
| pass | |
| def gcd(a, b): | |
| if not b: | |
| return a | |
| return gcd(b, a % b) | |
| def lcm(a, b): | |
| return b * a // gcd(b, a) | |
| class Solution: | |
| def solve(self, grid): | |
| def union(i, j): | |
| leader_i, leader_j = (find(i), find(j)) | |
| sets[leader_j] = sets[i] = sets[j] = leader_i | |
| def find(i): | |
| while i != sets[i]: | |
| i = sets[i] | |
| return i | |
| N = len(grid) + len(grid[0]) | |
| sets = list(range(N)) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| if grid[i][j] == '=': | |
| union(i, j + len(grid)) | |
| graph = collections.defaultdict(set) | |
| inc = collections.defaultdict(set) | |
| for i in range(len(grid)): | |
| for j in range(len(grid[0])): | |
| leader_i, leader_j = (find(i), find(j - len(grid))) | |
| if grid[i][j] == '>': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_j].add(leader_i) | |
| inc[leader_i].add(leader_j) | |
| elif grid[i][j] == '<': | |
| if leader_i == leader_j: | |
| print('No') | |
| return | |
| graph[leader_i].add(leader_j) | |
| inc[leader_j].add(leader_i) | |
| self.levels = [0] * N | |
| def dfs(node, level): | |
| self.levels[node] = max(self.levels[node], level) | |
| if not inc[node]: | |
| seen.add(node) | |
| for next_node in graph[node]: | |
| inc[next_node].discard(node) | |
| dfs(next_node, self.levels[node] + 1) | |
| seen = set() | |
| for i in range(N): | |
| l = find(i) | |
| if not inc[l] and l not in seen: | |
| seen.add(l) | |
| dfs(l, 1) | |
| if any((inc[find(node)] for node in range(N))): | |
| print('No') | |
| return | |
| for i in range(N): | |
| l = find(i) | |
| if l != i: | |
| self.levels[i] = self.levels[l] | |
| print('Yes') | |
| print(' '.join((str(o) for o in self.levels[:len(grid)]))) | |
| print(' '.join((str(o) for o in self.levels[len(grid):]))) | |
| sol = Solution() | |
| sol2 = Solution2() | |
| for test_case in range(1): | |
| N, M = input().split() | |
| a = [] | |
| for _ in range(int(N)): | |
| a.append(input()) | |
| out = sol.solve(a) | |
| ``` | |
| ### 198 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 199 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i - 1] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 200 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-1) | |
| ``` | |
| ### 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. | |