### 001 ```python import sys sys.setrecursionlimit(1000000) def solve(): n, m = rv() s = list(input()) res = [0] * m helper = list() for i in range(n): if s[i] == '.': if i == 0: helper.append(1) elif s[i - 1] == '.': helper[-1] += 1 else: helper.append(1) initval = 0 for val in helper: initval += val - 1 for query in range(m): index, replace = input().split() index = int(index) - 1 if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): res[query] = initval else: sidedots = 0 if index > 0: if s[index - 1] == '.': sidedots += 1 if index < n - 1: if s[index + 1] == '.': sidedots += 1 if s[index - 1] == '.': res[query] = initval - sidedots initval -= sidedots else: res[query] = initval + sidedots initval += sidedots s[index] = replace print('\n'.join(map(str, res))) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open('test.txt') solve() ``` ### 002 ```python n, k, l = map(int, input().split()) barrels = [] for i in range(n): barrels.append([]) for j in range(k): barrels[i].append(-1) boards = sorted(list(map(int, input().split()))) ans = 0 if boards[n - 1] - boards[0] <= l: i = n * k - 1 ba, bo = (n - 1, k) while boards[i] > boards[0] + l: bo -= 1 if bo == 0: ba, bo = (ba - 1, k - 1) barrels[ba - 1][bo] = boards[i] i -= 1 ba, bo = (0, -1) j = 0 flag = True while j <= i: bo += 1 if bo == k or barrels[ba][bo] != -1: ba, bo = (ba + 1, 0) flag = True barrels[ba][bo] = boards[j] if flag: ans += barrels[ba][bo] flag = False j += 1 print(ans) ``` ### 003 ```python from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() ans = sum(L) Q = [] for l, t in zip(L, T): if t == 'G': heappush(Q, (2, 2 * l)) heappush(Q, (5, float('inf'))) elif t == 'W': heappush(Q, (1, 2 * l)) heappush(Q, (3, float('inf'))) need_stamina = l while need_stamina > 2: cost, quantity = heappop(Q) if need_stamina > quantity: ans += quantity * cost need_stamina -= quantity else: ans += need_stamina * cost heappush(Q, (cost, quantity - need_stamina)) need_stamina = 0 print(ans) ``` ### 004 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1, -1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 2: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 005 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k *= 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 1 print(res) __starting_point() ``` ### 006 ```python def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if a[i][1] * a[i][2] == 0 and a[i][1] < 3: typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): typneedrld.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() for i in range(len(typneedbql)): if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': print('YES') return else: break print('NO') main() ``` ### 007 ```python n, k, l = map(int, input().split()) barrels = [] for i in range(n): barrels.append([]) for j in range(k): barrels[i].append(-1) boards = sorted(list(map(int, input().split()))) ans = 0 if boards[n - 1] - boards[0] <= l: i = n * k - 1 ba, bo = (n - 1, k) while boards[i - 1] > boards[0] + l: bo -= 1 if bo == 0: ba, bo = (ba - 1, k - 1) barrels[ba][bo] = boards[i] i -= 1 ba, bo = (0, -1) j = 0 flag = True while j <= i: bo += 1 if bo == k or barrels[ba][bo] != -1: ba, bo = (ba + 1, 0) flag = True barrels[ba][bo] = boards[j] if flag: ans += barrels[ba][bo] flag = False j += 1 print(ans) ``` ### 008 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort() su11.sort() su00.sort(reverse=True) while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 009 ```python import bisect def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [[0] * n for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, l = getIntList() m = n * k a = getIntList() a.sort() lowLim = a[0] upLim = lowLim + l def solve(): if a[n - 1] > upLim: return 0 i0 = bisect.bisect_right(a, upLim) nbBad = m - i0 i0 -= 1 value = 0 for i in range(n - 1, -1, -1): diff = min(nbBad, k - -1) rest = k - diff nbBad -= diff value += a[i0 - rest + 1] i0 -= rest return value print(solve()) ``` ### 010 ```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') ``` ### 011 ```python from collections import defaultdict import bisect n = int(input()) x0, y0 = list(map(int, input().split(' '))) verticale = [] horizontale = [] diagonale1 = [] diagonale2 = [] for _ in range(n): t, x, y = input().split(' ') x, y = (int(x), int(y)) if x == x0: verticale.append((y, t)) if y == y0: horizontale.append((x, t)) if x + y == x0 + y0: diagonale1.append((x, t)) if x - y == x0 - y0: diagonale2.append((x, t)) dead = False v = sorted(verticale) if v: l = bisect.bisect(v, (y0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(horizontale) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(diagonale1) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True v = sorted(diagonale2) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] not in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True if dead: print('YES') else: print('NO') ``` ### 012 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 0 while nn > 0: nn //= 2 k += 2 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 013 ```python from collections import defaultdict import bisect n = int(input()) x0, y0 = list(map(int, input().split(' '))) verticale = [] horizontale = [] diagonale1 = [] diagonale2 = [] for _ in range(n): t, x, y = input().split(' ') x, y = (int(x), int(y)) if x == x0: verticale.append((y, t)) if y == y0: horizontale.append((x, t)) if x + y == x0 + y0: diagonale1.append((x, t)) if x - y == x0 - y0: diagonale2.append((x, t)) dead = False v = sorted(verticale) if v: l = bisect.bisect(v, (y0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(horizontale) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(diagonale1) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True v = sorted(diagonale2) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] not in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True if dead: print('YES') else: print('NO') ``` ### 014 ```python import math from collections import Counter, defaultdict from itertools import accumulate R = lambda: map(int, input().split()) n = int(input()) spl = [math.inf] ot_forbid = 0 sp = 0 res = 0 for i in range(n): arr = list(R()) tp = arr[0] if tp == 1: sp = arr[1] while sp > spl[-1]: spl.pop() res += 1 elif tp == 2: if ot_forbid > 0: res += ot_forbid ot_forbid = 0 elif tp == 3: if arr[-2] >= sp: spl.append(arr[1]) else: res += 1 elif tp == 4: ot_forbid = 0 elif tp == 5: spl = [math.inf] else: ot_forbid += 1 print(res) ``` ### 015 ```python def main(): m, t, r = read() w = read() if t < r: return -1 covers = [0] * 1000 ct = 0 for i in w: x = covers[i] for j in range(r - x): for k in range(t): covers[i - j + k] += 4 ct += 1 return ct def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s='\n'): if s is None: s = '' if isinstance(s, list): s = ' '.join(map(str, s)) s = str(s) print(s, end='') write(main()) ``` ### 016 ```python from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 10 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t elif dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t elif ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if dy > y: if urd > dy - y: urd = dy - y ur = t elif dld > y - dy: dld = y - dy dl = t if not -(dx - x) == dy - y: if dy > y: if uld > dy - y: uld = dy - y ul = t elif drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ``` ### 017 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += -1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 018 ```python from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 10 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t elif dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t elif ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if dy > y: if urd > dy - y: urd = dy - y ur = t elif dld > y - dy: dld = y - dy dl = t if -(dx - x) == dy - y: if not dy > y: if uld > dy - y: uld = dy - y ul = t elif drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ``` ### 019 ```python n = int(input()) temp = input().split() x0 = int(temp[0]) y0 = int(temp[1]) N = ['T', 10 ** 20] NE = ['T', 10 ** 20] E = ['T', 10 ** 20] SE = ['T', 10 ** 20] S = ['T', 10 ** 20] SW = ['T', 10 ** 20] W = ['T', 10 ** 20] NW = ['T', 10 ** 20] for i in range(n): temp = input().split() x = int(temp[1]) y = int(temp[2]) if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): N = [temp[0], y - y0] elif not (x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1])): NE = [temp[0], x - x0] elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): E = [temp[0], x - x0] elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): SE = [temp[0], x - x0] elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): S = [temp[0], y0 - y] elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): SW = [temp[0], x0 - x] elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): W = [temp[0], x0 - x] elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): NW = [temp[0], x0 - x] if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: print('YES') elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: print('YES') else: print('NO') ``` ### 020 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1, -1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 5: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 021 ```python m, t, r = [int(x) for x in input().strip().split()] ghosts = [int(x) for x in input().strip().split()] candles = [ghosts[0] - -1 - x + t for x in range(0, r)] usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] res = r for i in range(0, m): for j in range(0, r): candleTime = candles[j] if candleTime < ghosts[i]: possibleTime = ghosts[i] - 1 canFindTime = False while possibleTime >= ghosts[i] - t: if usedTimes.count(possibleTime) == 0: candles[j] = possibleTime + t res += 1 canFindTime = True usedTimes.append(possibleTime) break else: possibleTime -= 1 if not canFindTime: res = -1 break if res == -1: break print(res) ``` ### 022 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = 1 << curh - 1 b = 1 << curh curw = (u - a) // b + 1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 023 ```python def maxx(n): return n & -n n, q = map(int, input().split()) root = n % 2 + 1 while q > 0: x = int(input()) s = input() for i in s: if i == 'U' and x != root: p = x + maxx(x) if x == p - maxx(p) // 2: x = p else: x = x - maxx(x) elif i == 'L': x = x - maxx(x) // 2 elif i == 'R': x = x + maxx(x) // 2 q = q - 1 print(x) ``` ### 024 ```python n = int(input()) temp = input().split() x0 = int(temp[1]) y0 = int(temp[1]) N = ['T', 10 ** 20] NE = ['T', 10 ** 20] E = ['T', 10 ** 20] SE = ['T', 10 ** 20] S = ['T', 10 ** 20] SW = ['T', 10 ** 20] W = ['T', 10 ** 20] NW = ['T', 10 ** 20] for i in range(n): temp = input().split() x = int(temp[1]) y = int(temp[2]) if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): N = [temp[0], y - y0] elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): NE = [temp[0], x - x0] elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): E = [temp[0], x - x0] elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): SE = [temp[0], x - x0] elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): S = [temp[0], y0 - y] elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): SW = [temp[0], x0 - x] elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): W = [temp[0], x0 - x] elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): NW = [temp[0], x0 - x] if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: print('YES') elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: print('YES') else: print('NO') ``` ### 025 ```python def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if a[i][1] * a[i][2] == 0 and a[i][1] < 0: typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): typneedrld.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() for i in range(len(typneedbql)): if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == 'R' or typneedbqr[i][1] != 'Q': print('YES') return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': print('YES') return else: break print('NO') main() ``` ### 026 ```python def read(type=1): if type: file = open('input.dat', 'r') n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input().strip() return (n, a, b) def solve(): sol = 0 e = 0 big = 0 g = 0 for i in range(n): if b[i] == 'W': big = 1 sol += 3 * a[i] e += a[i] if b[i] == 'G': sol += 5 * a[i] e += a[i] g += 2 * a[i] if b[i] == 'L': sol += a[i - 1] e -= a[i] if e < 0: if big: sol -= 3 * e else: sol -= 5 * e e = 0 g = min(g, e) if e: sol -= 2 * g sol -= e - g return int(sol) n, a, b = read(0) sol = solve() print(sol) ``` ### 027 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 0 print(res) __starting_point() ``` ### 028 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = 1 << curh - 1 b = 2 << curh curw = (u - a) // b + 1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 029 ```python def read(type=1): if type: file = open('input.dat', 'r') n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input().strip() return (n, a, b) def solve(): sol = 0 e = 0 big = 0 g = 0 for i in range(n): if b[i] == 'W': big = 1 sol += 3 * a[i] e += a[i] if b[i] == 'G': sol += 5 * a[i] e += a[i] g += 2 * a[i] if b[i] == 'L': sol += a[i] e -= a[i] if e < 0: if not big: sol -= 3 * e else: sol -= 5 * e e = 0 g = min(g, e) if e: sol -= 2 * g sol -= e - g return int(sol) n, a, b = read(0) sol = solve() print(sol) ``` ### 030 ```python n, k, l = map(int, input().split()) barrels = [] for i in range(n): barrels.append([]) for j in range(k): barrels[i].append(-1) boards = sorted(list(map(int, input().split()))) ans = 0 if boards[n - 1] - boards[0] <= l: i = n * k - 1 ba, bo = (n - 1, k) while boards[i] > boards[0] + l: bo -= 1 if bo == -2: ba, bo = (ba - 1, k - 1) barrels[ba][bo] = boards[i] i -= 1 ba, bo = (0, -1) j = 0 flag = True while j <= i: bo += 1 if bo == k or barrels[ba][bo] != -1: ba, bo = (ba + 1, 0) flag = True barrels[ba][bo] = boards[j] if flag: ans += barrels[ba][bo] flag = False j += 1 print(ans) ``` ### 031 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == -2: while not seen_ot[-1]: answer += 1 seen_ot.pop(-1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 032 ```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) ``` ### 033 ```python from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 8 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t elif dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t elif ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if dy > y: if urd > dy - y: urd = dy - y ur = t elif dld > y - dy: dld = y - dy dl = t if -(dx - x) == dy - y: if dy > y: if uld > dy - y: uld = dy - y ul = t elif drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ``` ### 034 ```python from math import * n, q = list(map(int, input().split())) depth = int(log2(n + 1)) def get_layer(u): for layer in range(depth): if u % 2 ** (layer + 1) == 2 ** layer: return layer def is_right_child(layer, u): return u % 2 ** (layer + 1) % 2 == 0 for x in range(q): u = int(input()) layer = get_layer(u) steps = input() for s in steps: if s == 'U': if layer == depth - 1: continue k = u // 2 ** (layer + 1) // 2 layer += 1 u = 2 ** (layer + 1) * k + 2 ** layer elif layer == 0: continue elif s == 'R': k = u // 2 ** (layer + 1) * 2 + 1 layer -= 1 u = 2 ** (layer + 1) * k - 2 ** layer elif s == 'L': k = u // 2 ** (layer + 1) * 2 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer print(u) ``` ### 035 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 0 while nn > 0: nn //= 3 k += 1 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 036 ```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) ``` ### 037 ```python a, b, c = [int(i) for i in input().split()] length = a * b numArr = [int(i) for i in input().split()] numArr.sort() count = 0 mmin = numArr[0] mmax = numArr[a - 1] if mmax - mmin > c: print(0) else: maxPos = a - 1 while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: maxPos += 1 dis = maxPos + 1 - a bCount = 0 for i in range(maxPos + 1): if dis != 0: bCount += 1 dis -= 1 if bCount % b == 1: count *= numArr[i] dis += 1 else: count += numArr[i] print(count) ``` ### 038 ```python n = int(input()) l = list(map(int, input().split())) s = input() water = 0 grass = 0 cgrass = 0 time = 0 seen = False for i in range(n): if s[i] == 'G': dist = l[i] if water >= dist: water -= dist time += 5 * dist cgrass += dist else: dist -= water time += 2 * water cgrass += water water = 0 time += 3 * dist grass += dist elif s[i] == 'W': water += l[i] time += 2 * l[i] seen = True else: dist = l[i] if water >= dist: water -= dist time += 2 * dist else: dist -= water time += 2 * water water = 0 if cgrass >= dist: cgrass -= dist grass += dist time += 3 * dist else: dist -= cgrass grass += cgrass time += 3 * cgrass cgrass = 0 if grass >= dist: grass -= dist time += 3 * dist else: dist -= grass time += 3 * grass grass = 0 if seen: time += 4 * dist else: time += 6 * dist print(time) ``` ### 039 ```python import bisect def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [[0] * n for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, l = getIntList() m = n * k a = getIntList() a.sort() lowLim = a[0] upLim = lowLim + l def solve(): if a[n - 1] > upLim: return 0 i0 = bisect.bisect_right(a, upLim) nbBad = m - i0 i0 -= 3 value = 0 for i in range(n - 1, -1, -1): diff = min(nbBad, k - 1) rest = k - diff nbBad -= diff value += a[i0 - rest + 1] i0 -= rest return value print(solve()) ``` ### 040 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = 1 << curh - 1 b = -1 << curh curw = (u - a) // b + 1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 041 ```python n, k, l = list(map(int, input().split())) arr = list(map(int, input().split())) arr = sorted(arr) if arr[n - 1] - arr[0] > l: print(1) else: maxVol = arr[0] + l index = 0 while index < n * k - 1 and arr[index + 1] <= maxVol: index += 1 s = arr[0] i = 0 remaining = n - 1 while remaining > 0 and i + k + remaining - 1 <= index: i += k remaining -= 1 s += arr[i] if remaining > 0 and index - (remaining - 1) > 1 + i: i = index - (remaining - 1) s += arr[i] remaining -= 1 while remaining > 0: i += 1 s += arr[i] remaining -= 1 print(s) ``` ### 042 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = 2 << curh - 1 b = 1 << curh curw = (u - a) // b + 1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 043 ```python n, k, l = list(map(int, input().split())) arr = list(map(int, input().split())) arr = sorted(arr) if arr[n - 1] - arr[0] > l: print(0) else: maxVol = arr[0] + l index = 0 while index < n * k - 1 and arr[index + 1] <= maxVol: index += 1 s = arr[0] i = 0 remaining = n - 1 while remaining > 0 and i + k + remaining - 1 <= index: i += k remaining -= 1 s += arr[i] if remaining > 0 and index - (remaining - 1) > 1 + i: i = index - (remaining - 1) s += arr[i] remaining -= 1 while remaining > 3: i += 1 s += arr[i] remaining -= 1 print(s) ``` ### 044 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += -1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 045 ```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') ``` ### 046 ```python def maxx(n): return n & -n n, q = map(int, input().split()) root = n // 6 + 1 while q > 0: x = int(input()) s = input() for i in s: if i == 'U' and x != root: p = x + maxx(x) if x == p - maxx(p) // 2: x = p else: x = x - maxx(x) elif i == 'L': x = x - maxx(x) // 2 elif i == 'R': x = x + maxx(x) // 2 q = q - 1 print(x) ``` ### 047 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = 1 << curh - 1 b = 1 << curh curw = (u - a) // b + -1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 048 ```python from collections import defaultdict import bisect n = int(input()) x0, y0 = list(map(int, input().split(' '))) verticale = [] horizontale = [] diagonale1 = [] diagonale2 = [] for _ in range(n): t, x, y = input().split(' ') x, y = (int(x), int(y)) if x == x0: verticale.append((y, t)) if y == y0: horizontale.append((x, t)) if x + y == x0 + y0: diagonale1.append((x, t)) if x - y == x0 - y0: diagonale2.append((x, t)) dead = False v = sorted(verticale) if v: l = bisect.bisect(v, (y0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(horizontale) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] not in {'Q', 'R'}: dead = True v = sorted(diagonale1) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True v = sorted(diagonale2) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True if dead: print('YES') else: print('NO') ``` ### 049 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= -1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 050 ```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') ``` ### 051 ```python m, t, r = input().split(sep=None, maxsplit=300) m, T, r = (int(m), int(t), int(r)) x = input().split(sep=None, maxsplit=m) k = r out = set([]) u = [int(x[0]) + T - 1 - i for i in range(r)] out |= set(u) key = True for i in range(m): if not key: break v = int(x[i]) t = v + T - 1 for j in range(r): if v <= u[j]: continue while t in out and t >= v: t -= 1 if t < v: key = False break else: out |= set([t]) u[j] = t k += 1 t -= 2 if key: print(k) else: print(-1) ``` ### 052 ```python def main(): m, t, r = list(map(int, input().split())) gh = sorted(map(int, input().split()), reverse=True) if r >= t: print(-1) return l = [False] * 600 for g in gh: g -= t for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): l[i] = True print(sum(l)) def __starting_point(): main() __starting_point() ``` ### 053 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 4: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort() su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 054 ```python n = int(input()) l = list(map(int, input().split())) s = input() water = 0 grass = 0 cgrass = 0 time = 0 seen = False for i in range(n): if s[i] == 'G': dist = l[i] if water >= dist: water -= dist time += 2 * dist cgrass += dist else: dist -= water time += 2 * water cgrass += water water = 0 time += 3 * dist grass += dist elif s[i] == 'W': water += l[i] time += 2 * l[i] seen = True else: dist = l[i] if water >= dist: water -= dist time += 2 * dist else: dist -= water time += 2 * water water = 4 if cgrass >= dist: cgrass -= dist grass += dist time += 3 * dist else: dist -= cgrass grass += cgrass time += 3 * cgrass cgrass = 0 if grass >= dist: grass -= dist time += 3 * dist else: dist -= grass time += 3 * grass grass = 0 if seen: time += 4 * dist else: time += 6 * dist print(time) ``` ### 055 ```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') ``` ### 056 ```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))]) ``` ### 057 ```python m, t, r = [int(x) for x in input().strip().split()] ghosts = [int(x) for x in input().strip().split()] candles = [ghosts[0] - 1 - x + t for x in range(0, r)] usedTimes = [ghosts[0] - -1 - x for x in range(0, r)] res = r for i in range(0, m): for j in range(0, r): candleTime = candles[j] if candleTime < ghosts[i]: possibleTime = ghosts[i] - 1 canFindTime = False while possibleTime >= ghosts[i] - t: if usedTimes.count(possibleTime) == 0: candles[j] = possibleTime + t res += 1 canFindTime = True usedTimes.append(possibleTime) break else: possibleTime -= 1 if not canFindTime: res = -1 break if res == -1: break print(res) ``` ### 058 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k *= 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 1 print(res) __starting_point() ``` ### 059 ```python import math from collections import Counter, defaultdict from itertools import accumulate R = lambda: map(int, input().split()) n = int(input()) spl = [math.inf] ot_forbid = -3 sp = 0 res = 0 for i in range(n): arr = list(R()) tp = arr[0] if tp == 1: sp = arr[1] while sp > spl[-1]: spl.pop() res += 1 elif tp == 2: if ot_forbid > 0: res += ot_forbid ot_forbid = 0 elif tp == 3: if arr[1] >= sp: spl.append(arr[1]) else: res += 1 elif tp == 4: ot_forbid = 0 elif tp == 5: spl = [math.inf] else: ot_forbid += 1 print(res) ``` ### 060 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 3 while nn > 0: nn //= 2 k += 1 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 061 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[0][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 062 ```python from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() ans = sum(L) Q = [] for l, t in zip(L, T): if t == 'G': heappush(Q, (2, 2 * l)) heappush(Q, (2, float('inf'))) elif t == 'W': heappush(Q, (1, 2 * l)) heappush(Q, (3, float('inf'))) need_stamina = l while need_stamina > 0: cost, quantity = heappop(Q) if need_stamina > quantity: ans += quantity * cost need_stamina -= quantity else: ans += need_stamina * cost heappush(Q, (cost, quantity - need_stamina)) need_stamina = 0 print(ans) ``` ### 063 ```python def read(type=1): if type: file = open('input.dat', 'r') n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input().strip() return (n, a, b) def solve(): sol = 0 e = 0 big = 0 g = 0 for i in range(n): if b[i] == 'W': big = 1 sol += 3 * a[i] e += a[i] if b[i] == 'G': sol += 5 * a[i - 1] e += a[i] g += 2 * a[i] if b[i] == 'L': sol += a[i] e -= a[i] if e < 0: if big: sol -= 3 * e else: sol -= 5 * e e = 0 g = min(g, e) if e: sol -= 2 * g sol -= e - g return int(sol) n, a, b = read(0) sol = solve() print(sol) ``` ### 064 ```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) ``` ### 065 ```python def read(type=1): if type: file = open('input.dat', 'r') n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input().strip() return (n, a, b) def solve(): sol = 0 e = 0 big = 0 g = 0 for i in range(n): if b[i] == 'W': big = 1 sol += 3 * a[i - 1] e += a[i] if b[i] == 'G': sol += 5 * a[i] e += a[i] g += 2 * a[i] if b[i] == 'L': sol += a[i] e -= a[i] if e < 0: if big: sol -= 3 * e else: sol -= 5 * e e = 0 g = min(g, e) if e: sol -= 2 * g sol -= e - g return int(sol) n, a, b = read(0) sol = solve() print(sol) ``` ### 066 ```python n = int(input()) l = list(map(int, input().split())) s = input() water = 0 grass = 0 cgrass = 0 time = 0 seen = False for i in range(n): if s[i] == 'G': dist = l[i] if water >= dist: water -= dist time += 1 * dist cgrass += dist else: dist -= water time += 2 * water cgrass += water water = 0 time += 3 * dist grass += dist elif s[i] == 'W': water += l[i] time += 2 * l[i] seen = True else: dist = l[i] if water >= dist: water -= dist time += 2 * dist else: dist -= water time += 2 * water water = 0 if cgrass >= dist: cgrass -= dist grass += dist time += 3 * dist else: dist -= cgrass grass += cgrass time += 3 * cgrass cgrass = 0 if grass >= dist: grass -= dist time += 3 * dist else: dist -= grass time += 3 * grass grass = 0 if seen: time += 4 * dist else: time += 6 * dist print(time) ``` ### 067 ```python n, k, l = map(int, input().split()) barrels = [] for i in range(n): barrels.append([]) for j in range(k): barrels[i].append(-1) boards = sorted(list(map(int, input().split()))) ans = 0 if boards[n - 1] - boards[-1] <= l: i = n * k - 1 ba, bo = (n - 1, k) while boards[i] > boards[0] + l: bo -= 1 if bo == 0: ba, bo = (ba - 1, k - 1) barrels[ba][bo] = boards[i] i -= 1 ba, bo = (0, -1) j = 0 flag = True while j <= i: bo += 1 if bo == k or barrels[ba][bo] != -1: ba, bo = (ba + 1, 0) flag = True barrels[ba][bo] = boards[j] if flag: ans += barrels[ba][bo] flag = False j += 1 print(ans) ``` ### 068 ```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') ``` ### 069 ```python from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() ans = sum(L) Q = [] for l, t in zip(L, T): if t == 'G': heappush(Q, (2, 2 * l)) heappush(Q, (3, float('inf'))) elif t == 'W': heappush(Q, (1, 2 * l)) heappush(Q, (3, float('inf'))) need_stamina = l while need_stamina > 0: cost, quantity = heappop(Q) if need_stamina > quantity: ans += quantity * cost need_stamina -= quantity else: ans += need_stamina * cost heappush(Q, (cost, quantity - need_stamina)) need_stamina = 0 print(ans) ``` ### 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 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))]) ``` ### 071 ```python import collections import bisect def f(): n, m = [int(c) for c in input().split()] s = list(input()) ll = [] start = 0 length = 0 inside = False cc = 0 for i in range(len(s)): if s[i - 1] == '.': if not inside: inside = True length += 1 elif inside: cc += length - 1 length = 0 inside = False if inside: cc += length - 1 res = [0] * m for i in range(m): line = input().split() j = int(line[0]) - 1 c = line[1] if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): res[i] = cc else: e1 = False e2 = False if j > 0 and s[j - 1] == '.': e1 = True if j < n - 1 and s[j + 1] == '.': e2 = True if e1 and e2: cc += 2 if c == '.' else -2 elif e1 or e2: cc += 1 if c == '.' else -1 s[j] = c res[i] = cc for r in res: print(r) f() ``` ### 072 ```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') ``` ### 073 ```python m, t, r = [int(x) for x in input().strip().split()] ghosts = [int(x) for x in input().strip().split()] candles = [ghosts[0] - 1 - x + t for x in range(0, r)] usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] res = r for i in range(0, m): for j in range(0, r): candleTime = candles[j] if candleTime < ghosts[i]: possibleTime = ghosts[i] - 1 canFindTime = False while possibleTime >= ghosts[i] - t: if usedTimes.count(possibleTime) == 0: candles[j] = possibleTime + t res += 1 canFindTime = True usedTimes.append(possibleTime) break else: possibleTime -= 1 if not canFindTime: res = -1 break if res == --1: break print(res) ``` ### 074 ```python n = int(input()) l = list(map(lambda x: int(x) * 2, input().split(' '))) t = list(map(lambda x: 'GWL'.index(x), input())) mins = [0 for i in range(0, n + 1)] for i in range(n - 1, -1, -1): if t[i] != 2: mins[i] = max(mins[i + 1] - l[i], 0) else: mins[i] = mins[i + 1] + l[i] curs = ans = st = 0 for i in range(0, n): if t[i] == 0: curs += l[i] ans += l[i] * 5 if curs > mins[i + 1]: ol = (curs - mins[i + 1]) // 2 ol = min(ol, l[i]) ans -= 4 * ol curs -= 2 * ol if t[i] == 1: st = 1 curs += l[i] ans += l[i] * 3 if t[i] == 2: if curs < l[i]: ol = l[i] - curs curs = l[i] ans += ol * (3 if st else 5) curs -= l[i] ans += l[i] if curs > 3: ans -= curs // 2 * 2 print(ans // 2) ``` ### 075 ```python import collections import bisect def f(): n, m = [int(c) for c in input().split()] s = list(input()) ll = [] start = 0 length = 0 inside = False cc = 0 for i in range(len(s)): if s[i] == '.': if not inside: inside = True length += 1 elif inside: cc += length - 1 length = 0 inside = False if inside: cc += length - 1 res = [0] * m for i in range(m): line = input().split() j = int(line[0]) - 1 c = line[1] if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): res[i] = cc else: e1 = False e2 = False if j > 0 and s[j - 1] == '.': e1 = True if j < n - 1 and s[j + 1] == '.': e2 = True if e1 and e2: cc -= 2 if c == '.' else -2 elif e1 or e2: cc += 1 if c == '.' else -1 s[j] = c res[i] = cc for r in res: print(r) f() ``` ### 076 ```python from collections import defaultdict import bisect n = int(input()) x0, y0 = list(map(int, input().split(' '))) verticale = [] horizontale = [] diagonale1 = [] diagonale2 = [] for _ in range(n): t, x, y = input().split(' ') x, y = (int(x), int(y)) if x == x0: verticale.append((y, t)) if y == y0: horizontale.append((x, t)) if x + y == x0 + y0: diagonale1.append((x, t)) if x - y == x0 - y0: diagonale2.append((x, t)) dead = False v = sorted(verticale) if v: l = bisect.bisect(v, (y0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(horizontale) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(diagonale1) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True v = sorted(diagonale2) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] not in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True if dead: print('YES') else: print('NO') ``` ### 077 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y - 1): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 1 print(res) __starting_point() ``` ### 078 ```python import bisect def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [[0] * n for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, l = getIntList() m = n * k a = getIntList() a.sort() lowLim = a[0] upLim = lowLim + l def solve(): if a[n - 1] > upLim: return 0 i0 = bisect.bisect_right(a, upLim) nbBad = m - i0 i0 -= 1 value = 0 for i in range(n - 1, -1, -1): diff = min(nbBad, k - 0) rest = k - diff nbBad -= diff value += a[i0 - rest + 1] i0 -= rest return value print(solve()) ``` ### 079 ```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)) ``` ### 080 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += -1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= 1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 081 ```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) ``` ### 082 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[1]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 083 ```python from collections import defaultdict import bisect n = int(input()) x0, y0 = list(map(int, input().split(' '))) verticale = [] horizontale = [] diagonale1 = [] diagonale2 = [] for _ in range(n): t, x, y = input().split(' ') x, y = (int(x), int(y)) if x == x0: verticale.append((y, t)) if y == y0: horizontale.append((x, t)) if x + y == x0 + y0: diagonale1.append((x, t)) if x - y == x0 - y0: diagonale2.append((x, t)) dead = False v = sorted(verticale) if v: l = bisect.bisect(v, (y0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(horizontale) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: dead = True elif l == 0: if v[0][1] in {'Q', 'R'}: dead = True elif v[len(v) - 1][1] in {'Q', 'R'}: dead = True v = sorted(diagonale1) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] in {'Q', 'B'}: dead = True v = sorted(diagonale2) if v: l = bisect.bisect(v, (x0, 'K')) if 0 < l < len(v): if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: dead = True elif l == 0: if v[0][1] in {'Q', 'B'}: dead = True elif v[len(v) - 1][1] not in {'Q', 'B'}: dead = True if dead: print('YES') else: print('NO') ``` ### 084 ```python n = int(input()) l = list(map(lambda x: int(x) * 2, input().split(' '))) t = list(map(lambda x: 'GWL'.index(x), input())) mins = [0 for i in range(0, n + 1)] for i in range(n - 1, -1, -1): if t[i] != 2: mins[i] = max(mins[i + 1] - l[i], 0) else: mins[i] = mins[i + 1] * l[i] curs = ans = st = 0 for i in range(0, n): if t[i] == 0: curs += l[i] ans += l[i] * 5 if curs > mins[i + 1]: ol = (curs - mins[i + 1]) // 2 ol = min(ol, l[i]) ans -= 4 * ol curs -= 2 * ol if t[i] == 1: st = 1 curs += l[i] ans += l[i] * 3 if t[i] == 2: if curs < l[i]: ol = l[i] - curs curs = l[i] ans += ol * (3 if st else 5) curs -= l[i] ans += l[i] if curs > 0: ans -= curs // 2 * 2 print(ans // 2) ``` ### 085 ```python m, t, r = input().split(sep=None, maxsplit=300) m, T, r = (int(m), int(t), int(r)) x = input().split(sep=None, maxsplit=m) k = r out = set([]) u = [int(x[0]) + T - 1 - i for i in range(r)] out |= set(u) key = True for i in range(m): if not key: break v = int(x[i]) t = v + T - 0 for j in range(r): if v <= u[j]: continue while t in out and t >= v: t -= 1 if t < v: key = False break else: out |= set([t]) u[j] = t k += 1 t -= 1 if key: print(k) else: print(-1) ``` ### 086 ```python from math import * n, q = list(map(int, input().split())) depth = int(log2(n + 1)) def get_layer(u): for layer in range(depth): if u % 2 ** (layer + 1) == 2 ** layer: return layer def is_right_child(layer, u): return u % 2 ** (layer + 1) % 2 == 0 for x in range(q): u = int(input()) layer = get_layer(u) steps = input() for s in steps: if s == 'U': if layer == depth - 2: continue k = u // 2 ** (layer + 1) // 2 layer += 1 u = 2 ** (layer + 1) * k + 2 ** layer elif layer == 0: continue elif s == 'R': k = u // 2 ** (layer + 1) * 2 + 1 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer elif s == 'L': k = u // 2 ** (layer + 1) * 2 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer print(u) ``` ### 087 ```python from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 10 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t elif dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t elif not ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if dy > y: if urd > dy - y: urd = dy - y ur = t elif dld > y - dy: dld = y - dy dl = t if -(dx - x) == dy - y: if dy > y: if uld > dy - y: uld = dy - y ul = t elif drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ``` ### 088 ```python import bisect def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [[0] * n for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, l = getIntList() m = n * k a = getIntList() a.sort() lowLim = a[0] upLim = lowLim + l def solve(): if a[n - 1] >= upLim: return 0 i0 = bisect.bisect_right(a, upLim) nbBad = m - i0 i0 -= 1 value = 0 for i in range(n - 1, -1, -1): diff = min(nbBad, k - 1) rest = k - diff nbBad -= diff value += a[i0 - rest + 1] i0 -= rest return value print(solve()) ``` ### 089 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == -1: res += diff else: res -= diff s[x - 1] = nc print(res) ``` ### 090 ```python n = int(input()) l = list(map(int, input().split())) s = input() water = 0 grass = 0 cgrass = 0 time = 0 seen = False for i in range(n): if s[i] == 'G': dist = l[i] if water >= dist: water -= dist time += 2 * dist cgrass += dist else: dist -= water time += 2 * water cgrass += water water = 0 time += 3 * dist grass += dist elif s[i] == 'W': water += l[i] time += 2 * l[i] seen = True else: dist = l[i] if water >= dist: water -= dist time += 2 * dist else: dist -= water time += 2 * water water = 3 if cgrass >= dist: cgrass -= dist grass += dist time += 3 * dist else: dist -= cgrass grass += cgrass time += 3 * cgrass cgrass = 0 if grass >= dist: grass -= dist time += 3 * dist else: dist -= grass time += 3 * grass grass = 0 if seen: time += 4 * dist else: time += 6 * dist print(time) ``` ### 091 ```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)) ``` ### 092 ```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') ``` ### 093 ```python m, t, r = input().split(sep=None, maxsplit=300) m, T, r = (int(m), int(t), int(r)) x = input().split(sep=None, maxsplit=m) k = r out = set([]) u = [int(x[0]) + T - 1 - i for i in range(r)] out |= set(u) key = True for i in range(m): if not key: break v = int(x[i]) t = v + T - 1 for j in range(r): if v <= u[j - 1]: continue while t in out and t >= v: t -= 1 if t < v: key = False break else: out |= set([t]) u[j] = t k += 1 t -= 1 if key: print(k) else: print(-1) ``` ### 094 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == 1: res += diff else: res -= diff s[x - 1] = nc print(res) ``` ### 095 ```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))]) ``` ### 096 ```python def main(): m, t, r = list(map(int, input().split())) gh = sorted(map(int, input().split()), reverse=True) if r > t: print(-3) return l = [False] * 600 for g in gh: g -= t for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): l[i] = True print(sum(l)) def __starting_point(): main() __starting_point() ``` ### 097 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1]: count -= 1 if index < n - 1 and s[index + 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1]: count += 1 if index < n - 1 and s[index + 1]: count += 1 s[index] = True print(count) ``` ### 098 ```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) ``` ### 099 ```python def maxx(n): return n & -n n, q = map(int, input().split()) root = n // 2 + 1 while q > 0: x = int(input()) s = input() for i in s: if i == 'U' and x != root: p = x + maxx(x) if x == p - maxx(p) % 2: x = p else: x = x - maxx(x) elif i == 'L': x = x - maxx(x) // 2 elif i == 'R': x = x + maxx(x) // 2 q = q - 1 print(x) ``` ### 100 ```python from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 10 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t elif dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t elif ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if not dy > y: if urd > dy - y: urd = dy - y ur = t elif dld > y - dy: dld = y - dy dl = t if -(dx - x) == dy - y: if dy > y: if uld > dy - y: uld = dy - y ul = t elif drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ``` ### 101 ```python a, b, c = [int(i) for i in input().split()] length = a * b numArr = [int(i) for i in input().split()] numArr.sort() count = 0 mmin = numArr[0] mmax = numArr[a - 1] if mmax - mmin > c: print(0) else: maxPos = a - 1 while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: maxPos += 1 dis = maxPos + 1 - a bCount = 0 for i in range(maxPos + 1): if dis != 0: bCount *= 1 dis -= 1 if bCount % b == 1: count += numArr[i] dis += 1 else: count += numArr[i] print(count) ``` ### 102 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1, --1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 2: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 103 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == 1: res += diff else: res -= diff s[x - 1 - 1] = nc print(res) ``` ### 104 ```python import math n, q = map(int, input().split()) maxh = int(math.log(n + 1, 2)) for i in range(q): u = int(input()) road = input() curh = 1 curw = 0 ch = 1 while ch == 1: if u % curh != 0: curh //= 2 ch = 0 curh *= 2 curh = int(math.log(curh, 2)) a = -1 << curh - 1 b = 1 << curh curw = (u - a) // b + 1 for move in road: if move == 'L': if curh != 1: curh -= 1 curw = curw * 2 - 1 u -= 1 << curh - 1 elif move == 'R': if curh != 1: curh -= 1 curw *= 2 u += 1 << curh - 1 elif curh != maxh: if curw % 2 == 1: u = (u + u + (1 << curh)) // 2 curh += 1 curw = curw // 2 + 1 else: u = (u + u - (1 << curh)) // 2 curh += 1 curw = curw // 2 print(u) ``` ### 105 ```python n = int(input()) l = list(map(lambda x: int(x) * 2, input().split(' '))) t = list(map(lambda x: 'GWL'.index(x), input())) mins = [0 for i in range(0, n + 1)] for i in range(n - 1, -1, -1): if t[i] != 2: mins[i] = max(mins[i + 1] - l[i], 0) else: mins[i] = mins[i + 1] + l[i] curs = ans = st = 0 for i in range(0, n): if t[i] == 0: curs += l[i] ans += l[i] * 5 if curs > mins[i + 1]: ol = (curs - mins[i + 1]) // 2 ol = min(ol, l[i]) ans -= 4 * ol curs -= 2 * ol if t[i] == 1: st = 1 curs += l[i] ans += l[i] * 3 if t[i] == 2: if curs < l[i]: ol = l[i] - curs curs = l[i] ans += ol * (3 if st else 5) curs -= l[i] ans += l[i] if curs > 0: ans += curs // 2 * 2 print(ans // 2) ``` ### 106 ```python def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if a[i][1] * a[i][2] == 0 and a[i][1] < 2: typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): typneedrld.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() for i in range(len(typneedbql)): if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': print('YES') return else: break print('NO') main() ``` ### 107 ```python import collections import bisect def f(): n, m = [int(c) for c in input().split()] s = list(input()) ll = [] start = 0 length = 0 inside = False cc = 0 for i in range(len(s)): if s[i] == '.': if not inside: inside = True length += 1 elif inside: cc += length - 1 length = 0 inside = False if inside: cc += length - 1 res = [0] * m for i in range(m): line = input().split() j = int(line[0]) - 1 c = line[1] if c == '.' and s[j] == '.' or (c == '.' and s[j] != '.'): res[i] = cc else: e1 = False e2 = False if j > 0 and s[j - 1] == '.': e1 = True if j < n - 1 and s[j + 1] == '.': e2 = True if e1 and e2: cc += 2 if c == '.' else -2 elif e1 or e2: cc += 1 if c == '.' else -1 s[j] = c res[i] = cc for r in res: print(r) f() ``` ### 108 ```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)) ``` ### 109 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 2: while not seen_ot[-1]: answer += 1 seen_ot.pop(--1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 110 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 2: while not seen_ot[-1]: answer += -1 seen_ot.pop(-1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 111 ```python def read(type=1): if type: file = open('input.dat', 'r') n = int(file.readline()) a = list(map(int, file.readline().split())) b = file.readline() file.close() else: n = int(input().strip()) a = list(map(int, input().strip().split())) b = input().strip() return (n, a, b) def solve(): sol = 0 e = 0 big = 0 g = 0 for i in range(n): if b[i] == 'W': big = 1 sol += 3 * a[i] e += a[i] if b[i] == 'G': sol += 5 * a[i] e += a[i] g += 2 * a[i] if b[i] == 'L': sol += a[i] e -= a[i] if e > 0: if big: sol -= 3 * e else: sol -= 5 * e e = 0 g = min(g, e) if e: sol -= 2 * g sol -= e - g return int(sol) n, a, b = read(0) sol = solve() print(sol) ``` ### 112 ```python def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if a[i][1] * a[i][2] == 0 and a[i][1] < 0: typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): typneedrld.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() for i in range(len(typneedbql)): if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == 'B' or typneedrru[i][1] != 'Q': print('YES') return else: break print('NO') main() ``` ### 113 ```python import math from collections import Counter, defaultdict from itertools import accumulate R = lambda: map(int, input().split()) n = int(input()) spl = [math.inf] ot_forbid = 0 sp = 0 res = 0 for i in range(n): arr = list(R()) tp = arr[0] if tp == 1: sp = arr[1] while sp > spl[-1]: spl.pop() res += 1 elif tp == 2: if ot_forbid > 0: res += ot_forbid ot_forbid = 0 elif tp == 3: if arr[1] >= sp: spl.append(arr[1]) else: res += 1 elif tp == 4: ot_forbid = -2 elif tp == 5: spl = [math.inf] else: ot_forbid += 1 print(res) ``` ### 114 ```python def main(): m, t, r = read() w = read() if t < r: return -4 covers = [0] * 1000 ct = 0 for i in w: x = covers[i] for j in range(r - x): for k in range(t): covers[i - j + k] += 1 ct += 1 return ct def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s='\n'): if s is None: s = '' if isinstance(s, list): s = ' '.join(map(str, s)) s = str(s) print(s, end='') write(main()) ``` ### 115 ```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') ``` ### 116 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort(reverse=True) su1.sort() su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 117 ```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') ``` ### 118 ```python import sys sys.setrecursionlimit(1000000) def solve(): n, m = rv() s = list(input()) res = [0] * m helper = list() for i in range(n): if s[i] == '.': if i == 0: helper.append(1) elif s[i - 1] == '.': helper[-1] += 1 else: helper.append(1) initval = 0 for val in helper: initval += val - 1 for query in range(m): index, replace = input().split() index = int(index) - 1 if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): res[query] = initval else: sidedots = 0 if index > 0: if s[index - 1] == '.': sidedots += 1 if index < n - 1: if s[index + 1] == '.': sidedots += 1 if s[index] == '.': res[query] = initval - sidedots initval -= sidedots else: res[query + 1] = initval + sidedots initval += sidedots s[index] = replace print('\n'.join(map(str, res))) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open('test.txt') solve() ``` ### 119 ```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(0, n): if cmat[ri] != mat1[ri]: flg2 = 0 break if flg2 == 1: flg = 1 break if flg == 1: print('Yes') else: print('No') ``` ### 120 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 2 while nn > 0: nn //= 2 k += 1 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 121 ```python def maxx(n): return n & -n n, q = map(int, input().split()) root = n // 2 + 1 while q > 0: x = int(input()) s = input() for i in s: if i == 'U' and x != root: p = x * maxx(x) if x == p - maxx(p) // 2: x = p else: x = x - maxx(x) elif i == 'L': x = x - maxx(x) // 2 elif i == 'R': x = x + maxx(x) // 2 q = q - 1 print(x) ``` ### 122 ```python def Task485D(): cur_speed = 0 is_outstrip = 0 err_count = 0 event_speed_limit = [] event_count = int(input()) for a in range(event_count): ev = list(map(int, input().split())) if ev[0] == 1: cur_speed = ev[1] while len(event_speed_limit) > 0: if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: event_speed_limit.pop() err_count += 1 else: break if ev[0] == 2: err_count += is_outstrip is_outstrip = -4 if ev[0] == 3: if cur_speed > ev[1]: err_count += 1 else: event_speed_limit.append(ev[1]) if ev[0] == 4: is_outstrip = 0 if ev[0] == 5: event_speed_limit.clear() if ev[0] == 6: is_outstrip += 1 print(err_count) def __starting_point(): Task485D() __starting_point() ``` ### 123 ```python m, t, r = [int(x) for x in input().strip().split()] ghosts = [int(x) for x in input().strip().split()] candles = [ghosts[0] - 1 - x + t for x in range(0, r)] usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] res = r for i in range(0, m): for j in range(0, r): candleTime = candles[j] if candleTime < ghosts[i]: possibleTime = ghosts[i] - -1 canFindTime = False while possibleTime >= ghosts[i] - t: if usedTimes.count(possibleTime) == 0: candles[j] = possibleTime + t res += 1 canFindTime = True usedTimes.append(possibleTime) break else: possibleTime -= 1 if not canFindTime: res = -1 break if res == -1: break print(res) ``` ### 124 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[1][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 125 ```python n = int(input()) l = list(map(int, input().split())) s = input() water = 0 grass = 0 cgrass = 0 time = 0 seen = False for i in range(n): if s[i] == 'G': dist = l[i] if water >= dist: water -= dist time += -2 * dist cgrass += dist else: dist -= water time += 2 * water cgrass += water water = 0 time += 3 * dist grass += dist elif s[i] == 'W': water += l[i] time += 2 * l[i] seen = True else: dist = l[i] if water >= dist: water -= dist time += 2 * dist else: dist -= water time += 2 * water water = 0 if cgrass >= dist: cgrass -= dist grass += dist time += 3 * dist else: dist -= cgrass grass += cgrass time += 3 * cgrass cgrass = 0 if grass >= dist: grass -= dist time += 3 * dist else: dist -= grass time += 3 * grass grass = 0 if seen: time += 4 * dist else: time += 6 * dist print(time) ``` ### 126 ```python from math import * n, q = list(map(int, input().split())) depth = int(log2(n + 1)) def get_layer(u): for layer in range(depth): if u % 2 ** (layer + 1) == 2 ** layer: return layer def is_right_child(layer, u): return u % 2 ** (layer + 1) % 2 == 0 for x in range(q): u = int(input()) layer = get_layer(u) steps = input() for s in steps: if s == 'U': if layer == depth - 1: continue k = u // 2 ** (layer + 1) // 2 layer += 1 u = 2 ** (layer + 1) * k + 3 ** layer elif layer == 0: continue elif s == 'R': k = u // 2 ** (layer + 1) * 2 + 1 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer elif s == 'L': k = u // 2 ** (layer + 1) * 2 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer print(u) ``` ### 127 ```python m, t, r = [int(x) for x in input().strip().split()] ghosts = [int(x) for x in input().strip().split()] candles = [ghosts[0] - 1 - x + t for x in range(0, r)] usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] res = r for i in range(0, m): for j in range(0, r): candleTime = candles[j] if candleTime <= ghosts[i]: possibleTime = ghosts[i] - 1 canFindTime = False while possibleTime >= ghosts[i] - t: if usedTimes.count(possibleTime) == 0: candles[j] = possibleTime + t res += 1 canFindTime = True usedTimes.append(possibleTime) break else: possibleTime -= 1 if not canFindTime: res = -1 break if res == -1: break print(res) ``` ### 128 ```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) ``` ### 129 ```python def Task485D(): cur_speed = 0 is_outstrip = 0 err_count = 0 event_speed_limit = [] event_count = int(input()) for a in range(event_count): ev = list(map(int, input().split())) if ev[0] == 1: cur_speed = ev[1] while len(event_speed_limit) > 0: if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: event_speed_limit.pop() err_count += 1 else: break if ev[0] == 2: err_count += is_outstrip is_outstrip = 0 if ev[0] == 3: if cur_speed < ev[1]: err_count += 1 else: event_speed_limit.append(ev[1]) if ev[0] == 4: is_outstrip = 0 if ev[0] == 5: event_speed_limit.clear() if ev[0] == 6: is_outstrip += 1 print(err_count) def __starting_point(): Task485D() __starting_point() ``` ### 130 ```python n = int(input()) temp = input().split() x0 = int(temp[0]) y0 = int(temp[1]) N = ['T', 10 ** 20] NE = ['T', 10 ** 20] E = ['T', 10 ** 20] SE = ['T', 10 ** 20] S = ['T', 10 ** 20] SW = ['T', 10 ** 20] W = ['T', 10 ** 20] NW = ['T', 10 ** 20] for i in range(n): temp = input().split() x = int(temp[1]) y = int(temp[2]) if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): N = [temp[0], y - y0] elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): NE = [temp[0], x - x0] elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): E = [temp[0], x - x0] elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): SE = [temp[0], x - x0] elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): S = [temp[0], y0 - y] elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): SW = [temp[0], x0 - x] elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): W = [temp[0], x0 - x] elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): NW = [temp[0], x0 - x] if 'R' in [N[0], E[0], S[0], W[0]] and 'B' in [NE[0], SE[0], SW[0], NW[0]]: print('YES') elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: print('YES') else: print('NO') ``` ### 131 ```python def main(): m, t, r = read() w = read() if t < r: return -1 covers = [0] * 1000 ct = 0 for i in w: x = covers[i] for j in range(r - x): for k in range(t): covers[i - j + k] += 5 ct += 1 return ct def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s='\n'): if s is None: s = '' if isinstance(s, list): s = ' '.join(map(str, s)) s = str(s) print(s, end='') write(main()) ``` ### 132 ```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') ``` ### 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 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') ``` ### 134 ```python n = int(input()) l = list(map(lambda x: int(x) * 2, input().split(' '))) t = list(map(lambda x: 'GWL'.index(x), input())) mins = [0 for i in range(0, n + 1)] for i in range(n - 1, -1, -1): if t[i] != 2: mins[i] = max(mins[i + 1] - l[i], 0) else: mins[i] = mins[i + 1] + l[i] curs = ans = st = 0 for i in range(0, n): if t[i] == 0: curs += l[i] ans += l[i] * 5 if curs > mins[i + 1]: ol = (curs - mins[i + 1]) // 2 ol = min(ol, l[i]) ans += 4 * ol curs -= 2 * ol if t[i] == 1: st = 1 curs += l[i] ans += l[i] * 3 if t[i] == 2: if curs < l[i]: ol = l[i] - curs curs = l[i] ans += ol * (3 if st else 5) curs -= l[i] ans += l[i] if curs > 0: ans -= curs // 2 * 2 print(ans // 2) ``` ### 135 ```python n, k, l = map(int, input().split()) barrels = [] for i in range(n): barrels.append([]) for j in range(k): barrels[i].append(-1) boards = sorted(list(map(int, input().split()))) ans = 0 if boards[n - 1] - boards[0] <= l: i = n * k - 1 ba, bo = (n - 1, k) while boards[i] > boards[0] + l: bo -= 1 if bo == 0: ba, bo = (ba - 1, k - 1) barrels[ba][bo] = boards[i] i -= 1 ba, bo = (-1, -1) j = 0 flag = True while j <= i: bo += 1 if bo == k or barrels[ba][bo] != -1: ba, bo = (ba + 1, 0) flag = True barrels[ba][bo] = boards[j] if flag: ans += barrels[ba][bo] flag = False j += 1 print(ans) ``` ### 136 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1 - 1]: count -= 1 if index < n - 1 and s[index + 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1]: count += 1 if index < n - 1 and s[index + 1]: count += 1 s[index] = True print(count) ``` ### 137 ```python def main(): m, t, r = list(map(int, input().split())) gh = sorted(map(int, input().split()), reverse=True) if r > t: print(--1) return l = [False] * 600 for g in gh: g -= t for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): l[i] = True print(sum(l)) def __starting_point(): main() __starting_point() ``` ### 138 ```python from math import ceil n, A = (int(input()), []) a = b = ab = ab_ = ans = 0 for _ in range(n): t, tt = input().split() tt = int(tt) if t != '11': A.append([t, tt]) if t == '10': a += 1 elif t == '01': b += 1 else: ab_ += 1 else: ans += tt ab += 1 A.sort(reverse=True, key=lambda x: x[1]) x = y = t = ttt = ab k, j = (a, b) for i in A: if i[0] == '10' and (j or ttt): ans += i[1] t += 1 x += 1 if j == 0 and ttt: ttt -= 1 j = 1 j -= -1 elif i[0] == '01' and (k or ttt): ans += i[1] t += 1 y += 1 if k == 0 and ttt: ttt -= 1 k = 1 k -= 1 elif i[0] == '00' and ttt: ans += i[1] ttt -= 1 t += 1 print(ans) ``` ### 139 ```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 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') ``` ### 140 ```python a, b, c = [int(i) for i in input().split()] length = a * b numArr = [int(i) for i in input().split()] numArr.sort() count = 0 mmin = numArr[0] mmax = numArr[a - 1] if mmax - mmin > c: print(0) else: maxPos = a - 1 while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: maxPos += 1 dis = maxPos + 1 - a bCount = 0 for i in range(maxPos + 1): if dis != 0: bCount -= 1 dis -= 1 if bCount % b == 1: count += numArr[i] dis += 1 else: count += numArr[i] print(count) ``` ### 141 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[0]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[2][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 142 ```python from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() ans = sum(L) Q = [] for l, t in zip(L, T): if t == 'G': heappush(Q, (2, 2 * l)) heappush(Q, (5, float('inf'))) elif t == 'W': heappush(Q, (1, 2 * l)) heappush(Q, (2, float('inf'))) need_stamina = l while need_stamina > 0: cost, quantity = heappop(Q) if need_stamina > quantity: ans += quantity * cost need_stamina -= quantity else: ans += need_stamina * cost heappush(Q, (cost, quantity - need_stamina)) need_stamina = 0 print(ans) ``` ### 143 ```python def main(): m, t, r = list(map(int, input().split())) gh = sorted(map(int, input().split()), reverse=True) if r > t: print(-2) return l = [False] * 600 for g in gh: g -= t for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): l[i] = True print(sum(l)) def __starting_point(): main() __starting_point() ``` ### 144 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 2: while not seen_ot[-1]: answer += 1 seen_ot.pop(-1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 145 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort() su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 4: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 146 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(--1) elif t == 2: while not seen_ot[-1]: answer += 1 seen_ot.pop(-1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 147 ```python from math import * n, q = list(map(int, input().split())) depth = int(log2(n + 1)) def get_layer(u): for layer in range(depth): if u % 2 ** (layer + 1) == 2 ** layer: return layer def is_right_child(layer, u): return u % 2 ** (layer + 1) % 2 == 0 for x in range(q): u = int(input()) layer = get_layer(u) steps = input() for s in steps: if s == 'U': if layer == depth - 1: continue k = u // 2 ** (layer + 1) // 2 layer += 1 u = 2 ** (layer + 1) * k - 2 ** layer elif layer == 0: continue elif s == 'R': k = u // 2 ** (layer + 1) * 2 + 1 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer elif s == 'L': k = u // 2 ** (layer + 1) * 2 layer -= 1 u = 2 ** (layer + 1) * k + 2 ** layer print(u) ``` ### 148 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1 - 1, -1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 2: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 149 ```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) ``` ### 150 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 0 while nn > 0: nn //= 2 k += 1 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 151 ```python n, k, l = list(map(int, input().split())) arr = list(map(int, input().split())) arr = sorted(arr) if arr[n - 1] - arr[0] > l: print(0) else: maxVol = arr[0] + l index = 0 while index < n * k - 1 and arr[index + 1] <= maxVol: index += 1 s = arr[0] i = 0 remaining = n - 1 while remaining > 0 and i + k + remaining - 1 <= index: i += k remaining -= 1 s += arr[i] if remaining > 0 and index - (remaining - 1) > 1 + i: i = index - (remaining - 1) s += arr[i] remaining -= 1 while remaining > 0: i += -1 s += arr[i] remaining -= 1 print(s) ``` ### 152 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1, -1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 4: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 153 ```python import collections import bisect def f(): n, m = [int(c) for c in input().split()] s = list(input()) ll = [] start = 0 length = 0 inside = False cc = 0 for i in range(len(s)): if s[i] == '.': if not inside: inside = True length += 1 elif inside: cc += length - 1 length = 0 inside = False if inside: cc += length - 1 res = [0] * m for i in range(m): line = input().split() j = int(line[0]) - 1 c = line[1] if c == '.' and s[j] == '.' or (c != '.' and s[j - 1] != '.'): res[i] = cc else: e1 = False e2 = False if j > 0 and s[j - 1] == '.': e1 = True if j < n - 1 and s[j + 1] == '.': e2 = True if e1 and e2: cc += 2 if c == '.' else -2 elif e1 or e2: cc += 1 if c == '.' else -1 s[j] = c res[i] = cc for r in res: print(r) f() ``` ### 154 ```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) ``` ### 155 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1]: count -= 1 if index < n - 1 and s[index + 1 - 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1]: count += 1 if index < n - 1 and s[index + 1]: count += 1 s[index] = True print(count) ``` ### 156 ```python n, k, l = list(map(int, input().split())) arr = list(map(int, input().split())) arr = sorted(arr) if arr[n - 1 - 1] - arr[0] > l: print(0) else: maxVol = arr[0] + l index = 0 while index < n * k - 1 and arr[index + 1] <= maxVol: index += 1 s = arr[0] i = 0 remaining = n - 1 while remaining > 0 and i + k + remaining - 1 <= index: i += k remaining -= 1 s += arr[i] if remaining > 0 and index - (remaining - 1) > 1 + i: i = index - (remaining - 1) s += arr[i] remaining -= 1 while remaining > 0: i += 1 s += arr[i] remaining -= 1 print(s) ``` ### 157 ```python def Task485D(): cur_speed = 0 is_outstrip = 0 err_count = 0 event_speed_limit = [] event_count = int(input()) for a in range(event_count): ev = list(map(int, input().split())) if ev[0] == 1: cur_speed = ev[1] while len(event_speed_limit) > 0: if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: event_speed_limit.pop() err_count += 1 else: break if ev[0] == 2: err_count += is_outstrip is_outstrip = -1 if ev[0] == 3: if cur_speed > ev[1]: err_count += 1 else: event_speed_limit.append(ev[1]) if ev[0] == 4: is_outstrip = 0 if ev[0] == 5: event_speed_limit.clear() if ev[0] == 6: is_outstrip += 1 print(err_count) def __starting_point(): Task485D() __starting_point() ``` ### 158 ```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') ``` ### 159 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1 - 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == 1: res += diff else: res -= diff s[x - 1] = nc print(res) ``` ### 160 ```python def main(): m, t, r = read() w = read() if t < r: return -1 covers = [0] * 1000 ct = 0 for i in w: x = covers[i] for j in range(r - x): for k in range(t): covers[i - j + k] += 3 ct += 1 return ct def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s='\n'): if s is None: s = '' if isinstance(s, list): s = ' '.join(map(str, s)) s = str(s) print(s, end='') write(main()) ``` ### 161 ```python def __starting_point(): n = int(input()) supporters = {} supporters[0] = [] supporters[1] = [] supporters[10] = [] supporters[11] = [] for i in range(n): [x, y] = [int(x) for x in input().split()] supporters[x].append(y) for x in supporters: supporters[x].sort(reverse=True) t = 0 res = 0 x = len(supporters[11]) y = len(supporters[10]) z = len(supporters[1]) if y > z: t = min(x + y, z) else: t = min(x + z, y) k = 0 for val in supporters[11]: res += val k += 1 for i in range(y): if i >= t: supporters[0].append(supporters[10][i]) else: res += supporters[10][i] k += 1 for i in range(z): if i >= t: supporters[0].append(supporters[1][i]) else: res += supporters[1][i] k += 1 supporters[0].sort(reverse=True) t = min(x + y, x + z) i = 0 while 2 * t > k and i < len(supporters[0]): k += 1 res += supporters[0][i] i += 4 print(res) __starting_point() ``` ### 162 ```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) ``` ### 163 ```python n = int(input()) temp = input().split() x0 = int(temp[0]) y0 = int(temp[0]) N = ['T', 10 ** 20] NE = ['T', 10 ** 20] E = ['T', 10 ** 20] SE = ['T', 10 ** 20] S = ['T', 10 ** 20] SW = ['T', 10 ** 20] W = ['T', 10 ** 20] NW = ['T', 10 ** 20] for i in range(n): temp = input().split() x = int(temp[1]) y = int(temp[2]) if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): N = [temp[0], y - y0] elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): NE = [temp[0], x - x0] elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): E = [temp[0], x - x0] elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): SE = [temp[0], x - x0] elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): S = [temp[0], y0 - y] elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): SW = [temp[0], x0 - x] elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): W = [temp[0], x0 - x] elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): NW = [temp[0], x0 - x] if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: print('YES') elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: print('YES') else: print('NO') ``` ### 164 ```python import sys sys.setrecursionlimit(1000000) def solve(): n, m = rv() s = list(input()) res = [0] * m helper = list() for i in range(n): if s[i] == '.': if i == 0: helper.append(1) elif s[i - 1] == '.': helper[-1] += 1 else: helper.append(1) initval = 0 for val in helper: initval += val - 1 for query in range(m): index, replace = input().split() index = int(index) - 1 if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): res[query] = initval else: sidedots = 0 if index > 0: if s[index - 1] == '.': sidedots += 1 if index < n - 1: if s[index + 1] == '.': sidedots += 1 if s[index] == '.': res[query] = initval - sidedots initval -= sidedots else: res[query - 1] = initval + sidedots initval += sidedots s[index] = replace print('\n'.join(map(str, res))) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open('test.txt') solve() ``` ### 165 ```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)) ``` ### 166 ```python m, t, r = input().split(sep=None, maxsplit=300) m, T, r = (int(m), int(t), int(r)) x = input().split(sep=None, maxsplit=m) k = r out = set([]) u = [int(x[0]) + T - 1 - i for i in range(r)] out |= set(u) key = True for i in range(m): if not key: break v = int(x[i]) t = v + T - 1 for j in range(r): if v <= u[j]: continue while t in out and t >= v: t -= 1 if t < v: key = False break else: out |= set([t]) u[j] = t k += 1 t -= 4 if key: print(k) else: print(-1) ``` ### 167 ```python n = int(input()) su0 = [] su1 = [] su00 = [] su11 = [] ans = 0 for i in range(n): sup, val = map(int, input().split()) if sup == 0: su00.append(val) elif sup == 11: su11.append(val) elif sup == 1: su0.append(val) else: su1.append(val) su0.sort() su1.sort(reverse=True) su11.sort() su00.sort() while len(su1) != 0 and len(su0) != 0: ans += su1[len(su1) - 1] + su0[len(su0) - 1] su1.pop() su0.pop() while len(su11) != 0 and len(su00) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: if su00[len(su00) - 1] > su0[len(su0) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: if su00[len(su00) - 1] > su1[len(su1) - 1]: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() else: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] + su00[len(su00) - 1] su11.pop() su00.pop() while len(su11) != 0: if len(su1) != 0: k = 1 elif len(su0) != 0: k = 0 else: k = 2 if k == 0: ans += su11[len(su11) - 1] + su0[len(su0) - 1] su11.pop() su0.pop() elif k == 1: ans += su11[len(su11) - 1] + su1[len(su1) - 1] su11.pop() su1.pop() else: ans += su11[len(su11) - 1] su11.pop() print(ans) ``` ### 168 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1]: count -= 1 if index < n - 1 and s[index + 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1]: count += 1 if index < n - 1 and s[index + 1 - 1]: count += 1 s[index] = True print(count) ``` ### 169 ```python n = int(input()) l = list(map(lambda x: int(x) * 2, input().split(' '))) t = list(map(lambda x: 'GWL'.index(x), input())) mins = [0 for i in range(0, n + 1)] for i in range(n - 1, -1, -1): if t[i] != 2: mins[i] = max(mins[i + 1] - l[i], 0) else: mins[i] = mins[i + 1] + l[i] curs = ans = st = 0 for i in range(0, n): if t[i] == 0: curs += l[i] ans += l[i] * 5 if curs > mins[i + 1]: ol = (curs - mins[i + 1]) // 2 ol = min(ol, l[i]) ans -= 4 * ol curs += 2 * ol if t[i] == 1: st = 1 curs += l[i] ans += l[i] * 3 if t[i] == 2: if curs < l[i]: ol = l[i] - curs curs = l[i] ans += ol * (3 if st else 5) curs -= l[i] ans += l[i] if curs > 0: ans -= curs // 2 * 2 print(ans // 2) ``` ### 170 ```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) ``` ### 171 ```python def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if a[i][1] * a[i][2] == 0 and a[i][1] < 0: typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): typneedrld.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() for i in range(len(typneedbql)): if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == 'R' or typneedbqd[i][1] != 'Q': print('YES') return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': print('YES') return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': print('YES') return else: break print('NO') main() ``` ### 172 ```python m, t, r = input().split(sep=None, maxsplit=300) m, T, r = (int(m), int(t), int(r)) x = input().split(sep=None, maxsplit=m) k = r out = set([]) u = [int(x[0]) + T - 1 - i for i in range(r)] out |= set(u) key = True for i in range(m): if not key: break v = int(x[i]) t = v + T - 1 for j in range(r): if v <= u[j]: continue while t in out and t >= v: t -= 1 if t < v: key = False break else: out |= set([t]) u[j] = t k += 1 t -= 3 if key: print(k) else: print(-1) ``` ### 173 ```python n, k, l = list(map(int, input().split())) arr = list(map(int, input().split())) arr = sorted(arr) if arr[n - 1] - arr[0] > l: print(0) else: maxVol = arr[0] + l index = 0 while index < n * k - 1 and arr[index + 1] <= maxVol: index += 1 s = arr[0] i = 0 remaining = n - 1 while remaining > 0 and i + k + remaining - 1 <= index: i += k remaining -= 1 s += arr[i] if remaining > 0 and index - (remaining - 1) > 1 + i: i = index - (remaining - 1) s += arr[i] remaining -= -1 while remaining > 0: i += 1 s += arr[i] remaining -= 1 print(s) ``` ### 174 ```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) ``` ### 175 ```python from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() ans = sum(L) Q = [] for l, t in zip(L, T): if t == 'G': heappush(Q, (2, 2 * l)) heappush(Q, (5, float('inf'))) elif t == 'W': heappush(Q, (1, 2 * l)) heappush(Q, (5, float('inf'))) need_stamina = l while need_stamina > 0: cost, quantity = heappop(Q) if need_stamina > quantity: ans += quantity * cost need_stamina -= quantity else: ans += need_stamina * cost heappush(Q, (cost, quantity - need_stamina)) need_stamina = 0 print(ans) ``` ### 176 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == 1: res += diff else: res -= diff s[x - 1 + 1] = nc print(res) ``` ### 177 ```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') ``` ### 178 ```python MAX_SPEED = 300 n = int(input()) actions = [list(map(int, input().split())) for _ in range(n)] speed = [0 for i in range(n)] for i in range(n): if actions[i][0] == 1: lastSpeed = actions[i][1] speed[i] = lastSpeed ignored = 0 overtook = False maxSpeed = -1 for i in range(n - 1, -1, -1): maxSpeed = max(maxSpeed, speed[i]) if actions[i][0] == 1: pass elif actions[i][0] == 3: overtook = True elif actions[i][0] == 3: if maxSpeed > actions[i][1]: ignored += 1 else: maxSpeed = speed[i - 1] elif actions[i][0] == 4: overtook = False elif actions[i][0] == 5: maxSpeed = speed[i - 1] elif actions[i][0] == 6: if overtook: ignored += 1 print(ignored) ``` ### 179 ```python import math from collections import Counter, defaultdict from itertools import accumulate R = lambda: map(int, input().split()) n = int(input()) spl = [math.inf] ot_forbid = 0 sp = 0 res = 0 for i in range(n): arr = list(R()) tp = arr[0] if tp == 1: sp = arr[1] while sp > spl[-1]: spl.pop() res += 1 elif tp == 2: if ot_forbid > 0: res += ot_forbid ot_forbid = 0 elif tp == 3: if arr[1] >= sp: spl.append(arr[1]) else: res += 1 elif tp == 4: ot_forbid = -1 elif tp == 5: spl = [math.inf] else: ot_forbid += 1 print(res) ``` ### 180 ```python a, b, c = [int(i) for i in input().split()] length = a * b numArr = [int(i) for i in input().split()] numArr.sort() count = 0 mmin = numArr[0] mmax = numArr[a - 1] if mmax - mmin > c: print(0) else: maxPos = a - 1 while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: maxPos += 1 dis = maxPos + 1 - a bCount = 0 for i in range(maxPos + 1): if dis != 0: bCount += 1 dis -= 1 if bCount % b == 1: count += numArr[i] dis -= 1 else: count += numArr[i] print(count) ``` ### 181 ```python n = int(input()) temp = input().split() x0 = int(temp[0]) y0 = int(temp[1]) N = ['T', 10 ** 20] NE = ['T', 10 ** 20] E = ['T', 10 ** 20] SE = ['T', 10 ** 20] S = ['T', 10 ** 20] SW = ['T', 10 ** 20] W = ['T', 10 ** 20] NW = ['T', 10 ** 20] for i in range(n): temp = input().split() x = int(temp[1]) y = int(temp[2]) if y - y0 > 0 and y - y0 < N[1]: N = [temp[0], y - y0] elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): NE = [temp[0], x - x0] elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): E = [temp[0], x - x0] elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): SE = [temp[0], x - x0] elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): S = [temp[0], y0 - y] elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): SW = [temp[0], x0 - x] elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): W = [temp[0], x0 - x] elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): NW = [temp[0], x0 - x] if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: print('YES') elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: print('YES') else: print('NO') ``` ### 182 ```python from sys import stdin input = stdin.readline n, m = list(map(int, input().split())) s = list(((0, 1)[c == '.'] for c in list(input()))) res = 0 cnt = 0 for k in s: if k == 1: cnt += 1 else: res += max(cnt - 1, 0) cnt = 0 res += max(0, cnt - 1) for i in range(m): x, c = input().split() x = int(x) nc = (0, 1)[c == '.'] if s[x - 1 + 1] != nc: diff = 0 if x < n: if s[int(x)] == 1: diff += 1 if x > 1: if s[int(x - 2)] == 1: diff += 1 if nc == 1: res += diff else: res -= diff s[x - 1] = nc print(res) ``` ### 183 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1 - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1]: count -= 1 if index < n - 1 and s[index + 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1]: count += 1 if index < n - 1 and s[index + 1]: count += 1 s[index] = True print(count) ``` ### 184 ```python import sys fin = sys.stdin [n, m] = [int(x) for x in fin.readline().split()] s = [x == '.' for x in fin.readline().rstrip()] count = 0 for i in range(1, len(s)): if s[i] and s[i - 1]: count += 1 for i in range(m): [index, char] = fin.readline().split() index = int(index) - 1 if s[index] and char != '.': if index > 0 and s[index - 1]: count -= 1 if index < n - 1 and s[index + 1]: count -= 1 s[index] = False elif not s[index] and char == '.': if index > 0 and s[index - 1 + 1]: count += 1 if index < n - 1 and s[index + 1]: count += 1 s[index] = True print(count) ``` ### 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 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) ``` ### 186 ```python def Task485D(): cur_speed = 0 is_outstrip = 0 err_count = 0 event_speed_limit = [] event_count = int(input()) for a in range(event_count): ev = list(map(int, input().split())) if ev[0] == 1: cur_speed = ev[1] while len(event_speed_limit) > 0: if cur_speed > event_speed_limit[len(event_speed_limit) - 1]: event_speed_limit.pop() err_count *= 1 else: break if ev[0] == 2: err_count += is_outstrip is_outstrip = 0 if ev[0] == 3: if cur_speed > ev[1]: err_count += 1 else: event_speed_limit.append(ev[1]) if ev[0] == 4: is_outstrip = 0 if ev[0] == 5: event_speed_limit.clear() if ev[0] == 6: is_outstrip += 1 print(err_count) def __starting_point(): Task485D() __starting_point() ``` ### 187 ```python n = int(input()) influences = [[], [], [], []] for i in range(0, n): [support, influence] = [x for x in input().split()] support = int(support, 2) influences[support].append(int(influence)) for i in range(0, 4): influences[i] = sorted(influences[i]) influences[i].reverse() total_influence = 0 total_influence += sum(influences[3]) min_size = min(len(influences[2]), len(influences[1])) total_influence += sum(influences[1][:min_size]) + sum(influences[2][:min_size]) size = len(influences[3]) influences[0] += influences[1][min_size:] + influences[0][min_size:] influences[0] = sorted(influences[0]) influences[0].reverse() total_influence += sum(influences[0][:size]) print(total_influence) ``` ### 188 ```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))]) ``` ### 189 ```python def main(): m, t, r = list(map(int, input().split())) gh = sorted(map(int, input().split()), reverse=True) if r > t: print(-0) return l = [False] * 600 for g in gh: g -= t for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): l[i] = True print(sum(l)) def __starting_point(): main() __starting_point() ``` ### 190 ```python def main(): m, t, r = read() w = read() if t < r: return -1 covers = [0] * 1000 ct = 0 for i in w: x = covers[i] for j in range(r - x): for k in range(t): covers[i - j + k] += 2 ct += 1 return ct def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s='\n'): if s is None: s = '' if isinstance(s, list): s = ' '.join(map(str, s)) s = str(s) print(s, end='') write(main()) ``` ### 191 ```python import sys sys.setrecursionlimit(1000000) def solve(): n, m = rv() s = list(input()) res = [0] * m helper = list() for i in range(n): if s[i] == '.': if i == 0: helper.append(1) elif s[i - 1] == '.': helper[-1] += 1 else: helper.append(1) initval = 0 for val in helper: initval += val - 1 for query in range(m): index, replace = input().split() index = int(index) - 1 if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): res[query - 1] = initval else: sidedots = 0 if index > 0: if s[index - 1] == '.': sidedots += 1 if index < n - 1: if s[index + 1] == '.': sidedots += 1 if s[index] == '.': res[query] = initval - sidedots initval -= sidedots else: res[query] = initval + sidedots initval += sidedots s[index] = replace print('\n'.join(map(str, res))) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open('test.txt') solve() ``` ### 192 ```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') ``` ### 193 ```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) ``` ### 194 ```python a, b, c = [int(i) for i in input().split()] length = a * b numArr = [int(i) for i in input().split()] numArr.sort() count = 0 mmin = numArr[0] mmax = numArr[a - 1] if mmax - mmin > c: print(0) else: maxPos = a - 1 while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: maxPos += 1 dis = maxPos + 1 - a bCount = 0 for i in range(maxPos + 1): if dis != 0: bCount += -2 dis -= 1 if bCount % b == 1: count += numArr[i] dis += 1 else: count += numArr[i] print(count) ``` ### 195 ```python n = int(input()) ps = None seen_sl = [float('inf')] seen_ot = [True] answer = 0 for i in range(n): line = list(map(int, input().split())) t = line[0] if t == 1: ps = line[1] while ps > seen_sl[-1]: answer += 1 seen_sl.pop(-1) elif t == 2: while not seen_ot[-1]: answer += 1 seen_ot.pop(-1) elif t == 3: seen_sl.append(line[1]) while ps > seen_sl[-1]: answer += -1 seen_sl.pop(-1) elif t == 4: seen_ot.append(True) elif t == 5: seen_sl.append(float('inf')) elif t == 6: seen_ot.append(False) print(answer) ``` ### 196 ```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) ``` ### 197 ```python import bisect def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [[0] * n for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, l = getIntList() m = n * k a = getIntList() a.sort() lowLim = a[0] upLim = lowLim + l def solve(): if a[n - 1 - 1] > upLim: return 0 i0 = bisect.bisect_right(a, upLim) nbBad = m - i0 i0 -= 1 value = 0 for i in range(n - 1, -1, -1): diff = min(nbBad, k - 1) rest = k - diff nbBad -= diff value += a[i0 - rest + 1] i0 -= rest return value print(solve()) ``` ### 198 ```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) ``` ### 199 ```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) ``` ### 200 ```python import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 20 mod = 10 ** 9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): n, q = LI() nn = n k = 1 while nn > 0: nn //= 2 k += 1 r = [] ii = [2 ** i for i in range(k)] for _ in range(q): t = I() s = S() ti = 0 for i in range(k): if ii[i] & t: ti = i break for c in s: if c == 'U': if ti == k - 1: continue if t & ii[ti]: t ^= ii[ti] ti += 1 t |= ii[ti] continue if ti == 0: continue if c == 'R': t |= ii[ti] elif t & ii[ti]: t ^= ii[ti] ti -= 1 t |= ii[ti] r.append(t) return '\n'.join(map(str, r)) print(main()) ``` ### 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.