|
Download tasks/equivalent-program-pair-search/contexts/sample-00031.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 290 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00031.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00031.md
-
curl -L -o sample-00031.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00031.md
290 kB
| ### 001 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y > 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 002 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum <= 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 003 | |
| ```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() | |
| ``` | |
| ### 004 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 005 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[5:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 006 | |
| ```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) | |
| ``` | |
| ### 007 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == -2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 008 | |
| ```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() | |
| ``` | |
| ### 009 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 0 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 010 | |
| ```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)) | |
| ``` | |
| ### 011 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * 2 * i | |
| sofar += b * (2 * i + -1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 012 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s >= s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 013 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch != 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 014 | |
| ```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() | |
| ``` | |
| ### 015 | |
| ```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() | |
| ``` | |
| ### 016 | |
| ```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)) | |
| ``` | |
| ### 017 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 1 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 018 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1] = a[i][n - 1] | |
| pref_up[i][n - 1 - 1] = a[i][n - 1] | |
| pref_down[i][n - 1] = a[i][n - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 019 | |
| ```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) | |
| ``` | |
| ### 020 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if not c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 021 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - -1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 022 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1 + 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 023 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][0] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 024 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 3] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 026 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 027 | |
| ```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() | |
| ``` | |
| ### 028 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i - 1] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 029 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1 - 1] = a[i][n - 1] | |
| pref_up[i][n - 1] = a[i][n - 1] | |
| pref_down[i][n - 1] = a[i][n - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 030 | |
| ```python | |
| from sys import stdin | |
| from math import * | |
| line = stdin.readline().rstrip().split() | |
| n = int(line[0]) | |
| numbers = list(map(int, stdin.readline().rstrip().split())) | |
| numbers2 = list(map(int, stdin.readline().rstrip().split())) | |
| growRateAccum = 0 | |
| topAccum = 0 | |
| bottomAccum = 0 | |
| accumsB = [0] * n | |
| accumsT = [0] * n | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers[i] | |
| if i < n - 1: | |
| growRateAccum *= numbers2[i + 1] | |
| accum += growRateAccum | |
| accumsB[i] = accum | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers2[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers2[i] | |
| if i < n - 1: | |
| growRateAccum += numbers[i + 1] | |
| accum += growRateAccum | |
| accumsT[i] = accum | |
| cMax = 0 | |
| currAccum = 0 | |
| growRateAccum = sum(numbers[1:]) + sum(numbers2[1:]) | |
| for i in range(n): | |
| if i % 2 == 0: | |
| cMax = max(cMax, currAccum + accumsT[i]) | |
| else: | |
| cMax = max(cMax, currAccum + accumsB[i]) | |
| if i < n - 1: | |
| if i % 2 == 0: | |
| currAccum += numbers2[i] + growRateAccum * 2 | |
| else: | |
| currAccum += numbers[i] + growRateAccum * 2 | |
| growRateAccum -= numbers[i + 1] | |
| growRateAccum -= numbers2[i + 1] | |
| print(cMax) | |
| ``` | |
| ### 031 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1 - 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 032 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans //= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 033 | |
| ```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) | |
| ``` | |
| ### 034 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-3] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 035 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(2, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 036 | |
| ```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)) | |
| ``` | |
| ### 037 | |
| ```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) | |
| ``` | |
| ### 038 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| curl_ud_weights = [0] * n | |
| curl_du_weights = [0] * n | |
| sums = [0] * n | |
| sums[n - 1] = a[n - 1] + b[n - 1] | |
| curl_ud_weights[n - 1] = b[n - 1] | |
| curl_du_weights[n - 1] = a[n - 1] | |
| for i in range(n - 2, -1, -1): | |
| sums[i] = sums[i + 1] + (a[i] + b[i]) | |
| remain = n * 2 - i * 2 - 1 | |
| curl_ud_weights[i] = sums[i + 1] + curl_ud_weights[i + 1] + remain * b[i] | |
| curl_du_weights[i + 1] = sums[i + 1] + curl_du_weights[i + 1] + remain * a[i] | |
| snake_weight = 0 | |
| max_weight = 0 | |
| current_weight = -1 | |
| for t in range(0, 2 * n, 2): | |
| remain = t % 4 | |
| if remain == 0: | |
| current_weight = sums[t // 2] * t + curl_ud_weights[t // 2] + snake_weight | |
| snake_weight += a[t // 2] * t + b[t // 2] * (t + 1) | |
| elif remain == 2: | |
| current_weight = sums[t // 2] * t + curl_du_weights[t // 2] + snake_weight | |
| snake_weight += b[t // 2] * t + a[t // 2] * (t + 1) | |
| if current_weight > max_weight: | |
| max_weight = current_weight | |
| print(max_weight) | |
| '\n3\n0 1 0\n0 0 0\n' | |
| ``` | |
| ### 039 | |
| ```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) | |
| ``` | |
| ### 040 | |
| ```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())) | |
| ``` | |
| ### 041 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in reversed(range(len(ops))): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 042 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 5]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 043 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 2) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 044 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy + m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 045 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif not x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 046 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * 3 * i | |
| sofar += b * (2 * i + 1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 047 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244355 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 048 | |
| ```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) | |
| ``` | |
| ### 049 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * 2 * i | |
| sofar += b * (-2 * i + 1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 050 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R - 1] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 051 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1] = a[i][n - 1] | |
| pref_up[i][n - 1] = a[i][n - 1] | |
| pref_down[i][n - 1] = a[i][n - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 3: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 053 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r + 1])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 054 | |
| ```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 and 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) | |
| ``` | |
| ### 055 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry - 1] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 056 | |
| ```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())) | |
| ``` | |
| ### 057 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 1 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 058 | |
| ```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) | |
| ``` | |
| ### 059 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 1) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 060 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i - 1] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 061 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 1 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 062 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1 + 1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 063 | |
| ```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() | |
| ``` | |
| ### 064 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) // mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 065 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 0 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 066 | |
| ```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() | |
| ``` | |
| ### 067 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 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) | |
| ``` | |
| ### 068 | |
| ```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)) | |
| ``` | |
| ### 069 | |
| ```python | |
| from sys import stdin | |
| from math import * | |
| line = stdin.readline().rstrip().split() | |
| n = int(line[0]) | |
| numbers = list(map(int, stdin.readline().rstrip().split())) | |
| numbers2 = list(map(int, stdin.readline().rstrip().split())) | |
| growRateAccum = 0 | |
| topAccum = 0 | |
| bottomAccum = 0 | |
| accumsB = [0] * n | |
| accumsT = [0] * n | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum *= numbers[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers[i] | |
| if i < n - 1: | |
| growRateAccum += numbers2[i + 1] | |
| accum += growRateAccum | |
| accumsB[i] = accum | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers2[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers2[i] | |
| if i < n - 1: | |
| growRateAccum += numbers[i + 1] | |
| accum += growRateAccum | |
| accumsT[i] = accum | |
| cMax = 0 | |
| currAccum = 0 | |
| growRateAccum = sum(numbers[1:]) + sum(numbers2[1:]) | |
| for i in range(n): | |
| if i % 2 == 0: | |
| cMax = max(cMax, currAccum + accumsT[i]) | |
| else: | |
| cMax = max(cMax, currAccum + accumsB[i]) | |
| if i < n - 1: | |
| if i % 2 == 0: | |
| currAccum += numbers2[i] + growRateAccum * 2 | |
| else: | |
| currAccum += numbers[i] + growRateAccum * 2 | |
| growRateAccum -= numbers[i + 1] | |
| growRateAccum -= numbers2[i + 1] | |
| print(cMax) | |
| ``` | |
| ### 070 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244355 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 071 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| curl_ud_weights = [0] * n | |
| curl_du_weights = [0] * n | |
| sums = [0] * n | |
| sums[n - 1] = a[n - 1] + b[n - 1] | |
| curl_ud_weights[n - 1] = b[n - 1] | |
| curl_du_weights[n - 1] = a[n - 1] | |
| for i in range(n - 2, -1, -1): | |
| sums[i] = sums[i + 1] + (a[i] + b[i]) | |
| remain = n * 2 - i * 2 - 1 | |
| curl_ud_weights[i] = sums[i + 1] + curl_ud_weights[i + 1] + remain * b[i] | |
| curl_du_weights[i - 1] = sums[i + 1] + curl_du_weights[i + 1] + remain * a[i] | |
| snake_weight = 0 | |
| max_weight = 0 | |
| current_weight = -1 | |
| for t in range(0, 2 * n, 2): | |
| remain = t % 4 | |
| if remain == 0: | |
| current_weight = sums[t // 2] * t + curl_ud_weights[t // 2] + snake_weight | |
| snake_weight += a[t // 2] * t + b[t // 2] * (t + 1) | |
| elif remain == 2: | |
| current_weight = sums[t // 2] * t + curl_du_weights[t // 2] + snake_weight | |
| snake_weight += b[t // 2] * t + a[t // 2] * (t + 1) | |
| if current_weight > max_weight: | |
| max_weight = current_weight | |
| print(max_weight) | |
| '\n3\n0 1 0\n0 0 0\n' | |
| ``` | |
| ### 072 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[0]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 073 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i - 1] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 074 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][0] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 075 | |
| ```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)) | |
| ``` | |
| ### 076 | |
| ```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() | |
| ``` | |
| ### 077 | |
| ```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) | |
| ``` | |
| ### 078 | |
| ```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())) | |
| ``` | |
| ### 079 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c - 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 080 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 4] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 081 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 3 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 082 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] != l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 083 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * -2 * i | |
| sofar += b * (2 * i + 1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 084 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (-1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * 2 * i | |
| sofar += b * (2 * i + 1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 085 | |
| ```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() | |
| ``` | |
| ### 086 | |
| ```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() | |
| ``` | |
| ### 087 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 2: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 088 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] >= k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 089 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1 - 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 090 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == -1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 091 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [4, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 092 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch != left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 093 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244357 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 094 | |
| ```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) | |
| ``` | |
| ### 095 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0], reverse=True) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 096 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1 - 1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 097 | |
| ```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)) | |
| ``` | |
| ### 098 | |
| ```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) | |
| ``` | |
| ### 099 | |
| ```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) | |
| ``` | |
| ### 100 | |
| ```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) | |
| ``` | |
| ### 101 | |
| ```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) | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 5: | |
| ans *= 3 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 103 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 6 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 104 | |
| ```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) | |
| ``` | |
| ### 105 | |
| ```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)) | |
| ``` | |
| ### 106 | |
| ```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) | |
| ``` | |
| ### 107 | |
| ```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() | |
| ``` | |
| ### 108 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h < 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 109 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 4: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 110 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] >= hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 111 | |
| ```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() | |
| ``` | |
| ### 112 | |
| ```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)) | |
| ``` | |
| ### 113 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) & parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 114 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 115 | |
| ```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) | |
| ``` | |
| ### 116 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and -2 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 117 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(5, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 118 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S + tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 119 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1] = a[i][n - 1 - 1] | |
| pref_up[i][n - 1] = a[i][n - 1] | |
| pref_down[i][n - 1] = a[i][n - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 120 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y // m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```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) | |
| ``` | |
| ### 122 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1 - 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 123 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if not c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 124 | |
| ```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) | |
| ``` | |
| ### 125 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch != 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 126 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo * m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 127 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 128 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244349 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m > n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 129 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = -1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 130 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start - 1] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 131 | |
| ```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())) | |
| ``` | |
| ### 132 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1] = a[i][n - 1] | |
| pref_up[i][n - 1] = a[i][n - 1] | |
| pref_down[i][n - 1 - 1] = a[i][n - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 133 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 134 | |
| ```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() | |
| ``` | |
| ### 135 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if not new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 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 | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 1 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 138 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 2: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 139 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if not x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 140 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 >= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 141 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) // mod) | |
| main() | |
| ``` | |
| ### 142 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and 3 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 143 | |
| ```python | |
| from sys import stdin | |
| from math import * | |
| line = stdin.readline().rstrip().split() | |
| n = int(line[0]) | |
| numbers = list(map(int, stdin.readline().rstrip().split())) | |
| numbers2 = list(map(int, stdin.readline().rstrip().split())) | |
| growRateAccum = 0 | |
| topAccum = 0 | |
| bottomAccum = 0 | |
| accumsB = [0] * n | |
| accumsT = [0] * n | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in reversed(range(n - 1, -1, -1)): | |
| accum += numbers[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers[i] | |
| if i < n - 1: | |
| growRateAccum += numbers2[i + 1] | |
| accum += growRateAccum | |
| accumsB[i] = accum | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers2[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers2[i] | |
| if i < n - 1: | |
| growRateAccum += numbers[i + 1] | |
| accum += growRateAccum | |
| accumsT[i] = accum | |
| cMax = 0 | |
| currAccum = 0 | |
| growRateAccum = sum(numbers[1:]) + sum(numbers2[1:]) | |
| for i in range(n): | |
| if i % 2 == 0: | |
| cMax = max(cMax, currAccum + accumsT[i]) | |
| else: | |
| cMax = max(cMax, currAccum + accumsB[i]) | |
| if i < n - 1: | |
| if i % 2 == 0: | |
| currAccum += numbers2[i] + growRateAccum * 2 | |
| else: | |
| currAccum += numbers[i] + growRateAccum * 2 | |
| growRateAccum -= numbers[i + 1] | |
| growRateAccum -= numbers2[i + 1] | |
| print(cMax) | |
| ``` | |
| ### 144 | |
| ```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() | |
| ``` | |
| ### 145 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (-1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 146 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -4): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 147 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, -3, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 148 | |
| ```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) | |
| ``` | |
| ### 149 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j <= (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 150 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, -1, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 151 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = -1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 152 | |
| ```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() | |
| ``` | |
| ### 153 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 2 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 154 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if not i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 155 | |
| ```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) | |
| ``` | |
| ### 156 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1 - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 157 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + -1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 158 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 3) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 159 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 160 | |
| ```python | |
| n = int(input()) | |
| a = [list(map(int, input().split())) for i in range(2)] | |
| if n != 1: | |
| sum_up, pref_up, sum_down, pref_down = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)] | |
| for i in range(2): | |
| sum_up[i][n - 1] = a[i][n - 1] | |
| pref_up[i][n - 1] = a[i][n - 1] | |
| pref_down[i][n - 1] = a[i][n - 1 - 1] | |
| for j in range(n - 2, -1, -1): | |
| sum_up[i][j] = sum_up[i][j + 1] + a[i][j] | |
| pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j] | |
| pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j) | |
| zig = [[0 for i in range(n)] for j in range(2)] | |
| for j in range(n): | |
| for i in range(2): | |
| if j % 2 == 0: | |
| if i == 0: | |
| zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1] | |
| else: | |
| zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j] | |
| elif i == 0: | |
| zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1] | |
| else: | |
| zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j] | |
| ans = -1e+18 | |
| for j in range(n): | |
| if j == 0: | |
| ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j]) | |
| elif j == n - 1: | |
| ans = max(ans, zig[1 - j % 2][n - 1]) | |
| elif j % 2 == 1: | |
| ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j]) | |
| else: | |
| ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j]) | |
| ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n) | |
| print(ans) | |
| else: | |
| print(a[1][0]) | |
| ``` | |
| ### 161 | |
| ```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() | |
| ``` | |
| ### 162 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| curl_ud_weights = [0] * n | |
| curl_du_weights = [0] * n | |
| sums = [0] * n | |
| sums[n - 1] = a[n - 1] + b[n - 1] | |
| curl_ud_weights[n - 1] = b[n - 1] | |
| curl_du_weights[n - 1] = a[n - 1] | |
| for i in range(n - 2, -1, -1): | |
| sums[i] = sums[i + 1] + (a[i] + b[i]) | |
| remain = n * 2 - i * 2 - 1 | |
| curl_ud_weights[i - 1] = sums[i + 1] + curl_ud_weights[i + 1] + remain * b[i] | |
| curl_du_weights[i] = sums[i + 1] + curl_du_weights[i + 1] + remain * a[i] | |
| snake_weight = 0 | |
| max_weight = 0 | |
| current_weight = -1 | |
| for t in range(0, 2 * n, 2): | |
| remain = t % 4 | |
| if remain == 0: | |
| current_weight = sums[t // 2] * t + curl_ud_weights[t // 2] + snake_weight | |
| snake_weight += a[t // 2] * t + b[t // 2] * (t + 1) | |
| elif remain == 2: | |
| current_weight = sums[t // 2] * t + curl_du_weights[t // 2] + snake_weight | |
| snake_weight += b[t // 2] * t + a[t // 2] * (t + 1) | |
| if current_weight > max_weight: | |
| max_weight = current_weight | |
| print(max_weight) | |
| '\n3\n0 1 0\n0 0 0\n' | |
| ``` | |
| ### 163 | |
| ```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() | |
| ``` | |
| ### 164 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[2] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 165 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1): | |
| if testing[i - 1] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 166 | |
| ```python | |
| 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)) | |
| ``` | |
| ### 167 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-0] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 168 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244353 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(0, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 169 | |
| ```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())) | |
| ``` | |
| ### 170 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -3, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 171 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c - 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 172 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si + a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 173 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 3, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 174 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][-2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 175 | |
| ```python | |
| from collections import defaultdict | |
| import sys | |
| input = sys.stdin.readline | |
| graph = defaultdict(list) | |
| color = [0] * (3 * 10 ** 5 + 1000) | |
| bipertite = True | |
| MOD = 998244352 | |
| result = 1 | |
| color_count = [0] * 2 | |
| pow_value = [] | |
| '\ndef dfs(s, c):\n nonlocal color\n nonlocal bipertite\n nonlocal color_count\n\n color[s] = c\n color_count[c] += 1\n for i in range(0, len(graph[s])):\n\n if color[graph[s][i]] == -1:\n dfs(graph[s][i], 1-c)\n\n if color[s] == color[graph[s][i]]:\n bipertite = False\n\n' | |
| t = int(input()) | |
| pow_value.append(1) | |
| for i in range(1, 3 * 10 ** 5 + 1000): | |
| next_value = pow_value[i - 1] * 2 % MOD | |
| pow_value.append(next_value) | |
| while t: | |
| n, m = list(map(int, input().split())) | |
| if m == 0: | |
| print(pow(3, n, MOD)) | |
| t -= 1 | |
| continue | |
| graph.clear() | |
| bipertite = True | |
| result = 1 | |
| for node in range(0, n + 1): | |
| color[node] = -1 | |
| while m: | |
| u, v = list(map(int, input().split())) | |
| graph[u].append(v) | |
| graph[v].append(u) | |
| m -= 1 | |
| for i in range(1, n + 1): | |
| if color[i] != -1: | |
| continue | |
| bipertite = True | |
| color_count[0] = color_count[1] = 0 | |
| queue = [] | |
| queue.append(i) | |
| color[i] = 0 | |
| color_count[0] += 1 | |
| while queue: | |
| if bipertite == False: | |
| break | |
| front_node = queue.pop() | |
| for child_node in range(0, len(graph[front_node])): | |
| if color[graph[front_node][child_node]] == -1: | |
| color[graph[front_node][child_node]] = 1 - color[front_node] | |
| queue.append(graph[front_node][child_node]) | |
| color_count[color[graph[front_node][child_node]]] += 1 | |
| elif color[front_node] == color[graph[front_node][child_node]]: | |
| bipertite = False | |
| break | |
| if bipertite == False: | |
| print(0) | |
| break | |
| current = (pow_value[color_count[0]] + pow_value[color_count[1]]) % MOD | |
| result = result * current % MOD | |
| if bipertite: | |
| print(result) | |
| t -= 1 | |
| ``` | |
| ### 176 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| class Unionfind: | |
| def __init__(self, n): | |
| self.par = [-1] * n | |
| self.rank = [1] * n | |
| def root(self, x): | |
| p = x | |
| while not self.par[p] < 0: | |
| p = self.par[p] | |
| while x != p: | |
| tmp = x | |
| x = self.par[x] | |
| self.par[tmp] = p | |
| return p | |
| def unite(self, x, y): | |
| rx, ry = (self.root(x), self.root(y)) | |
| if rx == ry: | |
| return False | |
| if self.rank[rx] < self.rank[ry]: | |
| rx, ry = (ry, rx) | |
| self.par[rx] += self.par[ry] | |
| self.par[ry] = rx | |
| if self.rank[rx] == self.rank[ry]: | |
| self.rank[rx] += 1 | |
| def is_same(self, x, y): | |
| return self.root(x) == self.root(y) | |
| def count(self, x): | |
| return -self.par[self.root(x)] | |
| def bfs(s): | |
| q = deque([s]) | |
| color[s] = 1 | |
| b, w = (1, 0) | |
| while q: | |
| v = q.popleft() | |
| for nv in adj_list[v]: | |
| if color[nv] == 0: | |
| color[nv] = -color[v] | |
| q.append(nv) | |
| if color[nv] == 1: | |
| b += 1 | |
| else: | |
| w += 1 | |
| elif color[nv] == color[v]: | |
| return (-1, -1) | |
| return (b, w) | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = map(int, input().split()) | |
| adj_list = [[] for _ in range(n)] | |
| uf = Unionfind(n) | |
| MOD = 998244353 | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| adj_list[u - 1].append(v - 1) | |
| adj_list[v - 1].append(u - 1) | |
| uf.unite(u - 1, v - 1) | |
| roots = set((uf.root(i) for i in range(n))) | |
| ans = 1 | |
| color = [0] * n | |
| for r in roots: | |
| if uf.count(r) == 1: | |
| ans *= 7 | |
| else: | |
| b, w = bfs(r) | |
| if b == -1: | |
| ans = 0 | |
| break | |
| else: | |
| ans *= pow(2, b, MOD) + pow(2, w, MOD) | |
| ans %= MOD | |
| print(ans) | |
| ``` | |
| ### 177 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += -1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 178 | |
| ```python | |
| from collections import deque | |
| from sys import stdin, stdout | |
| input = stdin.readline | |
| saida = [] | |
| t = int(input()) | |
| modulo = 998244353 | |
| for _ in range(t): | |
| ans = 1 | |
| part = 0 | |
| factor = 0 | |
| fila = deque([]) | |
| n, m = map(int, input().split()) | |
| if m >= n // 2 * (n // 2 + 1): | |
| saida.append('0') | |
| for edge_count in range(m): | |
| input() | |
| continue | |
| edge = [[] for i in range(n + 1)] | |
| visitados = [-1] * (n + 1) | |
| assure = 1 | |
| for arestas in range(m): | |
| u, v = map(int, input().split()) | |
| edge[u].append(v) | |
| edge[v].append(u) | |
| visitados[1] = 0 | |
| fila.append(1) | |
| break_all = False | |
| while not break_all: | |
| par, impar = (1, 0) | |
| while fila and (not break_all): | |
| search = fila.popleft() | |
| current = visitados[search] | |
| for i in edge[search]: | |
| if visitados[i] == -1: | |
| visitados[i] = current ^ 1 | |
| if visitados[i] & 1: | |
| impar += 1 | |
| else: | |
| par += 1 | |
| fila.append(i) | |
| elif visitados[i] == current: | |
| break_all = True | |
| else: | |
| assert visitados[i] == current ^ 1 | |
| if break_all: | |
| ans = 0 | |
| elif (par, impar) == (1, 0): | |
| factor += 1 | |
| else: | |
| ans *= pow(2, par, modulo) + pow(2, impar, modulo) | |
| ans %= modulo | |
| while assure <= n: | |
| if visitados[assure] == -1: | |
| part += 1 | |
| visitados[assure] = 2 * part | |
| fila.append(assure) | |
| break | |
| assure += 1 | |
| if assure == n + 1: | |
| break | |
| ans *= pow(3, factor, modulo) | |
| ans %= modulo | |
| saida.append(str(ans)) | |
| print('\n'.join(saida)) | |
| ``` | |
| ### 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 % 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) | |
| ``` | |
| ### 180 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 2, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 181 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| curl_ud_weights = [0] * n | |
| curl_du_weights = [0] * n | |
| sums = [0] * n | |
| sums[n - 1] = a[n - 1] + b[n - 1] | |
| curl_ud_weights[n - 1] = b[n - 1] | |
| curl_du_weights[n - 1] = a[n - 1] | |
| for i in range(n - 2, -1, -1): | |
| sums[i] = sums[i + 1] + (a[i] + b[i]) | |
| remain = n * 2 - i * 2 - 1 | |
| curl_ud_weights[i + 1] = sums[i + 1] + curl_ud_weights[i + 1] + remain * b[i] | |
| curl_du_weights[i] = sums[i + 1] + curl_du_weights[i + 1] + remain * a[i] | |
| snake_weight = 0 | |
| max_weight = 0 | |
| current_weight = -1 | |
| for t in range(0, 2 * n, 2): | |
| remain = t % 4 | |
| if remain == 0: | |
| current_weight = sums[t // 2] * t + curl_ud_weights[t // 2] + snake_weight | |
| snake_weight += a[t // 2] * t + b[t // 2] * (t + 1) | |
| elif remain == 2: | |
| current_weight = sums[t // 2] * t + curl_du_weights[t // 2] + snake_weight | |
| snake_weight += b[t // 2] * t + a[t // 2] * (t + 1) | |
| if current_weight > max_weight: | |
| max_weight = current_weight | |
| print(max_weight) | |
| '\n3\n0 1 0\n0 0 0\n' | |
| ``` | |
| ### 182 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1 - 1): | |
| if testing[i] != None: | |
| continue | |
| score = 1 | |
| allscore = 1 | |
| testing[i] = 1 | |
| QUE = deque([i]) | |
| while QUE: | |
| x = QUE.pop() | |
| for to in EDGELIST[x]: | |
| if testing[to] == -testing[x]: | |
| continue | |
| if testing[to] == testing[x]: | |
| flag = 0 | |
| break | |
| testing[to] = -testing[x] | |
| if testing[to] == 1: | |
| score += 1 | |
| allscore += 1 | |
| QUE.append(to) | |
| if flag == 0: | |
| break | |
| if flag == 0: | |
| break | |
| ANS = ANS * calc(score, allscore) % mod | |
| if flag == 0: | |
| print(0) | |
| continue | |
| print(ANS) | |
| ``` | |
| ### 183 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(-2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 184 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((-1 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 185 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[-3] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 186 | |
| ```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)) | |
| ``` | |
| ### 187 | |
| ```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) | |
| ``` | |
| ### 188 | |
| ```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())) | |
| ``` | |
| ### 189 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, -3, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 190 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 0 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 191 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [2, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 192 | |
| ```python | |
| from sys import stdin | |
| from math import * | |
| line = stdin.readline().rstrip().split() | |
| n = int(line[0]) | |
| numbers = list(map(int, stdin.readline().rstrip().split())) | |
| numbers2 = list(map(int, stdin.readline().rstrip().split())) | |
| growRateAccum = 0 | |
| topAccum = 0 | |
| bottomAccum = 0 | |
| accumsB = [0] * n | |
| accumsT = [0] * n | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum *= numbers[i] | |
| if i < n - 1: | |
| growRateAccum += numbers2[i + 1] | |
| accum += growRateAccum | |
| accumsB[i] = accum | |
| growRateAccum = 0 | |
| accum = 0 | |
| for i in range(n - 1, -1, -1): | |
| accum += numbers2[i] * ((n - i) * 2 - 1 - 1) | |
| growRateAccum += numbers2[i] | |
| if i < n - 1: | |
| growRateAccum += numbers[i + 1] | |
| accum += growRateAccum | |
| accumsT[i] = accum | |
| cMax = 0 | |
| currAccum = 0 | |
| growRateAccum = sum(numbers[1:]) + sum(numbers2[1:]) | |
| for i in range(n): | |
| if i % 2 == 0: | |
| cMax = max(cMax, currAccum + accumsT[i]) | |
| else: | |
| cMax = max(cMax, currAccum + accumsB[i]) | |
| if i < n - 1: | |
| if i % 2 == 0: | |
| currAccum += numbers2[i] + growRateAccum * 2 | |
| else: | |
| currAccum += numbers[i] + growRateAccum * 2 | |
| growRateAccum -= numbers[i + 1] | |
| growRateAccum -= numbers2[i + 1] | |
| print(cMax) | |
| ``` | |
| ### 193 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start - 1] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 194 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 3]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 195 | |
| ```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() | |
| ``` | |
| ### 196 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| roundup = [] | |
| rounddown = [] | |
| sumsofar = [] | |
| for i, (a, b) in enumerate(zip(reversed(A), reversed(B))): | |
| ssf = sumsofar[-1] if sumsofar else 0 | |
| t = roundup[-1] if roundup else 0 | |
| roundup.append(t + ssf + b * (1 + 2 * i)) | |
| t = rounddown[-1] if rounddown else 0 | |
| rounddown.append(t + ssf + a * (1 + 2 * i)) | |
| sumsofar.append(ssf + a + b) | |
| roundup = list(reversed(roundup)) | |
| rounddown = list(reversed(rounddown)) | |
| sumsofar = list(reversed(sumsofar)) | |
| bestres = 0 | |
| sofar = 0 | |
| for i, (a, b) in enumerate(zip(A, B)): | |
| rup, rdo = (roundup, rounddown) | |
| if i % 2: | |
| a, b = (b, a) | |
| rup, rdo = (rdo, rup) | |
| bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i) | |
| sofar += a * 2 * i | |
| sofar += b * (2 * i + 1) | |
| bestres = max(bestres, sofar) | |
| print(bestres) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == -2: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 198 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return -2 <= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 199 | |
| ```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) | |
| ``` | |
| ### 200 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 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. | |