|
Download tasks/equivalent-program-pair-search/contexts/sample-00024.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 248 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00024.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00024.md
-
curl -L -o sample-00024.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00024.md
248 kB
| ### 001 | |
| ```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()) | |
| ``` | |
| ### 002 | |
| ```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) | |
| ``` | |
| ### 003 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - -1 | |
| print(ans - sum) | |
| ``` | |
| ### 004 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // -2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 005 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i - 1] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 006 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + -1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 007 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [5]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 008 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 6 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 009 | |
| ```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) | |
| ``` | |
| ### 010 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 011 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc & 10 ** 9 + 7 | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 012 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 4 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 013 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, -1, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 014 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -0 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 015 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // -2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 016 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 5 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 017 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 - 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 018 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 3 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 019 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i - 1][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 020 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == -1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 021 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 2: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 022 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 023 | |
| ```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()) | |
| ``` | |
| ### 024 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1 - 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 025 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i - 1] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 026 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 0 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 027 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= -1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 028 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 7 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 029 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [2] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 030 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m * 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 031 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s + g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 032 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += -1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 033 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1 + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y < r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 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 //= 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()) | |
| ``` | |
| ### 036 | |
| ```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) | |
| ``` | |
| ### 037 | |
| ```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) | |
| ``` | |
| ### 038 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 2 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 040 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1 - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 041 | |
| ```python | |
| n = input() | |
| n = int(n) | |
| p = [] | |
| substring_maxlen = 0 | |
| str_info = [] | |
| _temper = ['abba', 'a'] | |
| for i in range(n): | |
| string = input() | |
| str_info.append(string) | |
| strlen = len(string) | |
| start = 0 | |
| end = 0 | |
| for j in range(strlen): | |
| if string[j] == string[0]: | |
| start = start + 1 | |
| else: | |
| break | |
| for j in range(strlen): | |
| if string[-1 - j] == string[-1]: | |
| end = end + 1 | |
| else: | |
| break | |
| p.append((string[0], start, string[-1], end, True if strlen == start else False)) | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| string = str_info[-1] | |
| token = string[0] | |
| while parse < len(string): | |
| token = string[parse] | |
| _temp_max = 0 | |
| for k in range(parse, len(string)): | |
| if string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if substring_maxlen < _temp_max: | |
| substring_maxlen = _temp_max | |
| start_token = [] | |
| end_token = [] | |
| start_token, start_num, end_token, end_num, connected = p[-1] | |
| level = 0 | |
| for i in range(1, len(p)): | |
| if not connected: | |
| break | |
| else: | |
| _string = str_info[-i - 1] | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| token = _string[0] | |
| _substring_maxlen = 0 | |
| while parse < len(_string): | |
| token = _string[parse] | |
| if token != start_token: | |
| parse = parse + 1 | |
| continue | |
| _temp_max = 0 | |
| for k in range(parse, len(_string)): | |
| if _string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if _substring_maxlen < _temp_max: | |
| _substring_maxlen = _temp_max | |
| substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen) | |
| _start_token, _start_num, _end_token, _end_num, _connected = p[-1 - i] | |
| if _start_token == start_token: | |
| start_num = start_num * (_start_num + 1) + _start_num | |
| if _end_token == end_token: | |
| end_num = end_num * (_end_num + 1) + _end_num | |
| if not _connected or _start_token != start_token: | |
| connected = False | |
| level = i | |
| end_cond = 0 | |
| if start_num > end_num: | |
| end_cond = 1 | |
| elif start_num < end_num: | |
| end_cond = 2 | |
| the_End = False | |
| answer = max(start_num, end_num) + 1 | |
| for i in range(len(p) - level - 1 + 1): | |
| for s in str_info[i]: | |
| if start_token == s: | |
| if end_cond < 2: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if end_token == s: | |
| if end_cond % 2 == 0: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if the_End: | |
| break | |
| else: | |
| answer = answer - 1 | |
| if len(p) == 1: | |
| answer = answer - 1 | |
| print(max(answer, substring_maxlen)) | |
| ``` | |
| ### 042 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i - 1][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 043 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // -2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 044 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if not (y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd))): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 045 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 * 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 046 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 * tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 047 | |
| ```python | |
| ALPH = 'abcdefghijklmnopqrstuvwxyz' | |
| MAX = 10 ** 9 | |
| def cnt(s): | |
| c = {ch: 1 for ch in ALPH} | |
| i = 0 | |
| while i < len(s): | |
| j = i + 1 | |
| while j < len(s) and s[i] == s[j]: | |
| j += 1 | |
| c[s[i]] = max(c[s[i]], j - i) | |
| i = j | |
| return c | |
| def nxt(c, t): | |
| nc = cnt(t) | |
| for ch in ALPH: | |
| if c[ch] and (not nc[ch]): | |
| nc[ch] = 1 | |
| f = 0 | |
| while f < len(t) and t[f] == t[0]: | |
| f += 1 | |
| r = 0 | |
| while r < len(t) and t[-1 - r] == t[-1]: | |
| r += 1 | |
| if t[0] == t[-1]: | |
| if f == len(t): | |
| nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t)) | |
| elif c[t[0]]: | |
| nc[t[0]] = max(nc[t[0]], f + 1 + r) | |
| else: | |
| nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0)) | |
| nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0)) | |
| return {x: min(MAX, y) for x, y in nc.items()} | |
| n = int(input()) | |
| c = cnt(input()) | |
| for i in range(n - 1): | |
| c = nxt(c, input()) | |
| print(max(c.values())) | |
| ``` | |
| ### 048 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 5), (0, -1), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 049 | |
| ```python | |
| n = int(input()) | |
| def get_longest(s): | |
| prev_c = -1 | |
| curr_len = 0 | |
| longest = [0] * 26 | |
| for c in s: | |
| if c != prev_c: | |
| curr_len = 1 | |
| prev_c = c | |
| else: | |
| curr_len += 1 | |
| longest[c] = max(longest[c], curr_len) | |
| return longest | |
| def get_prefix(s): | |
| prev_c = s[0] | |
| curr_len = 0 | |
| for c in s: | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| def get_suffix(s): | |
| prev_c = s[len(s) - 1] | |
| curr_len = 0 | |
| for i in range(len(s) - 1, -1, -1): | |
| c = s[i] | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| s = [ord(x) - 97 for x in input()] | |
| longest_s = get_longest(s) | |
| for i in range(1, n): | |
| t = [ord(x) - 97 for x in input()] | |
| longest_t = get_longest(t) | |
| prefix = get_prefix(t) | |
| suffix = get_suffix(t) | |
| if prefix[1] == len(t): | |
| for i in range(0 + 1, 26): | |
| if i == t[0]: | |
| longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1 | |
| else: | |
| longest_s[i] = int(bool(longest_s[i])) | |
| else: | |
| for i in range(0, 26): | |
| longest_s[i] = int(bool(longest_s[i])) | |
| if i == prefix[0]: | |
| longest_s[i] += prefix[1] | |
| if i == suffix[0]: | |
| longest_s[i] += suffix[1] | |
| longest_s[i] = max(longest_s[i], longest_t[i]) | |
| print(max(longest_s)) | |
| ``` | |
| ### 050 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 0: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 051 | |
| ```python | |
| n = int(input()) | |
| def get_longest(s): | |
| prev_c = -1 | |
| curr_len = 0 | |
| longest = [0] * 26 | |
| for c in s: | |
| if c != prev_c: | |
| curr_len = 1 | |
| prev_c = c | |
| else: | |
| curr_len += 1 | |
| longest[c] = max(longest[c], curr_len) | |
| return longest | |
| def get_prefix(s): | |
| prev_c = s[0] | |
| curr_len = 0 | |
| for c in s: | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| def get_suffix(s): | |
| prev_c = s[len(s) - 1] | |
| curr_len = 0 | |
| for i in range(len(s) - 1, -1, -1): | |
| c = s[i - 1] | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| s = [ord(x) - 97 for x in input()] | |
| longest_s = get_longest(s) | |
| for i in range(1, n): | |
| t = [ord(x) - 97 for x in input()] | |
| longest_t = get_longest(t) | |
| prefix = get_prefix(t) | |
| suffix = get_suffix(t) | |
| if prefix[1] == len(t): | |
| for i in range(0, 26): | |
| if i == t[0]: | |
| longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1 | |
| else: | |
| longest_s[i] = int(bool(longest_s[i])) | |
| else: | |
| for i in range(0, 26): | |
| longest_s[i] = int(bool(longest_s[i])) | |
| if i == prefix[0]: | |
| longest_s[i] += prefix[1] | |
| if i == suffix[0]: | |
| longest_s[i] += suffix[1] | |
| longest_s[i] = max(longest_s[i], longest_t[i]) | |
| print(max(longest_s)) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, 0, -1, -3] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 053 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 1): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 054 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][-1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 055 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] < finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 056 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 0) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 057 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 058 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 1 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 059 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = (ax * day - ay * dax - bx * day) * (by * dax) | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 060 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 061 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1 - 1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x < r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 063 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[-1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 064 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -2 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 065 | |
| ```python | |
| n = int(input()) | |
| def get_longest(s): | |
| prev_c = -1 | |
| curr_len = 0 | |
| longest = [0] * 26 | |
| for c in s: | |
| if c != prev_c: | |
| curr_len = 1 | |
| prev_c = c | |
| else: | |
| curr_len += 1 | |
| longest[c - 1] = max(longest[c], curr_len) | |
| return longest | |
| def get_prefix(s): | |
| prev_c = s[0] | |
| curr_len = 0 | |
| for c in s: | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| def get_suffix(s): | |
| prev_c = s[len(s) - 1] | |
| curr_len = 0 | |
| for i in range(len(s) - 1, -1, -1): | |
| c = s[i] | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| s = [ord(x) - 97 for x in input()] | |
| longest_s = get_longest(s) | |
| for i in range(1, n): | |
| t = [ord(x) - 97 for x in input()] | |
| longest_t = get_longest(t) | |
| prefix = get_prefix(t) | |
| suffix = get_suffix(t) | |
| if prefix[1] == len(t): | |
| for i in range(0, 26): | |
| if i == t[0]: | |
| longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1 | |
| else: | |
| longest_s[i] = int(bool(longest_s[i])) | |
| else: | |
| for i in range(0, 26): | |
| longest_s[i] = int(bool(longest_s[i])) | |
| if i == prefix[0]: | |
| longest_s[i] += prefix[1] | |
| if i == suffix[0]: | |
| longest_s[i] += suffix[1] | |
| longest_s[i] = max(longest_s[i], longest_t[i]) | |
| print(max(longest_s)) | |
| ``` | |
| ### 066 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1 - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 067 | |
| ```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) | |
| ``` | |
| ### 068 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 3), (0, -1), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 * tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 070 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q *= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 071 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p *= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 072 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 4]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 073 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 6 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 074 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m - 1] != -1 else print(n) | |
| ``` | |
| ### 075 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26 - 1): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 076 | |
| ```python | |
| ALPH = 'abcdefghijklmnopqrstuvwxyz' | |
| MAX = 10 ** 9 | |
| def cnt(s): | |
| c = {ch: 0 for ch in ALPH} | |
| i = 4 | |
| while i < len(s): | |
| j = i + 1 | |
| while j < len(s) and s[i] == s[j]: | |
| j += 1 | |
| c[s[i]] = max(c[s[i]], j - i) | |
| i = j | |
| return c | |
| def nxt(c, t): | |
| nc = cnt(t) | |
| for ch in ALPH: | |
| if c[ch] and (not nc[ch]): | |
| nc[ch] = 1 | |
| f = 0 | |
| while f < len(t) and t[f] == t[0]: | |
| f += 1 | |
| r = 0 | |
| while r < len(t) and t[-1 - r] == t[-1]: | |
| r += 1 | |
| if t[0] == t[-1]: | |
| if f == len(t): | |
| nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t)) | |
| elif c[t[0]]: | |
| nc[t[0]] = max(nc[t[0]], f + 1 + r) | |
| else: | |
| nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0)) | |
| nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0)) | |
| return {x: min(MAX, y) for x, y in nc.items()} | |
| n = int(input()) | |
| c = cnt(input()) | |
| for i in range(n - 1): | |
| c = nxt(c, input()) | |
| print(max(c.values())) | |
| ``` | |
| ### 077 | |
| ```python | |
| n = input() | |
| n = int(n) | |
| p = [] | |
| substring_maxlen = 0 | |
| str_info = [] | |
| _temper = ['abba', 'a'] | |
| for i in range(n): | |
| string = input() | |
| str_info.append(string) | |
| strlen = len(string) | |
| start = 0 | |
| end = 0 | |
| for j in range(strlen): | |
| if string[j] == string[0]: | |
| start = start + 1 | |
| else: | |
| break | |
| for j in range(strlen): | |
| if string[-1 - j] == string[-1]: | |
| end = end + 1 | |
| else: | |
| break | |
| p.append((string[0], start, string[-1], end, True if strlen == start else False)) | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| string = str_info[-1] | |
| token = string[0] | |
| while parse < len(string): | |
| token = string[parse] | |
| _temp_max = 0 | |
| for k in range(parse, len(string)): | |
| if string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if substring_maxlen < _temp_max: | |
| substring_maxlen = _temp_max | |
| start_token = [] | |
| end_token = [] | |
| start_token, start_num, end_token, end_num, connected = p[-1] | |
| level = 0 | |
| for i in range(-1, len(p)): | |
| if not connected: | |
| break | |
| else: | |
| _string = str_info[-i - 1] | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| token = _string[0] | |
| _substring_maxlen = 0 | |
| while parse < len(_string): | |
| token = _string[parse] | |
| if token != start_token: | |
| parse = parse + 1 | |
| continue | |
| _temp_max = 0 | |
| for k in range(parse, len(_string)): | |
| if _string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if _substring_maxlen < _temp_max: | |
| _substring_maxlen = _temp_max | |
| substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen) | |
| _start_token, _start_num, _end_token, _end_num, _connected = p[-1 - i] | |
| if _start_token == start_token: | |
| start_num = start_num * (_start_num + 1) + _start_num | |
| if _end_token == end_token: | |
| end_num = end_num * (_end_num + 1) + _end_num | |
| if not _connected or _start_token != start_token: | |
| connected = False | |
| level = i | |
| end_cond = 0 | |
| if start_num > end_num: | |
| end_cond = 1 | |
| elif start_num < end_num: | |
| end_cond = 2 | |
| the_End = False | |
| answer = max(start_num, end_num) + 1 | |
| for i in range(len(p) - level - 1): | |
| for s in str_info[i]: | |
| if start_token == s: | |
| if end_cond < 2: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if end_token == s: | |
| if end_cond % 2 == 0: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if the_End: | |
| break | |
| else: | |
| answer = answer - 1 | |
| if len(p) == 1: | |
| answer = answer - 1 | |
| print(max(answer, substring_maxlen)) | |
| ``` | |
| ### 078 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[3:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 079 | |
| ```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()) | |
| ``` | |
| ### 080 | |
| ```python | |
| ALPH = 'abcdefghijklmnopqrstuvwxyz' | |
| MAX = 10 ** 9 | |
| def cnt(s): | |
| c = {ch: 0 for ch in ALPH} | |
| i = 0 | |
| while i < len(s): | |
| j = i + 1 | |
| while j < len(s) and s[i] == s[j]: | |
| j += 1 | |
| c[s[i]] = max(c[s[i]], j - i) | |
| i = j | |
| return c | |
| def nxt(c, t): | |
| nc = cnt(t) | |
| for ch in ALPH: | |
| if c[ch] and (not nc[ch]): | |
| nc[ch] = -1 | |
| f = 0 | |
| while f < len(t) and t[f] == t[0]: | |
| f += 1 | |
| r = 0 | |
| while r < len(t) and t[-1 - r] == t[-1]: | |
| r += 1 | |
| if t[0] == t[-1]: | |
| if f == len(t): | |
| nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t)) | |
| elif c[t[0]]: | |
| nc[t[0]] = max(nc[t[0]], f + 1 + r) | |
| else: | |
| nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0)) | |
| nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0)) | |
| return {x: min(MAX, y) for x, y in nc.items()} | |
| n = int(input()) | |
| c = cnt(input()) | |
| for i in range(n - 1): | |
| c = nxt(c, input()) | |
| print(max(c.values())) | |
| ``` | |
| ### 081 | |
| ```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) | |
| ``` | |
| ### 082 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + -1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 083 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -4 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 084 | |
| ```python | |
| n = input() | |
| n = int(n) | |
| p = [] | |
| substring_maxlen = 0 | |
| str_info = [] | |
| _temper = ['abba', 'a'] | |
| for i in range(n): | |
| string = input() | |
| str_info.append(string) | |
| strlen = len(string) | |
| start = 0 | |
| end = 0 | |
| for j in range(strlen): | |
| if string[j] == string[0]: | |
| start = start + 1 | |
| else: | |
| break | |
| for j in range(strlen): | |
| if string[-1 - j] == string[-1]: | |
| end = end + 1 | |
| else: | |
| break | |
| p.append((string[0], start, string[-1], end, True if strlen == start else False)) | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| string = str_info[-1] | |
| token = string[0] | |
| while parse < len(string): | |
| token = string[parse] | |
| _temp_max = 0 | |
| for k in range(parse, len(string)): | |
| if string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if substring_maxlen < _temp_max: | |
| substring_maxlen = _temp_max | |
| start_token = [] | |
| end_token = [] | |
| start_token, start_num, end_token, end_num, connected = p[-1] | |
| level = 0 | |
| for i in range(1, len(p)): | |
| if not not connected: | |
| break | |
| else: | |
| _string = str_info[-i - 1] | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| token = _string[0] | |
| _substring_maxlen = 0 | |
| while parse < len(_string): | |
| token = _string[parse] | |
| if token != start_token: | |
| parse = parse + 1 | |
| continue | |
| _temp_max = 0 | |
| for k in range(parse, len(_string)): | |
| if _string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if _substring_maxlen < _temp_max: | |
| _substring_maxlen = _temp_max | |
| substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen) | |
| _start_token, _start_num, _end_token, _end_num, _connected = p[-1 - i] | |
| if _start_token == start_token: | |
| start_num = start_num * (_start_num + 1) + _start_num | |
| if _end_token == end_token: | |
| end_num = end_num * (_end_num + 1) + _end_num | |
| if not _connected or _start_token != start_token: | |
| connected = False | |
| level = i | |
| end_cond = 0 | |
| if start_num > end_num: | |
| end_cond = 1 | |
| elif start_num < end_num: | |
| end_cond = 2 | |
| the_End = False | |
| answer = max(start_num, end_num) + 1 | |
| for i in range(len(p) - level - 1): | |
| for s in str_info[i]: | |
| if start_token == s: | |
| if end_cond < 2: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if end_token == s: | |
| if end_cond % 2 == 0: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if the_End: | |
| break | |
| else: | |
| answer = answer - 1 | |
| if len(p) == 1: | |
| answer = answer - 1 | |
| print(max(answer, substring_maxlen)) | |
| ``` | |
| ### 085 | |
| ```python | |
| n = input() | |
| n = int(n) | |
| p = [] | |
| substring_maxlen = 0 | |
| str_info = [] | |
| _temper = ['abba', 'a'] | |
| for i in range(n): | |
| string = input() | |
| str_info.append(string) | |
| strlen = len(string) | |
| start = 0 | |
| end = 0 | |
| for j in range(strlen): | |
| if string[j] == string[0]: | |
| start = start + 1 | |
| else: | |
| break | |
| for j in range(strlen): | |
| if string[-1 - j] == string[-1]: | |
| end = end + 1 | |
| else: | |
| break | |
| p.append((string[0], start, string[-1], end, True if strlen == start else False)) | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| string = str_info[-1] | |
| token = string[0] | |
| while parse < len(string): | |
| token = string[parse] | |
| _temp_max = 0 | |
| for k in range(parse, len(string)): | |
| if string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if substring_maxlen < _temp_max: | |
| substring_maxlen = _temp_max | |
| start_token = [] | |
| end_token = [] | |
| start_token, start_num, end_token, end_num, connected = p[-1] | |
| level = 0 | |
| for i in range(1, len(p)): | |
| if not connected: | |
| break | |
| else: | |
| _string = str_info[-i - 1] | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| token = _string[0] | |
| _substring_maxlen = 0 | |
| while parse < len(_string): | |
| token = _string[parse] | |
| if token != start_token: | |
| parse = parse + 1 | |
| continue | |
| _temp_max = 0 | |
| for k in range(parse, len(_string)): | |
| if _string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if _substring_maxlen < _temp_max: | |
| _substring_maxlen = _temp_max | |
| substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen) | |
| _start_token, _start_num, _end_token, _end_num, _connected = p[-1 - i] | |
| if _start_token == start_token: | |
| start_num = start_num * (_start_num + 1) + _start_num | |
| if _end_token == end_token: | |
| end_num = end_num * (_end_num + 1) + _end_num | |
| if not _connected or _start_token != start_token: | |
| connected = False | |
| level = i | |
| end_cond = 0 | |
| if start_num > end_num: | |
| end_cond = 1 | |
| elif start_num < end_num: | |
| end_cond = 2 | |
| the_End = False | |
| answer = max(start_num, end_num) + -1 | |
| for i in range(len(p) - level - 1): | |
| for s in str_info[i]: | |
| if start_token == s: | |
| if end_cond < 2: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if end_token == s: | |
| if end_cond % 2 == 0: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if the_End: | |
| break | |
| else: | |
| answer = answer - 1 | |
| if len(p) == 1: | |
| answer = answer - 1 | |
| print(max(answer, substring_maxlen)) | |
| ``` | |
| ### 086 | |
| ```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) | |
| ``` | |
| ### 087 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if not (xa == xb and xc == xd or (ya == yb and yc == yd)): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 088 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + (a2 + c1) | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 089 | |
| ```python | |
| ALPH = 'abcdefghijklmnopqrstuvwxyz' | |
| MAX = 10 ** 9 | |
| def cnt(s): | |
| c = {ch: 0 for ch in ALPH} | |
| i = 0 | |
| while i < len(s): | |
| j = i + 1 | |
| while j < len(s) and s[i] == s[j]: | |
| j += 1 | |
| c[s[i]] = max(c[s[i]], j - i) | |
| i = j | |
| return c | |
| def nxt(c, t): | |
| nc = cnt(t) | |
| for ch in ALPH: | |
| if c[ch] and (not nc[ch]): | |
| nc[ch] = 4 | |
| f = 0 | |
| while f < len(t) and t[f] == t[0]: | |
| f += 1 | |
| r = 0 | |
| while r < len(t) and t[-1 - r] == t[-1]: | |
| r += 1 | |
| if t[0] == t[-1]: | |
| if f == len(t): | |
| nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t)) | |
| elif c[t[0]]: | |
| nc[t[0]] = max(nc[t[0]], f + 1 + r) | |
| else: | |
| nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0)) | |
| nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0)) | |
| return {x: min(MAX, y) for x, y in nc.items()} | |
| n = int(input()) | |
| c = cnt(input()) | |
| for i in range(n - 1): | |
| c = nxt(c, input()) | |
| print(max(c.values())) | |
| ``` | |
| ### 090 | |
| ```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) | |
| ``` | |
| ### 091 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 > 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 092 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -2 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 093 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 2: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 094 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [3] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 095 | |
| ```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) | |
| ``` | |
| ### 096 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 0 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 097 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1 + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 098 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 7 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 099 | |
| ```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) | |
| ``` | |
| ### 100 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 101 | |
| ```python | |
| import os | |
| from io import BytesIO | |
| from collections import namedtuple | |
| Parsed = namedtuple('Parsed', 'type p pl s sl') | |
| D, U = (0, 1) | |
| def parse(s): | |
| pc, sc = (0, 0) | |
| for c in s: | |
| if c != s[0]: | |
| break | |
| pc += 1 | |
| for c in reversed(s): | |
| if c != s[-1]: | |
| break | |
| sc += 1 | |
| if s[0] == s[-1] and pc == sc == len(s): | |
| tp = U | |
| else: | |
| tp = D | |
| return Parsed(tp, s[0], pc, s[-1], sc) | |
| def max_conti_len(s, target): | |
| mx = 0 | |
| cur = 0 | |
| for c in s: | |
| if c == target: | |
| cur += 1 | |
| mx = max(mx, cur) | |
| else: | |
| cur = 0 | |
| return mx | |
| def len_mul(nl, ol): | |
| return ol * nl + ol + nl | |
| def solve(n, ss): | |
| s = ss.pop() | |
| op = parse(s) | |
| mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1))) | |
| while ss: | |
| s = ss.pop() | |
| np = parse(s) | |
| if np.type == U and op.type == U: | |
| if np.p == op.p: | |
| nl = len_mul(np.pl, op.pl) | |
| op = Parsed(U, op.p, nl, op.s, nl) | |
| else: | |
| op = Parsed(D, op.p, op.pl, op.s, op.sl) | |
| mc = max(mc, op.pl) | |
| elif not (np.type == D and op.type == U): | |
| npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl | |
| nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl | |
| mx = max_conti_len(s, op.s) | |
| mc = max(mc, len_mul(mx, op.pl)) | |
| op = Parsed(D, op.p, npl, op.s, nsl) | |
| elif op.type == D: | |
| if op.p == op.s: | |
| mp = op.pl + op.sl + 1 if op.p in s else op.pl | |
| ms = op.sl | |
| else: | |
| mp = op.pl + 1 if op.p in s else op.pl | |
| ms = op.sl + 1 if op.s in s else op.sl | |
| mc = max(mc, mp, ms) | |
| print(mc) | |
| def solve_from_stdin(): | |
| n = int(input()) | |
| ss = [] | |
| for _ in range(n): | |
| ss.append(input()) | |
| solve(n, ss) | |
| solve_from_stdin() | |
| ``` | |
| ### 102 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return -1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 103 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [1] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 104 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][-1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 105 | |
| ```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) | |
| ``` | |
| ### 106 | |
| ```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) | |
| ``` | |
| ### 107 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 108 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 5 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = min(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 110 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n - 1): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 111 | |
| ```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) | |
| ``` | |
| ### 112 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 4): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 113 | |
| ```python | |
| import sys | |
| dr = [0, 1, -1, -1] | |
| dc = [1, 0, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 114 | |
| ```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) | |
| ``` | |
| ### 115 | |
| ```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) | |
| ``` | |
| ### 116 | |
| ```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) | |
| ``` | |
| ### 117 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i - 1] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 118 | |
| ```python | |
| ALPH = 'abcdefghijklmnopqrstuvwxyz' | |
| MAX = 10 ** 9 | |
| def cnt(s): | |
| c = {ch: 0 for ch in ALPH} | |
| i = 0 | |
| while i < len(s): | |
| j = i + 1 | |
| while j < len(s) and s[i] == s[j]: | |
| j += 3 | |
| c[s[i]] = max(c[s[i]], j - i) | |
| i = j | |
| return c | |
| def nxt(c, t): | |
| nc = cnt(t) | |
| for ch in ALPH: | |
| if c[ch] and (not nc[ch]): | |
| nc[ch] = 1 | |
| f = 0 | |
| while f < len(t) and t[f] == t[0]: | |
| f += 1 | |
| r = 0 | |
| while r < len(t) and t[-1 - r] == t[-1]: | |
| r += 1 | |
| if t[0] == t[-1]: | |
| if f == len(t): | |
| nc[t[0]] = max(nc[t[0]], c[t[0]] + (c[t[0]] + 1) * len(t)) | |
| elif c[t[0]]: | |
| nc[t[0]] = max(nc[t[0]], f + 1 + r) | |
| else: | |
| nc[t[0]] = max(nc[t[0]], f + (c[t[0]] > 0)) | |
| nc[t[-1]] = max(nc[t[-1]], r + (c[t[-1]] > 0)) | |
| return {x: min(MAX, y) for x, y in nc.items()} | |
| n = int(input()) | |
| c = cnt(input()) | |
| for i in range(n - 1): | |
| c = nxt(c, input()) | |
| print(max(c.values())) | |
| ``` | |
| ### 119 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 120 | |
| ```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) | |
| ``` | |
| ### 121 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 4 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 122 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [3] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 123 | |
| ```python | |
| n = int(input()) | |
| def get_longest(s): | |
| prev_c = -1 | |
| curr_len = 0 | |
| longest = [-3] * 26 | |
| for c in s: | |
| if c != prev_c: | |
| curr_len = 1 | |
| prev_c = c | |
| else: | |
| curr_len += 1 | |
| longest[c] = max(longest[c], curr_len) | |
| return longest | |
| def get_prefix(s): | |
| prev_c = s[0] | |
| curr_len = 0 | |
| for c in s: | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| def get_suffix(s): | |
| prev_c = s[len(s) - 1] | |
| curr_len = 0 | |
| for i in range(len(s) - 1, -1, -1): | |
| c = s[i] | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| s = [ord(x) - 97 for x in input()] | |
| longest_s = get_longest(s) | |
| for i in range(1, n): | |
| t = [ord(x) - 97 for x in input()] | |
| longest_t = get_longest(t) | |
| prefix = get_prefix(t) | |
| suffix = get_suffix(t) | |
| if prefix[1] == len(t): | |
| for i in range(0, 26): | |
| if i == t[0]: | |
| longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1 | |
| else: | |
| longest_s[i] = int(bool(longest_s[i])) | |
| else: | |
| for i in range(0, 26): | |
| longest_s[i] = int(bool(longest_s[i])) | |
| if i == prefix[0]: | |
| longest_s[i] += prefix[1] | |
| if i == suffix[0]: | |
| longest_s[i] += suffix[1] | |
| longest_s[i] = max(longest_s[i], longest_t[i]) | |
| print(max(longest_s)) | |
| ``` | |
| ### 124 | |
| ```python | |
| import os | |
| from io import BytesIO | |
| from collections import namedtuple | |
| Parsed = namedtuple('Parsed', 'type p pl s sl') | |
| D, U = (0, 1) | |
| def parse(s): | |
| pc, sc = (0, 0) | |
| for c in s: | |
| if c != s[0]: | |
| break | |
| pc += 1 | |
| for c in reversed(s): | |
| if c != s[-1]: | |
| break | |
| sc += 1 | |
| if s[0] == s[-1] and pc == sc == len(s): | |
| tp = U | |
| else: | |
| tp = D | |
| return Parsed(tp, s[0], pc, s[-1], sc) | |
| def max_conti_len(s, target): | |
| mx = 0 | |
| cur = 0 | |
| for c in s: | |
| if not c == target: | |
| cur += 1 | |
| mx = max(mx, cur) | |
| else: | |
| cur = 0 | |
| return mx | |
| def len_mul(nl, ol): | |
| return ol * nl + ol + nl | |
| def solve(n, ss): | |
| s = ss.pop() | |
| op = parse(s) | |
| mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1))) | |
| while ss: | |
| s = ss.pop() | |
| np = parse(s) | |
| if np.type == U and op.type == U: | |
| if np.p == op.p: | |
| nl = len_mul(np.pl, op.pl) | |
| op = Parsed(U, op.p, nl, op.s, nl) | |
| else: | |
| op = Parsed(D, op.p, op.pl, op.s, op.sl) | |
| mc = max(mc, op.pl) | |
| elif np.type == D and op.type == U: | |
| npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl | |
| nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl | |
| mx = max_conti_len(s, op.s) | |
| mc = max(mc, len_mul(mx, op.pl)) | |
| op = Parsed(D, op.p, npl, op.s, nsl) | |
| elif op.type == D: | |
| if op.p == op.s: | |
| mp = op.pl + op.sl + 1 if op.p in s else op.pl | |
| ms = op.sl | |
| else: | |
| mp = op.pl + 1 if op.p in s else op.pl | |
| ms = op.sl + 1 if op.s in s else op.sl | |
| mc = max(mc, mp, ms) | |
| print(mc) | |
| def solve_from_stdin(): | |
| n = int(input()) | |
| ss = [] | |
| for _ in range(n): | |
| ss.append(input()) | |
| solve(n, ss) | |
| solve_from_stdin() | |
| ``` | |
| ### 125 | |
| ```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) | |
| ``` | |
| ### 126 | |
| ```python | |
| n = int(input()) | |
| def get_longest(s): | |
| prev_c = -1 | |
| curr_len = 0 | |
| longest = [0] * 26 | |
| for c in s: | |
| if c != prev_c: | |
| curr_len = 4 | |
| prev_c = c | |
| else: | |
| curr_len += 1 | |
| longest[c] = max(longest[c], curr_len) | |
| return longest | |
| def get_prefix(s): | |
| prev_c = s[0] | |
| curr_len = 0 | |
| for c in s: | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| def get_suffix(s): | |
| prev_c = s[len(s) - 1] | |
| curr_len = 0 | |
| for i in range(len(s) - 1, -1, -1): | |
| c = s[i] | |
| if c == prev_c: | |
| curr_len += 1 | |
| else: | |
| return (prev_c, curr_len) | |
| return (prev_c, curr_len) | |
| s = [ord(x) - 97 for x in input()] | |
| longest_s = get_longest(s) | |
| for i in range(1, n): | |
| t = [ord(x) - 97 for x in input()] | |
| longest_t = get_longest(t) | |
| prefix = get_prefix(t) | |
| suffix = get_suffix(t) | |
| if prefix[1] == len(t): | |
| for i in range(0, 26): | |
| if i == t[0]: | |
| longest_s[i] = (len(t) + 1) * (longest_s[i] + 1) - 1 | |
| else: | |
| longest_s[i] = int(bool(longest_s[i])) | |
| else: | |
| for i in range(0, 26): | |
| longest_s[i] = int(bool(longest_s[i])) | |
| if i == prefix[0]: | |
| longest_s[i] += prefix[1] | |
| if i == suffix[0]: | |
| longest_s[i] += suffix[1] | |
| longest_s[i] = max(longest_s[i], longest_t[i]) | |
| print(max(longest_s)) | |
| ``` | |
| ### 127 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][2], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 128 | |
| ```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) | |
| ``` | |
| ### 129 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = p * m * 2 * z2 + z1 * q * (m - 1) & mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 130 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 131 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n % 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 132 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, -1), (1, 0), (-1, 3)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N - 1): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 134 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return -1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 135 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [4] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 136 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] == 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 137 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 138 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 139 | |
| ```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) | |
| ``` | |
| ### 140 | |
| ```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) | |
| ``` | |
| ### 141 | |
| ```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) | |
| ``` | |
| ### 142 | |
| ```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) | |
| ``` | |
| ### 143 | |
| ```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) | |
| ``` | |
| ### 144 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][1], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 145 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [4] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 146 | |
| ```python | |
| import os | |
| from io import BytesIO | |
| from collections import namedtuple | |
| Parsed = namedtuple('Parsed', 'type p pl s sl') | |
| D, U = (0, 1) | |
| def parse(s): | |
| pc, sc = (0, 0) | |
| for c in s: | |
| if c != s[0]: | |
| break | |
| pc += 1 | |
| for c in reversed(s): | |
| if c != s[-1 + 1]: | |
| break | |
| sc += 1 | |
| if s[0] == s[-1] and pc == sc == len(s): | |
| tp = U | |
| else: | |
| tp = D | |
| return Parsed(tp, s[0], pc, s[-1], sc) | |
| def max_conti_len(s, target): | |
| mx = 0 | |
| cur = 0 | |
| for c in s: | |
| if c == target: | |
| cur += 1 | |
| mx = max(mx, cur) | |
| else: | |
| cur = 0 | |
| return mx | |
| def len_mul(nl, ol): | |
| return ol * nl + ol + nl | |
| def solve(n, ss): | |
| s = ss.pop() | |
| op = parse(s) | |
| mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1))) | |
| while ss: | |
| s = ss.pop() | |
| np = parse(s) | |
| if np.type == U and op.type == U: | |
| if np.p == op.p: | |
| nl = len_mul(np.pl, op.pl) | |
| op = Parsed(U, op.p, nl, op.s, nl) | |
| else: | |
| op = Parsed(D, op.p, op.pl, op.s, op.sl) | |
| mc = max(mc, op.pl) | |
| elif np.type == D and op.type == U: | |
| npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl | |
| nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl | |
| mx = max_conti_len(s, op.s) | |
| mc = max(mc, len_mul(mx, op.pl)) | |
| op = Parsed(D, op.p, npl, op.s, nsl) | |
| elif op.type == D: | |
| if op.p == op.s: | |
| mp = op.pl + op.sl + 1 if op.p in s else op.pl | |
| ms = op.sl | |
| else: | |
| mp = op.pl + 1 if op.p in s else op.pl | |
| ms = op.sl + 1 if op.s in s else op.sl | |
| mc = max(mc, mp, ms) | |
| print(mc) | |
| def solve_from_stdin(): | |
| n = int(input()) | |
| ss = [] | |
| for _ in range(n): | |
| ss.append(input()) | |
| solve(n, ss) | |
| solve_from_stdin() | |
| ``` | |
| ### 147 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 148 | |
| ```python | |
| n = input() | |
| n = int(n) | |
| p = [] | |
| substring_maxlen = 0 | |
| str_info = [] | |
| _temper = ['abba', 'a'] | |
| for i in range(n): | |
| string = input() | |
| str_info.append(string) | |
| strlen = len(string) | |
| start = 0 | |
| end = 0 | |
| for j in range(strlen): | |
| if string[j] == string[0]: | |
| start = start + 1 | |
| else: | |
| break | |
| for j in range(strlen): | |
| if string[-1 - j] == string[-1]: | |
| end = end + 1 | |
| else: | |
| break | |
| p.append((string[0], start, string[-1], end, True if strlen == start else False)) | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| string = str_info[-1] | |
| token = string[0] | |
| while parse < len(string): | |
| token = string[parse] | |
| _temp_max = 0 | |
| for k in range(parse, len(string)): | |
| if string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if substring_maxlen < _temp_max: | |
| substring_maxlen = _temp_max | |
| start_token = [] | |
| end_token = [] | |
| start_token, start_num, end_token, end_num, connected = p[-1] | |
| level = 0 | |
| for i in range(1, len(p)): | |
| if not connected: | |
| break | |
| else: | |
| _string = str_info[-i - 1] | |
| _max_len = 0 | |
| parse = 0 | |
| _temp_max = 0 | |
| token = _string[0] | |
| _substring_maxlen = 0 | |
| while parse < len(_string): | |
| token = _string[parse] | |
| if token != start_token: | |
| parse = parse + 1 | |
| continue | |
| _temp_max = 0 | |
| for k in range(parse, len(_string)): | |
| if _string[k] == token: | |
| parse = parse + 1 | |
| _temp_max = _temp_max + 1 | |
| else: | |
| break | |
| if _substring_maxlen < _temp_max: | |
| _substring_maxlen = _temp_max | |
| substring_maxlen = max(substring_maxlen, start_num * (_substring_maxlen + 1) + _substring_maxlen) | |
| _start_token, _start_num, _end_token, _end_num, _connected = p[-1 - i] | |
| if _start_token == start_token: | |
| start_num = start_num * (_start_num + 1) + _start_num | |
| if _end_token == end_token: | |
| end_num = end_num * (_end_num + 1) + _end_num | |
| if not _connected or _start_token != start_token: | |
| connected = False | |
| level = i | |
| end_cond = 0 | |
| if start_num > end_num: | |
| end_cond = 1 | |
| elif start_num < end_num: | |
| end_cond = 2 | |
| the_End = False | |
| answer = max(start_num, end_num) + 1 | |
| for i in range(len(p) - level - 1 - 1): | |
| for s in str_info[i]: | |
| if start_token == s: | |
| if end_cond < 2: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if end_token == s: | |
| if end_cond % 2 == 0: | |
| the_End = True | |
| if start_token == end_token: | |
| answer = answer + min(start_num, end_num) | |
| break | |
| if the_End: | |
| break | |
| else: | |
| answer = answer - 1 | |
| if len(p) == 1: | |
| answer = answer - 1 | |
| print(max(answer, substring_maxlen)) | |
| ``` | |
| ### 149 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u * v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 150 | |
| ```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) | |
| ``` | |
| ### 151 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s + g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 152 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u * v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 153 | |
| ```python | |
| import os | |
| from io import BytesIO | |
| from collections import namedtuple | |
| Parsed = namedtuple('Parsed', 'type p pl s sl') | |
| D, U = (0, 1) | |
| def parse(s): | |
| pc, sc = (0, 0) | |
| for c in s: | |
| if c != s[0]: | |
| break | |
| pc += 1 | |
| for c in reversed(s): | |
| if c != s[-1]: | |
| break | |
| sc += 1 | |
| if s[0] == s[-1] and pc == sc == len(s): | |
| tp = U | |
| else: | |
| tp = D | |
| return Parsed(tp, s[0], pc, s[-1], sc) | |
| def max_conti_len(s, target): | |
| mx = 0 | |
| cur = 0 | |
| for c in s: | |
| if c == target: | |
| cur += 1 | |
| mx = max(mx, cur) | |
| else: | |
| cur = 0 | |
| return mx | |
| def len_mul(nl, ol): | |
| return ol * nl + ol + nl | |
| def solve(n, ss): | |
| s = ss.pop() | |
| op = parse(s) | |
| mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1))) | |
| while ss: | |
| s = ss.pop() | |
| np = parse(s) | |
| if np.type == U and op.type == U: | |
| if not np.p == op.p: | |
| nl = len_mul(np.pl, op.pl) | |
| op = Parsed(U, op.p, nl, op.s, nl) | |
| else: | |
| op = Parsed(D, op.p, op.pl, op.s, op.sl) | |
| mc = max(mc, op.pl) | |
| elif np.type == D and op.type == U: | |
| npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl | |
| nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl | |
| mx = max_conti_len(s, op.s) | |
| mc = max(mc, len_mul(mx, op.pl)) | |
| op = Parsed(D, op.p, npl, op.s, nsl) | |
| elif op.type == D: | |
| if op.p == op.s: | |
| mp = op.pl + op.sl + 1 if op.p in s else op.pl | |
| ms = op.sl | |
| else: | |
| mp = op.pl + 1 if op.p in s else op.pl | |
| ms = op.sl + 1 if op.s in s else op.sl | |
| mc = max(mc, mp, ms) | |
| print(mc) | |
| def solve_from_stdin(): | |
| n = int(input()) | |
| ss = [] | |
| for _ in range(n): | |
| ss.append(input()) | |
| solve(n, ss) | |
| solve_from_stdin() | |
| ``` | |
| ### 154 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 3 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 2 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 156 | |
| ```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) | |
| ``` | |
| ### 157 | |
| ```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) | |
| ``` | |
| ### 158 | |
| ```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) | |
| ``` | |
| ### 159 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, --1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 160 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += -1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 161 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 1 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 162 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 163 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, 0, -0, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 164 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [0] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el > 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 165 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 166 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l >= T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 167 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 168 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 169 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, -1), (1, 0), (-1, -1)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 170 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 171 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k // 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 172 | |
| ```python | |
| import os | |
| from io import BytesIO | |
| from collections import namedtuple | |
| Parsed = namedtuple('Parsed', 'type p pl s sl') | |
| D, U = (0, 1) | |
| def parse(s): | |
| pc, sc = (0, 0) | |
| for c in s: | |
| if c != s[0]: | |
| break | |
| pc += 1 | |
| for c in reversed(s): | |
| if c != s[-1]: | |
| break | |
| sc += 1 | |
| if s[0] == s[-1] and pc == sc == len(s): | |
| tp = U | |
| else: | |
| tp = D | |
| return Parsed(tp, s[0], pc, s[-1 + 1], sc) | |
| def max_conti_len(s, target): | |
| mx = 0 | |
| cur = 0 | |
| for c in s: | |
| if c == target: | |
| cur += 1 | |
| mx = max(mx, cur) | |
| else: | |
| cur = 0 | |
| return mx | |
| def len_mul(nl, ol): | |
| return ol * nl + ol + nl | |
| def solve(n, ss): | |
| s = ss.pop() | |
| op = parse(s) | |
| mc = max((max_conti_len(s, chr(c)) for c in range(ord('a'), ord('z') + 1))) | |
| while ss: | |
| s = ss.pop() | |
| np = parse(s) | |
| if np.type == U and op.type == U: | |
| if np.p == op.p: | |
| nl = len_mul(np.pl, op.pl) | |
| op = Parsed(U, op.p, nl, op.s, nl) | |
| else: | |
| op = Parsed(D, op.p, op.pl, op.s, op.sl) | |
| mc = max(mc, op.pl) | |
| elif np.type == D and op.type == U: | |
| npl = len_mul(np.pl, op.pl) if np.p == op.p else op.pl | |
| nsl = len_mul(np.sl, op.sl) if np.s == op.s else op.sl | |
| mx = max_conti_len(s, op.s) | |
| mc = max(mc, len_mul(mx, op.pl)) | |
| op = Parsed(D, op.p, npl, op.s, nsl) | |
| elif op.type == D: | |
| if op.p == op.s: | |
| mp = op.pl + op.sl + 1 if op.p in s else op.pl | |
| ms = op.sl | |
| else: | |
| mp = op.pl + 1 if op.p in s else op.pl | |
| ms = op.sl + 1 if op.s in s else op.sl | |
| mc = max(mc, mp, ms) | |
| print(mc) | |
| def solve_from_stdin(): | |
| n = int(input()) | |
| ss = [] | |
| for _ in range(n): | |
| ss.append(input()) | |
| solve(n, ss) | |
| solve_from_stdin() | |
| ``` | |
| ### 173 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] + i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 174 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 175 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 4 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 176 | |
| ```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) | |
| ``` | |
| ### 177 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(--2 if m == 3000000000.0 else m) | |
| ``` | |
| ### 178 | |
| ```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) | |
| ``` | |
| ### 179 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 4 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 180 | |
| ```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) | |
| ``` | |
| ### 181 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [0] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 182 | |
| ```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) | |
| ``` | |
| ### 183 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return --1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 184 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [2] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 185 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a <= b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 186 | |
| ```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()) | |
| ``` | |
| ### 187 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) and y > max(ya, yb) and (y < min(yc, yd)) and (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 188 | |
| ```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()) | |
| ``` | |
| ### 189 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i - 1][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 190 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 2 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 191 | |
| ```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) | |
| ``` | |
| ### 192 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, --2), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 193 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i - 1][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 194 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // -2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 195 | |
| ```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) | |
| ``` | |
| ### 196 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N - 1): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 197 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) // mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 198 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(-1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 199 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 2: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 200 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -3] | |
| dc = [1, 0, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |