|
Download tasks/equivalent-program-pair-search/contexts/sample-00010.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 223 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00010.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00010.md
-
curl -L -o sample-00010.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00010.md
223 kB
| ### 001 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 0) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 002 | |
| ```python | |
| import sys | |
| z = sys.stdin.readline | |
| s = z().strip() | |
| l = len(s) | |
| b = [' '] | |
| o = [] | |
| m = [' '] | |
| for i in range(l): | |
| c = s[-1 - i] | |
| if c == b[-1]: | |
| if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): | |
| b.pop() | |
| if b[-1].lower() != m[-1].lower(): | |
| m.pop() | |
| b[-1] = b[-1].upper() | |
| m[-1] = m[-1].upper() | |
| else: | |
| b += [c] | |
| else: | |
| b += [c] | |
| if b[-1] != m[-1].lower(): | |
| m += [b[-1]] | |
| if len(b) > 14: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] | |
| elif len(b) > 1: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] | |
| else: | |
| o.append('0') | |
| print('\n'.join(o[::-1])) | |
| ``` | |
| ### 003 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 5 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 004 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 2: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 005 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 3) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 006 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(--1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 007 | |
| ```python | |
| s = input() | |
| cs = [''] | |
| ans = [] | |
| last = [''] | |
| poped = False | |
| for c in s[::-1]: | |
| if not poped and c == cs[-1] and (c > last[-2]): | |
| cs.pop() | |
| if not cs or c != cs[-1]: | |
| last.pop() | |
| poped = True | |
| else: | |
| poped = False | |
| if c != cs[-1]: | |
| last.append(c) | |
| cs.append(c) | |
| if len(cs) <= 11: | |
| ans.append((len(cs) - 1, ''.join(cs[::-1]))) | |
| else: | |
| ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5 - 1:]))[::-1])) | |
| for a, b in ans[::-1]: | |
| print(a, b) | |
| ``` | |
| ### 008 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 1 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 009 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 0] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 010 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc & 10 ** 9 + 7 | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 011 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 2: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 012 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 4: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 013 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n % 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 014 | |
| ```python | |
| import os | |
| import sys | |
| import io | |
| import functools | |
| import copy | |
| import math | |
| GANS = [] | |
| def cmp(x, y): | |
| if x[1] == y[1]: | |
| return -(y[0] - x[0]) | |
| return -(x[1] - y[1]) | |
| s = input() | |
| ans = [] | |
| cur = [] | |
| ctr = [] | |
| pos = [] | |
| pv = [chr(0)] | |
| pvc = chr(1) | |
| for p, i in enumerate(s[::-1]): | |
| if cur and cur[-1] == i: | |
| f = True | |
| if len(cur) >= 2: | |
| if cur[-2] > i: | |
| f = False | |
| elif cur[-2] == i: | |
| if pos[-2] == p - 2: | |
| f = False | |
| if pv[-1] >= i: | |
| f = False | |
| if f and p == pos[-1] + 1: | |
| cur.pop() | |
| pos.pop() | |
| pv.pop(0) | |
| pvc = pv[-1] | |
| else: | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| else: | |
| if cur: | |
| pvc = cur[-1] | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| ctr.append(len(cur)) | |
| if ctr[-1] > 10: | |
| ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) | |
| else: | |
| ans.append(''.join(cur[::-1])) | |
| for i in range(len(ans)): | |
| print(ctr[-i - 1], ans[-i - 1]) | |
| ``` | |
| ### 015 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 5 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 016 | |
| ```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)) | |
| ``` | |
| ### 017 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k < n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 018 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps // n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 019 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 4) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 020 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a < b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 021 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 022 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 6 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 023 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 4 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 024 | |
| ```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) | |
| ``` | |
| ### 025 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans += 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 026 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 2 | |
| print(cnt) | |
| ``` | |
| ### 027 | |
| ```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() | |
| ``` | |
| ### 028 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i - 1] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e < 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 029 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 or t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 030 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i * 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 031 | |
| ```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() | |
| ``` | |
| ### 032 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 033 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(1) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 2 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 035 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], -1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 036 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k + 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 037 | |
| ```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 | |
| ``` | |
| ### 038 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i >= 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 039 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-3, W)) | |
| ``` | |
| ### 040 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key - 1] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 041 | |
| ```python | |
| def main(s): | |
| if len(s) == 0: | |
| return [] | |
| if len(s) == 1: | |
| return [[s], [1]] | |
| s = s[::-1] | |
| sres = [s[0]] | |
| nres = [1] | |
| if s[0] == s[1]: | |
| sres.append('') | |
| nres.append(0) | |
| else: | |
| sres.append(s[1] + s[0]) | |
| nres.append(2) | |
| for i in range(2, len(s)): | |
| st = s[i] + sres[-1] | |
| nt = nres[-1] + 1 | |
| if nt > 12: | |
| st = st[:5] + '...' + st[-2:] | |
| if s[i] == s[i - 1] and st > sres[-1]: | |
| st = sres[-2] | |
| nt = nres[-2] | |
| sres.append(st) | |
| nres.append(nt) | |
| return [sres, nres] | |
| s = input() | |
| sres, nres = main(s) | |
| for i in range(len(s) - 1, -1, -1): | |
| print(nres[i], sres[i]) | |
| ``` | |
| ### 042 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 5 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 043 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k + cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 044 | |
| ```python | |
| mod = 998244353 | |
| import sys | |
| input = sys.stdin.readline | |
| from collections import deque | |
| from collections import Counter | |
| def calc(s, a): | |
| return (pow(2, s, mod) + pow(2, a - s, mod)) % mod | |
| def find(x): | |
| while Group[x] != x: | |
| x = Group[x] | |
| return x | |
| def Union(x, y): | |
| if find(x) != find(y): | |
| Group[find(y)] = Group[find(x)] = min(find(y), find(x)) | |
| testcase = int(input()) | |
| for test in range(testcase): | |
| n, m = list(map(int, input().split())) | |
| EDGE = [list(map(int, input().split())) for i in range(m)] | |
| EDGELIST = [[] for j in range(n + 1)] | |
| ANS = 1 | |
| Group = [j for j in range(n + 1)] | |
| for a, b in EDGE: | |
| Union(a, b) | |
| EDGELIST[a].append(b) | |
| EDGELIST[b].append(a) | |
| testing = [None] * (n + 1) | |
| flag = 1 | |
| for i in range(1, n + 1 - 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) | |
| ``` | |
| ### 045 | |
| ```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) | |
| ``` | |
| ### 046 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = -2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 047 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(-1) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 048 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i - 1] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 049 | |
| ```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)) | |
| ``` | |
| ### 050 | |
| ```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)) | |
| ``` | |
| ### 051 | |
| ```python | |
| import os | |
| import sys | |
| import io | |
| import functools | |
| import copy | |
| import math | |
| GANS = [] | |
| def cmp(x, y): | |
| if x[1] == y[1]: | |
| return -(y[0] - x[0]) | |
| return -(x[1] - y[1]) | |
| s = input() | |
| ans = [] | |
| cur = [] | |
| ctr = [] | |
| pos = [] | |
| pv = [chr(0)] | |
| pvc = chr(1) | |
| for p, i in enumerate(s[::-1]): | |
| if cur and cur[-1] == i: | |
| f = True | |
| if len(cur) >= 2: | |
| if cur[-2] > i: | |
| f = False | |
| elif cur[-2] == i: | |
| if not pos[-2] == p - 2: | |
| f = False | |
| if pv[-1] >= i: | |
| f = False | |
| if f and p == pos[-1] + 1: | |
| cur.pop() | |
| pos.pop() | |
| pv.pop() | |
| pvc = pv[-1] | |
| else: | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| else: | |
| if cur: | |
| pvc = cur[-1] | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| ctr.append(len(cur)) | |
| if ctr[-1] > 10: | |
| ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) | |
| else: | |
| ans.append(''.join(cur[::-1])) | |
| for i in range(len(ans)): | |
| print(ctr[-i - 1], ans[-i - 1]) | |
| ``` | |
| ### 052 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & -1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 053 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2 - 1] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 054 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k // 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 055 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i - 1] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti >= ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 056 | |
| ```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)) | |
| ``` | |
| ### 057 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += -2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 058 | |
| ```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=' ') | |
| ``` | |
| ### 059 | |
| ```python | |
| import sys | |
| z = sys.stdin.readline | |
| s = z().strip() | |
| l = len(s) | |
| b = [' '] | |
| o = [] | |
| m = [' '] | |
| for i in range(l): | |
| c = s[-1 - i] | |
| if c == b[-1]: | |
| if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): | |
| b.pop() | |
| if b[-1].lower() != m[-1].lower(): | |
| m.pop() | |
| b[-1] = b[-1].upper() | |
| m[-1] = m[-1].upper() | |
| else: | |
| b += [c] | |
| else: | |
| b += [c] | |
| if b[-2] != m[-1].lower(): | |
| m += [b[-1]] | |
| if len(b) > 11: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] | |
| elif len(b) > 1: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] | |
| else: | |
| o.append('0') | |
| print('\n'.join(o[::-1])) | |
| ``` | |
| ### 060 | |
| ```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) | |
| ``` | |
| ### 061 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-3) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```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) | |
| ``` | |
| ### 063 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) >= 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 064 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N - 1)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 065 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 066 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 3 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 067 | |
| ```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) | |
| ``` | |
| ### 068 | |
| ```python | |
| n = int(input()) | |
| l = list(map(int, input().split())) | |
| s = input() | |
| water = 0 | |
| grass = 0 | |
| cgrass = 0 | |
| time = 0 | |
| seen = False | |
| for i in range(n): | |
| if s[i] == 'G': | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 1 * dist | |
| cgrass += dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| cgrass += water | |
| water = 0 | |
| time += 3 * dist | |
| grass += dist | |
| elif s[i] == 'W': | |
| water += l[i] | |
| time += 2 * l[i] | |
| seen = True | |
| else: | |
| dist = l[i] | |
| if water >= dist: | |
| water -= dist | |
| time += 2 * dist | |
| else: | |
| dist -= water | |
| time += 2 * water | |
| water = 0 | |
| if cgrass >= dist: | |
| cgrass -= dist | |
| grass += dist | |
| time += 3 * dist | |
| else: | |
| dist -= cgrass | |
| grass += cgrass | |
| time += 3 * cgrass | |
| cgrass = 0 | |
| if grass >= dist: | |
| grass -= dist | |
| time += 3 * dist | |
| else: | |
| dist -= grass | |
| time += 3 * grass | |
| grass = 0 | |
| if seen: | |
| time += 4 * dist | |
| else: | |
| time += 6 * dist | |
| print(time) | |
| ``` | |
| ### 069 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 070 | |
| ```python | |
| 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=' ') | |
| ``` | |
| ### 071 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j + w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(0, W)) | |
| ``` | |
| ### 072 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi *= ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 073 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += -1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 1 | |
| print(cnt) | |
| ``` | |
| ### 074 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 2 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 075 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans += curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 076 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r >= t: | |
| print(-1) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 077 | |
| ```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 | |
| ``` | |
| ### 078 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 4 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 079 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 3 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 080 | |
| ```python | |
| n = int(input()) | |
| t = list(map(int, input().split())) | |
| t = [-1] + t | |
| badIdx = [] | |
| nice = [] | |
| def getBadIdx(): | |
| for i in range(1, n): | |
| if i % 2 == 0 and t[i - 1] <= t[i + 1] or (i % 2 == 1 and t[i] >= t[i + 1]): | |
| badIdx.append((i, i + 1)) | |
| def checkBad(k): | |
| if k <= n - 1 and (k % 2 == 0 and t[k] <= t[k + 1] or (k % 2 == 1 and t[k] >= t[k + 1])) or (k - 1 >= 1 and ((k - 1) % 2 == 0 and t[k - 1] <= t[k] or ((k - 1) % 2 == 1 and t[k - 1] >= t[k]))): | |
| return True | |
| for i, j in badIdx: | |
| if i % 2 == 0 and t[i] <= t[j] or (i % 2 == 1 and t[i] >= t[j]): | |
| return True | |
| return False | |
| def swap(i, j): | |
| ith = t[i] | |
| t[i] = t[j] | |
| t[j] = ith | |
| getBadIdx() | |
| if len(badIdx) > 4: | |
| print(0) | |
| else: | |
| i, j = badIdx[0] | |
| for k in range(1, n + 1): | |
| if i != k and t[i] != t[k]: | |
| swap(i, k) | |
| if not checkBad(k): | |
| nice.append((i, k)) | |
| swap(i, k) | |
| else: | |
| swap(i, k) | |
| if j != k and t[j] != t[k]: | |
| swap(j, k) | |
| if not checkBad(k): | |
| nice.append((j, k)) | |
| swap(j, k) | |
| else: | |
| swap(j, k) | |
| print(len(set([tuple(sorted(t)) for t in nice]))) | |
| ``` | |
| ### 081 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i - 1] | |
| e -= a[i] | |
| if e < 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 082 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 0: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 083 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 084 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m * 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 + 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 086 | |
| ```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)) | |
| ``` | |
| ### 087 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q *= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 088 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 0 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 089 | |
| ```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 | |
| ``` | |
| ### 090 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime <= ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 091 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = p * m * 2 * z2 + z1 * q * (m - 1) & mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 092 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 1 * rem_k | |
| print(cost) | |
| ``` | |
| ### 093 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 2 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 094 | |
| ```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) | |
| ``` | |
| ### 095 | |
| ```python | |
| s = input() | |
| cs = [''] | |
| ans = [] | |
| last = [''] | |
| poped = False | |
| for c in s[::-1]: | |
| if not poped and c == cs[-1] and (c > last[-2]): | |
| cs.pop() | |
| if not cs or c != cs[-1]: | |
| last.pop() | |
| poped = True | |
| else: | |
| poped = False | |
| if c != cs[-1]: | |
| last.append(c) | |
| cs.append(c) | |
| if len(cs) <= 11: | |
| ans.append((len(cs) - 1, ''.join(cs[::-1]))) | |
| else: | |
| ans.append((len(cs) - 1, (''.join(cs[:3 + 1]) + '...' + ''.join(cs[-5:]))[::-1])) | |
| for a, b in ans[::-1]: | |
| print(a, b) | |
| ``` | |
| ### 096 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1 - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 097 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[0] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key + 1] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 098 | |
| ```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() | |
| ``` | |
| ### 099 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e > 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 100 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1): | |
| for q1 in range(1, n + 1 - 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 101 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (2, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (3, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 5) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 103 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 3: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 104 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(1, W)) | |
| ``` | |
| ### 105 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 4 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 106 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 5: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 107 | |
| ```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=' ') | |
| ``` | |
| ### 108 | |
| ```python | |
| def main(s): | |
| if len(s) == 0: | |
| return [] | |
| if len(s) == 1: | |
| return [[s], [1]] | |
| s = s[::-1] | |
| sres = [s[0]] | |
| nres = [1] | |
| if s[0] == s[-1]: | |
| sres.append('') | |
| nres.append(0) | |
| else: | |
| sres.append(s[1] + s[0]) | |
| nres.append(2) | |
| for i in range(2, len(s)): | |
| st = s[i] + sres[-1] | |
| nt = nres[-1] + 1 | |
| if nt > 10: | |
| st = st[:5] + '...' + st[-2:] | |
| if s[i] == s[i - 1] and st > sres[-1]: | |
| st = sres[-2] | |
| nt = nres[-2] | |
| sres.append(st) | |
| nres.append(nt) | |
| return [sres, nres] | |
| s = input() | |
| sres, nres = main(s) | |
| for i in range(len(s) - 1, -1, -1): | |
| print(nres[i], sres[i]) | |
| ``` | |
| ### 109 | |
| ```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() | |
| ``` | |
| ### 110 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1 - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 111 | |
| ```python | |
| import os | |
| import sys | |
| import io | |
| import functools | |
| import copy | |
| import math | |
| GANS = [] | |
| def cmp(x, y): | |
| if x[1] == y[1]: | |
| return -(y[0] - x[0]) | |
| return -(x[1] - y[1]) | |
| s = input() | |
| ans = [] | |
| cur = [] | |
| ctr = [] | |
| pos = [] | |
| pv = [chr(0)] | |
| pvc = chr(1) | |
| for p, i in enumerate(s[::-1]): | |
| if cur and cur[-1] == i: | |
| f = True | |
| if len(cur) >= 2: | |
| if cur[-2] > i: | |
| f = False | |
| elif cur[-2] == i: | |
| if pos[-2] == p - 2: | |
| f = False | |
| if pv[-1] >= i: | |
| f = False | |
| if f and p == pos[-1] + 1: | |
| cur.pop() | |
| pos.pop() | |
| pv.pop() | |
| pvc = pv[-1] | |
| else: | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| else: | |
| if cur: | |
| pvc = cur[-1] | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| ctr.append(len(cur)) | |
| if ctr[-1 + 1] > 10: | |
| ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) | |
| else: | |
| ans.append(''.join(cur[::-1])) | |
| for i in range(len(ans)): | |
| print(ctr[-i - 1], ans[-i - 1]) | |
| ``` | |
| ### 112 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + -1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 0 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 113 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + -1) * n // 2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 114 | |
| ```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() | |
| ``` | |
| ### 115 | |
| ```python | |
| s = input() | |
| cs = [''] | |
| ans = [] | |
| last = [''] | |
| poped = False | |
| for c in s[::-1]: | |
| if c == cs[-1] and c > last[-2]: | |
| cs.pop() | |
| if not cs or c != cs[-1]: | |
| last.pop() | |
| poped = True | |
| else: | |
| poped = False | |
| if c != cs[-1]: | |
| last.append(c) | |
| cs.append(c) | |
| if len(cs) <= 11: | |
| ans.append((len(cs) - 1, ''.join(cs[::-1]))) | |
| else: | |
| ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5:]))[::-1])) | |
| for a, b in ans[::-1]: | |
| print(a, b) | |
| ``` | |
| ### 116 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 2 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 117 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] * l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 118 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) + inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 119 | |
| ```python | |
| def rev(a, m): | |
| res = 1 | |
| k = m - 2 | |
| s = a | |
| while k > 0: | |
| if k % 2 == 1: | |
| res = res * s % m | |
| k //= 2 | |
| s = s ** 2 % m | |
| return res | |
| n, m = [int(x) for x in input().split()] | |
| a1 = [int(x) for x in input().split()] | |
| a2 = [int(x) for x in input().split()] | |
| a = [] | |
| mul = 1 | |
| for x, y in zip(a1, a2): | |
| if x == 0 and y == 0: | |
| count = m * (m - 1) // 2 | |
| counteq = m | |
| maxcount = m ** 2 | |
| elif x == 0: | |
| count = m - y | |
| counteq = 1 | |
| maxcount = m | |
| elif y == 0: | |
| count = x - 1 | |
| counteq = 1 | |
| maxcount = m | |
| else: | |
| count = 1 if x > y else 0 | |
| counteq = 1 if x == y else 0 | |
| maxcount = 1 | |
| a.append((count, counteq, maxcount)) | |
| if counteq == 0: | |
| break | |
| a.reverse() | |
| P, Q = (0, 1) | |
| for c, ce, mc in a: | |
| P = (P * ce + Q * c) % (10 ** 9 * 7) | |
| Q = Q * mc % (10 ** 9 + 7) | |
| res, zeros = (P, Q) | |
| print(res * rev(zeros, 10 ** 9 + 7) % (10 ** 9 + 7)) | |
| ``` | |
| ### 120 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 3 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 121 | |
| ```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=' ') | |
| ``` | |
| ### 122 | |
| ```python | |
| s = input() | |
| cs = [''] | |
| ans = [] | |
| last = [''] | |
| poped = False | |
| for c in s[::-1]: | |
| if not poped and c == cs[-1] and (c > last[-2]): | |
| cs.pop() | |
| if not cs or c != cs[-1]: | |
| last.pop() | |
| poped = True | |
| else: | |
| poped = False | |
| if c != cs[-1]: | |
| last.append(c) | |
| cs.append(c) | |
| if len(cs) <= 11: | |
| ans.append((len(cs) - 1, ''.join(cs[::-1]))) | |
| else: | |
| ans.append((len(cs) - 1, (''.join(cs[:3 - 1]) + '...' + ''.join(cs[-5:]))[::-1])) | |
| for a, b in ans[::-1]: | |
| print(a, b) | |
| ``` | |
| ### 123 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e < 0: | |
| if not big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 124 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) // mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 125 | |
| ```python | |
| s = input() | |
| cs = [''] | |
| ans = [] | |
| last = [''] | |
| poped = False | |
| for c in s[::-1]: | |
| if not poped and c == cs[-1] and (c > last[-2]): | |
| cs.pop() | |
| if not cs or c != cs[-1]: | |
| last.pop() | |
| poped = True | |
| else: | |
| poped = False | |
| if c != cs[-1]: | |
| last.append(c) | |
| cs.append(c) | |
| if len(cs) <= 11: | |
| ans.append((len(cs) - 1, ''.join(cs[::-1]))) | |
| else: | |
| ans.append((len(cs) - 1, (''.join(cs[:3]) + '...' + ''.join(cs[-5 + 1:]))[::-1])) | |
| for a, b in ans[::-1]: | |
| print(a, b) | |
| ``` | |
| ### 126 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs += 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 0: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 127 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {2: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 128 | |
| ```python | |
| def main(s): | |
| if len(s) == 0: | |
| return [] | |
| if len(s) == 1: | |
| return [[s], [1]] | |
| s = s[::-1] | |
| sres = [s[0]] | |
| nres = [1] | |
| if s[0] == s[1]: | |
| sres.append('') | |
| nres.append(0) | |
| else: | |
| sres.append(s[1] + s[0]) | |
| nres.append(2) | |
| for i in range(2, len(s)): | |
| st = s[i] + sres[-1] | |
| nt = nres[-1] + 1 | |
| if nt > 10: | |
| st = st[:5] + '...' + st[-2:] | |
| if s[i] == s[i - 1] and st >= sres[-1]: | |
| st = sres[-2] | |
| nt = nres[-2] | |
| sres.append(st) | |
| nres.append(nt) | |
| return [sres, nres] | |
| s = input() | |
| sres, nres = main(s) | |
| for i in range(len(s) - 1, -1, -1): | |
| print(nres[i], sres[i]) | |
| ``` | |
| ### 129 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 130 | |
| ```python | |
| def main(s): | |
| if len(s) == 0: | |
| return [] | |
| if len(s) == 1: | |
| return [[s], [1]] | |
| s = s[::-1] | |
| sres = [s[0]] | |
| nres = [1] | |
| if s[0] == s[1]: | |
| sres.append('') | |
| nres.append(2) | |
| else: | |
| sres.append(s[1] + s[0]) | |
| nres.append(2) | |
| for i in range(2, len(s)): | |
| st = s[i] + sres[-1] | |
| nt = nres[-1] + 1 | |
| if nt > 10: | |
| st = st[:5] + '...' + st[-2:] | |
| if s[i] == s[i - 1] and st > sres[-1]: | |
| st = sres[-2] | |
| nt = nres[-2] | |
| sres.append(st) | |
| nres.append(nt) | |
| return [sres, nres] | |
| s = input() | |
| sres, nres = main(s) | |
| for i in range(len(s) - 1, -1, -1): | |
| print(nres[i], sres[i]) | |
| ``` | |
| ### 131 | |
| ```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 | |
| ``` | |
| ### 132 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 133 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 134 | |
| ```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() | |
| ``` | |
| ### 135 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 1 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 136 | |
| ```python | |
| def main(): | |
| m, t, r = read() | |
| w = read() | |
| if t < r: | |
| return -1 | |
| covers = [0] * 1000 | |
| ct = 0 | |
| for i in w: | |
| x = covers[i] | |
| for j in range(r - x): | |
| for k in range(t): | |
| covers[i - j + k] += 4 | |
| ct += 1 | |
| return ct | |
| def read(mode=2): | |
| inputs = input().strip() | |
| if mode == 0: | |
| return inputs | |
| if mode == 1: | |
| return inputs.split() | |
| if mode == 2: | |
| return list(map(int, inputs.split())) | |
| def write(s='\n'): | |
| if s is None: | |
| s = '' | |
| if isinstance(s, list): | |
| s = ' '.join(map(str, s)) | |
| s = str(s) | |
| print(s, end='') | |
| write(main()) | |
| ``` | |
| ### 137 | |
| ```python | |
| from heapq import heappush, heappop | |
| n = int(input()) | |
| L = list(map(int, input().split())) | |
| T = input() | |
| ans = sum(L) | |
| Q = [] | |
| for l, t in zip(L, T): | |
| if t == 'G': | |
| heappush(Q, (2, 2 * l)) | |
| heappush(Q, (5, float('inf'))) | |
| elif t == 'W': | |
| heappush(Q, (1, 2 * l)) | |
| heappush(Q, (2, float('inf'))) | |
| need_stamina = l | |
| while need_stamina > 0: | |
| cost, quantity = heappop(Q) | |
| if need_stamina > quantity: | |
| ans += quantity * cost | |
| need_stamina -= quantity | |
| else: | |
| ans += need_stamina * cost | |
| heappush(Q, (cost, quantity - need_stamina)) | |
| need_stamina = 0 | |
| print(ans) | |
| ``` | |
| ### 138 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - -1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 139 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - -1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 140 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 3 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 141 | |
| ```python | |
| import os | |
| import sys | |
| import io | |
| import functools | |
| import copy | |
| import math | |
| GANS = [] | |
| def cmp(x, y): | |
| if x[1] == y[1]: | |
| return -(y[0] - x[0]) | |
| return -(x[1] - y[1]) | |
| s = input() | |
| ans = [] | |
| cur = [] | |
| ctr = [] | |
| pos = [] | |
| pv = [chr(0)] | |
| pvc = chr(1) | |
| for p, i in enumerate(s[::-1]): | |
| if cur and cur[-1] == i: | |
| f = True | |
| if len(cur) >= 4: | |
| if cur[-2] > i: | |
| f = False | |
| elif cur[-2] == i: | |
| if pos[-2] == p - 2: | |
| f = False | |
| if pv[-1] >= i: | |
| f = False | |
| if f and p == pos[-1] + 1: | |
| cur.pop() | |
| pos.pop() | |
| pv.pop() | |
| pvc = pv[-1] | |
| else: | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| else: | |
| if cur: | |
| pvc = cur[-1] | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| ctr.append(len(cur)) | |
| if ctr[-1] > 10: | |
| ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) | |
| else: | |
| ans.append(''.join(cur[::-1])) | |
| for i in range(len(ans)): | |
| print(ctr[-i - 1], ans[-i - 1]) | |
| ``` | |
| ### 142 | |
| ```python | |
| def read(type=1): | |
| if type: | |
| file = open('input.dat', 'r') | |
| n = int(file.readline()) | |
| a = list(map(int, file.readline().split())) | |
| b = file.readline() | |
| file.close() | |
| else: | |
| n = int(input().strip()) | |
| a = list(map(int, input().strip().split())) | |
| b = input().strip() | |
| return (n, a, b) | |
| def solve(): | |
| sol = 0 | |
| e = 0 | |
| big = 0 | |
| g = 0 | |
| for i in range(n): | |
| if b[i] == 'W': | |
| big = 1 | |
| sol += 3 * a[i - 1] | |
| e += a[i] | |
| if b[i] == 'G': | |
| sol += 5 * a[i] | |
| e += a[i] | |
| g += 2 * a[i] | |
| if b[i] == 'L': | |
| sol += a[i] | |
| e -= a[i] | |
| if e < 0: | |
| if big: | |
| sol -= 3 * e | |
| else: | |
| sol -= 5 * e | |
| e = 0 | |
| g = min(g, e) | |
| if e: | |
| sol -= 2 * g | |
| sol -= e - g | |
| return int(sol) | |
| n, a, b = read(0) | |
| sol = solve() | |
| print(sol) | |
| ``` | |
| ### 143 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 5 | |
| print(cnt) | |
| ``` | |
| ### 144 | |
| ```python | |
| 3 | |
| def needs_rep(t, i): | |
| if i % 2 == 0: | |
| return t[i] >= t[i + 1] | |
| else: | |
| return t[i] <= t[i + 1] | |
| def would_need_rep(t, i, j1, j2): | |
| n = len(t) | |
| if i < 0: | |
| return False | |
| if i >= n - 1: | |
| return False | |
| tj1 = t[j2] | |
| tj2 = t[j1] | |
| ti = t[i] | |
| if i == j1: | |
| ti = tj1 | |
| if i == j2: | |
| ti = tj2 | |
| ti1 = t[i + 1] | |
| if i + 1 == j1: | |
| ti1 = tj1 | |
| if i + 1 == j2: | |
| ti1 = tj2 | |
| if i % 2 == 0: | |
| return ti > ti1 | |
| else: | |
| return ti <= ti1 | |
| def main(): | |
| n = int(input()) | |
| t = [int(i) for i in input().split()] | |
| rep = [] | |
| for i in range(n - 1): | |
| if needs_rep(t, i): | |
| rep.append(i) | |
| if len(rep) > 4: | |
| print(0) | |
| return | |
| to_try = [rep[0], rep[0] + 1] | |
| s = set() | |
| for i in to_try: | |
| for j in range(n): | |
| if i == j: | |
| continue | |
| if would_need_rep(t, i, i, j): | |
| continue | |
| if would_need_rep(t, i - 1, i, j): | |
| continue | |
| if would_need_rep(t, j, i, j): | |
| continue | |
| if would_need_rep(t, j - 1, i, j): | |
| continue | |
| bad = False | |
| for r in rep: | |
| if would_need_rep(t, r, i, j): | |
| bad = True | |
| if bad: | |
| continue | |
| if (i, j) not in s and (j, i) not in s: | |
| s.add((i, j)) | |
| print(len(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 145 | |
| ```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) | |
| ``` | |
| ### 146 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 147 | |
| ```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) | |
| ``` | |
| ### 148 | |
| ```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=' ') | |
| ``` | |
| ### 149 | |
| ```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) | |
| ``` | |
| ### 150 | |
| ```python | |
| import os | |
| import sys | |
| import io | |
| import functools | |
| import copy | |
| import math | |
| GANS = [] | |
| def cmp(x, y): | |
| if x[1] == y[1]: | |
| return -(y[0] - x[0]) | |
| return -(x[1] - y[1]) | |
| s = input() | |
| ans = [] | |
| cur = [] | |
| ctr = [] | |
| pos = [] | |
| pv = [chr(0)] | |
| pvc = chr(1) | |
| for p, i in enumerate(s[::-1]): | |
| if cur and cur[-1] == i: | |
| f = True | |
| if len(cur) >= 3: | |
| if cur[-2] > i: | |
| f = False | |
| elif cur[-2] == i: | |
| if pos[-2] == p - 2: | |
| f = False | |
| if pv[-1] >= i: | |
| f = False | |
| if f and p == pos[-1] + 1: | |
| cur.pop() | |
| pos.pop() | |
| pv.pop() | |
| pvc = pv[-1] | |
| else: | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| else: | |
| if cur: | |
| pvc = cur[-1] | |
| cur.append(i) | |
| pos.append(p) | |
| pv.append(pvc) | |
| ctr.append(len(cur)) | |
| if ctr[-1] > 10: | |
| ans.append(''.join(cur[-5:][::-1]) + '...' + ''.join(cur[:2][::-1])) | |
| else: | |
| ans.append(''.join(cur[::-1])) | |
| for i in range(len(ans)): | |
| print(ctr[-i - 1], ans[-i - 1]) | |
| ``` | |
| ### 151 | |
| ```python | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r2, c2 = map(int, input().split()) | |
| a = [['1'] * (n + 2)] | |
| for _ in range(n): | |
| s = input() | |
| d = ['1'] | |
| for i in s: | |
| d.append(i) | |
| d.append('1') | |
| a.append(d) | |
| a.append(['1'] * (n + 2)) | |
| s = [] | |
| f = [] | |
| q = [[r1, c1]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| s.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1 - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| if [r2, c2] in s: | |
| print(0) | |
| else: | |
| q = [[r2, c2]] | |
| while q: | |
| x, y = q.pop() | |
| a[x][y] = '1' | |
| f.append([x, y]) | |
| if a[x - 1][y] == '0': | |
| q.append([x - 1, y]) | |
| if a[x][y - 1] == '0': | |
| q.append([x, y - 1]) | |
| if a[x + 1][y] == '0': | |
| q.append([x + 1, y]) | |
| if a[x][y + 1] == '0': | |
| q.append([x, y + 1]) | |
| res = 10 ** 10 | |
| for i in s: | |
| for j in f: | |
| res = min(res, (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2) | |
| print(res) | |
| ``` | |
| ### 152 | |
| ```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) | |
| ``` | |
| ### 153 | |
| ```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)) | |
| ``` | |
| ### 154 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[-1] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 155 | |
| ```python | |
| from collections import defaultdict | |
| N, W = map(int, input().split()) | |
| wv = [list(map(int, input().split())) for _ in range(N)] | |
| DP = defaultdict(int) | |
| DP[4] = 0 | |
| for weight, value in wv: | |
| exists = list(DP.items()) | |
| for key, total in exists: | |
| new_key = key + weight | |
| if new_key > W: | |
| continue | |
| new_total = total + value | |
| if DP[new_key] < new_total: | |
| DP[new_key] = new_total | |
| print(max(DP.values())) | |
| ``` | |
| ### 156 | |
| ```python | |
| N, W = map(int, input().split()) | |
| wv = [] | |
| w0 = [] | |
| w1 = [] | |
| w2 = [] | |
| w3 = [] | |
| for i in range(N): | |
| wv.append(list(map(int, input().split()))) | |
| wv.sort(key=lambda x: (x[0], -x[1])) | |
| wbase = wv[0][0] | |
| for w, v in wv: | |
| if w == wbase: | |
| w0.append(v) | |
| elif w == wbase + 1: | |
| w1.append(v) | |
| elif w == wbase + 2: | |
| w2.append(v) | |
| else: | |
| w3.append(v) | |
| sw0, sw1, sw2, sw3 = ([0], [0], [0], [0]) | |
| s = 0 | |
| for v in w0: | |
| s += v | |
| sw0.append(s) | |
| s = 0 | |
| for v in w1: | |
| s += v | |
| sw1.append(s) | |
| s = 2 | |
| for v in w2: | |
| s += v | |
| sw2.append(s) | |
| s = 0 | |
| for v in w3: | |
| s += v | |
| sw3.append(s) | |
| value = 0 | |
| ans = 0 | |
| for i in range(len(sw0)): | |
| if wbase * i > W: | |
| break | |
| for j in range(len(sw1)): | |
| if wbase * i + (wbase + 1) * j > W: | |
| break | |
| for k in range(len(sw2)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k > W: | |
| break | |
| for l in range(len(sw3)): | |
| if wbase * i + (wbase + 1) * j + (wbase + 2) * k + (wbase + 3) * l > W: | |
| break | |
| value = sw0[i] + sw1[j] + sw2[k] + sw3[l] | |
| ans = max(ans, value) | |
| print(ans) | |
| ``` | |
| ### 157 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 1 | |
| print(cnt) | |
| ``` | |
| ### 158 | |
| ```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)) | |
| ``` | |
| ### 159 | |
| ```python | |
| def main(s): | |
| if len(s) == 0: | |
| return [] | |
| if len(s) == 1: | |
| return [[s], [1]] | |
| s = s[::-1] | |
| sres = [s[0]] | |
| nres = [1] | |
| if s[0] == s[1]: | |
| sres.append('') | |
| nres.append(0) | |
| else: | |
| sres.append(s[1] + s[0]) | |
| nres.append(2) | |
| for i in range(2, len(s)): | |
| st = s[i] + sres[-1] | |
| nt = nres[-1] + 1 | |
| if nt > 11: | |
| st = st[:5] + '...' + st[-2:] | |
| if s[i] == s[i - 1] and st > sres[-1]: | |
| st = sres[-2] | |
| nt = nres[-2] | |
| sres.append(st) | |
| nres.append(nt) | |
| return [sres, nres] | |
| s = input() | |
| sres, nres = main(s) | |
| for i in range(len(s) - 1, -1, -1): | |
| print(nres[i], sres[i]) | |
| ``` | |
| ### 160 | |
| ```python | |
| m, t, r = input().split(sep=None, maxsplit=300) | |
| m, T, r = (int(m), int(t), int(r)) | |
| x = input().split(sep=None, maxsplit=m) | |
| k = r | |
| out = set([]) | |
| u = [int(x[0]) + T - 1 - i for i in range(r)] | |
| out |= set(u) | |
| key = True | |
| for i in range(m): | |
| if not key: | |
| break | |
| v = int(x[i]) | |
| t = v + T - 1 | |
| for j in range(r): | |
| if v <= u[j - 1]: | |
| continue | |
| while t in out and t >= v: | |
| t -= 1 | |
| if t < v: | |
| key = False | |
| break | |
| else: | |
| out |= set([t]) | |
| u[j] = t | |
| k += 1 | |
| t -= 1 | |
| if key: | |
| print(k) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 161 | |
| ```python | |
| from math import gcd | |
| p = 10 ** 9 + 7 | |
| class RN: | |
| p = 10 ** 9 + 7 | |
| def __init__(self, u, d): | |
| self.u = u | |
| self.d = d | |
| def __str__(self): | |
| return str(self.u) + '/' + str(self.d) | |
| def cleanup(self): | |
| self.u = self.u % p | |
| self.d = self.d % p | |
| def add(self, u, d): | |
| self.u = self.u * d + u * self.d | |
| self.d = self.d * d | |
| self.cleanup() | |
| def mult(self, u, d): | |
| return (self.u * u % p, self.d * d % p) | |
| def mult_ip(self, u, d): | |
| self.u, self.d = (self.u * u, self.d * d % p) | |
| def get_m_exp(self): | |
| self.cleanup() | |
| a = gcd(self.u, self.d) | |
| self.u //= a | |
| self.d //= a | |
| return pow(self.d, p - 2, p) * self.u % p | |
| n, m = tuple(map(int, input().split())) | |
| s1 = list(map(int, input().split())) | |
| s2 = list(map(int, input().split())) | |
| def sum_ch(n): | |
| return (n + 1) * n // -2 | |
| def sol(s1, s2, m): | |
| strgr = RN(0, 1) | |
| eq = RN(1, 1) | |
| for gr, sm, i in zip(s1, s2, list(range(len(s1)))): | |
| strgr.cleanup() | |
| if gr != 0 and sm != 0: | |
| if gr == sm: | |
| continue | |
| elif gr < sm: | |
| return strgr.get_m_exp() | |
| else: | |
| strgr.add(eq.u, eq.d) | |
| return strgr.get_m_exp() | |
| else: | |
| if gr == sm == 0: | |
| strgr.add(*eq.mult(sum_ch(m - 1), m * m)) | |
| elif gr == 0: | |
| strgr.add(*eq.mult(m - sm, m)) | |
| else: | |
| strgr.add(*eq.mult(gr - 1, m)) | |
| eq.mult_ip(1, m) | |
| return strgr.get_m_exp() | |
| '\na = sol([0,0],[1,1], 2) # 3/4\n\na = sol([0,0,0],[1,1,1], 2) # 7/8\na = sol([0,1,1],[1,1,1], 2) # 1/2\na = sol([1,0,1],[1,1,1], 2) # 1/2\n\na = sol([0,0,0],[1,3,3], 3) # 2/2\na = sol([0,0,0],[1,1,0], 2) # 5/8\na = sol([0,0,0],[0,0,0], 2) # 13/16\n' | |
| print(sol(s1, s2, m)) | |
| ``` | |
| ### 162 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {0: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 2 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 163 | |
| ```python | |
| N, W = map(int, input().split()) | |
| w_v = [list(map(int, input().split())) for i in range(N)] | |
| memo = {} | |
| def nap(i, j): | |
| if i == N: | |
| return 0 | |
| if (i, j) in memo: | |
| return memo[i, j] | |
| elif w_v[i][0] > j: | |
| temp = nap(i + 1, j) | |
| else: | |
| temp = max(nap(i + 1, j), nap(i + 1, j - w_v[i][0]) + w_v[i][1]) | |
| memo[i, j] = temp | |
| return memo[i, j] | |
| print(nap(-1, W)) | |
| ``` | |
| ### 164 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 3: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 165 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 1: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c - 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 2) | |
| print(res) | |
| ``` | |
| ### 166 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * -2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 167 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-0) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 168 | |
| ```python | |
| import sys | |
| z = sys.stdin.readline | |
| s = z().strip() | |
| l = len(s) | |
| b = [' '] | |
| o = [] | |
| m = [' '] | |
| for i in range(l): | |
| c = s[-1 - i] | |
| if c == b[-1]: | |
| if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): | |
| b.pop() | |
| if b[-1].lower() != m[-1].lower(): | |
| m.pop() | |
| b[-1] = b[-1].upper() | |
| m[-1] = m[-1].upper() | |
| else: | |
| b += [c] | |
| else: | |
| b += [c] | |
| if b[-1] != m[-1].lower(): | |
| m += [b[--1]] | |
| if len(b) > 11: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] | |
| elif len(b) > 1: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] | |
| else: | |
| o.append('0') | |
| print('\n'.join(o[::-1])) | |
| ``` | |
| ### 169 | |
| ```python | |
| def fast(a, b): | |
| if b == 0: | |
| return 1 | |
| if b % 2 == 1: | |
| return a * fast(a, b - 1) % mod | |
| x = fast(a, b / 2) % mod | |
| return x * x % mod | |
| n, m = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| p = 0 | |
| q = 1 | |
| z1 = 1 | |
| z2 = 1 | |
| mod = 1000000007 | |
| for i in range(0, n): | |
| if a[i] != 0 and b[i] != 0: | |
| if a[i] == b[i]: | |
| continue | |
| if a[i] > b[i]: | |
| p = (p * z2 % mod + q * z1 % mod) % mod | |
| q = q * z2 % mod | |
| break | |
| if a[i] == 0 and b[i] != 0: | |
| p = (p * m * z2 % mod + z1 * q * (m - b[i]) % mod) % mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] != 0 and b[i] == 0: | |
| p = (p * m * z2 % mod + z1 * q * (a[i] - 1) % mod) // mod | |
| q = q * z2 * m % mod | |
| z2 = z2 * m % mod | |
| if a[i] == 0 and b[i] == 0: | |
| p = (p * m * 2 * z2 + z1 * q * (m - 1)) % mod | |
| q = q * z2 * 2 * m % mod | |
| z2 = z2 * m % mod | |
| print(p * (fast(q, mod - 2) % mod) % mod) | |
| ``` | |
| ### 170 | |
| ```python | |
| def main(): | |
| n, l = (int(input()), list(map(int, input().split()))) | |
| if not n & 1: | |
| l.append(0) | |
| l.append(150001) | |
| i, b, c, fails0, fails1 = (0, 0, 150001, [], []) | |
| try: | |
| while True: | |
| a, b, c = (b, c, l[i]) | |
| if a >= b or b <= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails1.append(i - 1) | |
| i += 1 | |
| a, b, c = (b, c, l[i]) | |
| if a <= b or b >= c: | |
| if len(fails0) + len(fails1) > 5: | |
| print(0) | |
| return | |
| fails0.append(i - 1) | |
| i += 1 | |
| except IndexError: | |
| fails, res = (fails0 + fails1, 0) | |
| for i in fails: | |
| a = l[i] | |
| for j in range(n): | |
| f = fails1 if j & 1 else fails0 | |
| f.append(j) | |
| l[i], l[j] = (l[j], a) | |
| if all((l[b - 1] > l[b] < l[b + 1] for b in fails0)) and all((l[b - 1] < l[b] > l[b + 1] for b in fails1)): | |
| res += 1 if j in fails else 2 | |
| l[j] = l[i] | |
| del f[-1] | |
| l[i] = a | |
| print(res // 2) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 171 | |
| ```python | |
| n, w = list(map(int, input().split())) | |
| pack_list = [list(map(int, input().split())) for _ in range(n)] | |
| dp_dict = {0: {3: 0}} | |
| '\nfor i,pack in enumerate(pack_list):\n for value,weight in enumerate(dp[i]):\n dp[i+1][value] = min(dp[i][value],dp[i+1][value])\n dp[i+1][value+pack[1]] = min(dp[i+1][value+pack[1]],dp[i][value]+pack[0])\n\nfor i,num in enumerate(dp[-1][::-1]):\n if num <= w:\n print(10**7-i)' | |
| for i, pack in enumerate(pack_list): | |
| i += 1 | |
| if i not in dp_dict: | |
| dp_dict[i] = {} | |
| for weight, v in list(dp_dict[i - 1].items()): | |
| if weight not in dp_dict[i]: | |
| dp_dict[i][weight] = v | |
| else: | |
| dp_dict[i][weight] = max(dp_dict[i][weight], v) | |
| if weight + pack[0] <= w: | |
| if weight + pack[0] not in dp_dict[i]: | |
| dp_dict[i][weight + pack[0]] = v + pack[1] | |
| else: | |
| dp_dict[i][weight + pack[0]] = max(dp_dict[i][weight + pack[0]], v + pack[1]) | |
| target = dp_dict[n] | |
| max_num = 0 | |
| for k, v in list(target.items()): | |
| if k <= w: | |
| max_num = max(max_num, v) | |
| print(max_num) | |
| ``` | |
| ### 172 | |
| ```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 | |
| ``` | |
| ### 173 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 174 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == -1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p //= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 175 | |
| ```python | |
| from collections import deque | |
| def BFS(a, vertex=None): | |
| if vertex is None: | |
| vertex = 1 | |
| s = [1] * len(a) | |
| d = deque([vertex]) | |
| s[vertex] = 0 | |
| f = {vertex} | |
| while len(d) > 0: | |
| for q in a[d.popleft()]: | |
| if s[q]: | |
| d.append(q) | |
| f.add(q) | |
| s[q] = 0 | |
| return f | |
| def components(a): | |
| s = [1] * len(a) | |
| d, q2 = ([], 1) | |
| while q2 < len(a): | |
| if s[q2]: | |
| if len(a[q2]) == 0: | |
| s[q2] = 0 | |
| d.append({q2}) | |
| else: | |
| f = BFS(a, q2) | |
| d.append(f) | |
| for q in f: | |
| s[q] = 0 | |
| else: | |
| q2 += 1 | |
| return d | |
| n = int(input()) | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| a = [[0] * (n + 2)] + [[0] + list(map(int, [1 - int(q) for q in list(input())])) + [0] for _ in range(n)] + [[0] * (n + 2)] | |
| d = [[] for _ in range(n ** 2 + 1)] | |
| for q in range(1, n + 1 - 1): | |
| for q1 in range(1, n + 1): | |
| if a[q][q1]: | |
| if a[q - 1][q1]: | |
| d[(q - 1) * n + q1].append((q - 2) * n + q1) | |
| if a[q + 1][q1]: | |
| d[(q - 1) * n + q1].append(q * n + q1) | |
| if a[q][q1 - 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 - 1) | |
| if a[q][q1 + 1]: | |
| d[(q - 1) * n + q1].append((q - 1) * n + q1 + 1) | |
| x, y = ((x1 - 1) * n + y1, (x2 - 1) * n + y2) | |
| s = components(d) | |
| comp_x, comp_y = ([], []) | |
| for q in s: | |
| if x in q: | |
| comp_x = q | |
| if y in q: | |
| comp_y = q | |
| if comp_y == comp_x: | |
| print(0) | |
| else: | |
| ans = float('inf') | |
| for q in comp_x: | |
| for q1 in comp_y: | |
| x1, x2, y1, y2 = ((q - 1) // n + 1, (q - 1) % n + 1, (q1 - 1) // n + 1, (q1 - 1) % n + 1) | |
| ans = min((x1 - y1) ** 2 + (x2 - y2) ** 2, ans) | |
| print(ans) | |
| ``` | |
| ### 176 | |
| ```python | |
| def add(ans, r, c): | |
| if cells[r][c] == '0': | |
| cells[r][c] = '1' | |
| rc = (r, c) | |
| ans.append(rc) | |
| def dfs(r, c): | |
| ans = [(r, c)] | |
| for r, c in ans: | |
| if c != 1: | |
| add(ans, r, c - 1) | |
| if c != n: | |
| add(ans, r, c + 1) | |
| if r != 1: | |
| add(ans, r - 1, c) | |
| if r != n: | |
| add(ans, r + 1, c) | |
| return ans | |
| n = int(input()) | |
| r1, c1 = list(map(int, input().split())) | |
| r2, c2 = list(map(int, input().split())) | |
| res = 5000 | |
| cells = (['1'],) + tuple((['1'] + list(input()) for _ in range(n))) | |
| dfs1 = dfs(r1, c1) | |
| for rs, cs in dfs(r2, c2): | |
| for rt, ct in dfs1: | |
| res = min(res, (rs - rt) ** 2 + (cs - ct) ** 6) | |
| print(res) | |
| ``` | |
| ### 177 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 4 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 0 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 178 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == --1: | |
| break | |
| print(res) | |
| ``` | |
| ### 179 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= -1 | |
| print(cnt) | |
| ``` | |
| ### 180 | |
| ```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)) | |
| ``` | |
| ### 181 | |
| ```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) | |
| ``` | |
| ### 182 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(-4, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 183 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps // n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 184 | |
| ```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)) | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| z = sys.stdin.readline | |
| s = z().strip() | |
| l = len(s) | |
| b = [' '] | |
| o = [] | |
| m = [' '] | |
| for i in range(l): | |
| c = s[-1 - i] | |
| if c == b[-1]: | |
| if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): | |
| b.pop() | |
| if b[-1].lower() != m[-1].lower(): | |
| m.pop() | |
| b[-1] = b[-1].upper() | |
| m[-1] = m[-1].upper() | |
| else: | |
| b += [c] | |
| else: | |
| b += [c] | |
| if b[-1] != m[-1].lower(): | |
| m += [b[-1]] | |
| if len(b) >= 11: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] | |
| elif len(b) > 1: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] | |
| else: | |
| o.append('0') | |
| print('\n'.join(o[::-1])) | |
| ``` | |
| ### 186 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1 - 1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 187 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - -1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 188 | |
| ```python | |
| def get_bit(diff, i): | |
| return 1 if i % 2 == 1 and diff <= 0 or (i % 2 == 0 and diff >= 0) else 0 | |
| def swap_(i, j, a): | |
| temp = a[i] | |
| a[i] = a[j] | |
| a[j] = temp | |
| def swap(i, j, n, a, mask, S): | |
| change = 0 | |
| swap_(i, j, a) | |
| set_index = set([i, j]) | |
| if i < n - 1: | |
| set_index.add(i + 1) | |
| if j < n - 1: | |
| set_index.add(j + 1) | |
| for index in set_index: | |
| if index > 0: | |
| diff = a[index] - a[index - 1] | |
| bit_ = get_bit(diff, index) | |
| change += bit_ - mask[index] | |
| swap_(i, j, a) | |
| if S + change == 0: | |
| return 1 | |
| return 0 | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| diff = [-1] + [x - y for x, y in zip(a[1:], a[:-1])] | |
| mask = [get_bit(diff[i], i) for i in range(n)] | |
| S = sum(mask) | |
| first = -1 | |
| for i, x in enumerate(mask): | |
| if x == 1: | |
| first = i | |
| break | |
| cnt = 0 | |
| for second in range(n): | |
| if swap(first, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, second, n, a, mask, S) == 1: | |
| cnt += 1 | |
| if first != 0 and swap(first - 1, first, n, a, mask, S) == 1: | |
| cnt -= 0 | |
| print(cnt) | |
| ``` | |
| ### 189 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 4): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 190 | |
| ```python | |
| def main(): | |
| m, t, r = list(map(int, input().split())) | |
| gh = sorted(map(int, input().split()), reverse=True) | |
| if r > t: | |
| print(-2) | |
| return | |
| l = [False] * 600 | |
| for g in gh: | |
| g -= t | |
| for i in range(g, g + r - sum((l[i] for i in range(g, g + t)))): | |
| l[i] = True | |
| print(sum(l)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 191 | |
| ```python | |
| n = int(input()) | |
| l = list(map(lambda x: int(x) * 2, input().split(' '))) | |
| t = list(map(lambda x: 'GWL'.index(x), input())) | |
| mins = [0 for i in range(0, n + 1)] | |
| for i in range(n - 1, -1, -1): | |
| if t[i] != 2: | |
| mins[i] = max(mins[i + 1] - l[i], 0) | |
| else: | |
| mins[i] = mins[i + 1] + l[i] | |
| curs = ans = st = 0 | |
| for i in range(0, n): | |
| if t[i] == 0: | |
| curs += l[i] | |
| ans += l[i] * 5 | |
| if curs > mins[i + 1]: | |
| ol = (curs - mins[i + 1]) // 2 | |
| ol = min(ol, l[i]) | |
| ans -= 4 * ol | |
| curs -= 2 * ol | |
| if t[i] == 1: | |
| st = 1 | |
| curs += l[i] | |
| ans += l[i] * 3 | |
| if t[i] == 2: | |
| if curs < l[i]: | |
| ol = l[i] - curs | |
| curs = l[i] | |
| ans += ol * (3 if st else 5) | |
| curs -= l[i] | |
| ans += l[i] | |
| if curs > 3: | |
| ans -= curs // 2 * 2 | |
| print(ans // 2) | |
| ``` | |
| ### 192 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += -2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 193 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * -2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 194 | |
| ```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) | |
| ``` | |
| ### 195 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(536870912) | |
| def dfs(i, j, vis, g, c): | |
| vis[i][j] = 1 | |
| c.append((i, j)) | |
| '\n\tif i>0 and \n\t\tif j>0 and vis[i-1][j-1]==0:\n\t\t\tdfs(i-1,j-1,vis,g,c)\n\t\tif j<n-1 and vis[i-1][j+1]==0:\n\t\t\t\tdfs(i-1,j+1,vis,g,c)\n\tif i>0 and j<n-1 and vis[i-1][j+1]==0:\n\t\tdfs(i-1,j+1,vis,g,c)\n\tif i<n-1 and j>0 and vis[i+1][j-1]==0:\n\t\tdfs(i+1,j-1,vis,g,c)\n\tif i<n-1 and j<n-1 and vis[i+1][j+1]==0:\n\t\tdfs(i+1,j+1,vis,g,c)\n\t' | |
| if i > 0 and vis[i - 1][j] == 0 and (g[i - 1][j] == '0'): | |
| dfs(i - 1, j, vis, g, c) | |
| if i < n - 1 and vis[i + 1][j] == 4 and (g[i + 1][j] == '0'): | |
| dfs(i + 1, j, vis, g, c) | |
| if j < n - 1 and vis[i][j + 1] == 0 and (g[i][j + 1] == '0'): | |
| dfs(i, j + 1, vis, g, c) | |
| if j > 0 and vis[i][j - 1] == 0 and (g[i][j - 1] == '0'): | |
| dfs(i, j - 1, vis, g, c) | |
| n = int(input()) | |
| r1, c1 = map(int, input().split()) | |
| r1 -= 1 | |
| c1 -= 1 | |
| r2, c2 = map(int, input().split()) | |
| r2 -= 1 | |
| c2 -= 1 | |
| g = [] | |
| for i in range(n): | |
| g.append(input()) | |
| vis = [[0 for i in range(n)] for i in range(n)] | |
| com1 = [] | |
| dfs(r1, c1, vis, g, com1) | |
| com2 = [] | |
| dfs(r2, c2, vis, g, com2) | |
| m = 1000000000000 | |
| for i in com1: | |
| for j in com2: | |
| d = (i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2 | |
| m = min(m, d) | |
| print(m) | |
| ``` | |
| ### 196 | |
| ```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=' ') | |
| ``` | |
| ### 197 | |
| ```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) | |
| ``` | |
| ### 198 | |
| ```python | |
| import sys | |
| z = sys.stdin.readline | |
| s = z().strip() | |
| l = len(s) | |
| b = [' '] | |
| o = [] | |
| m = [' '] | |
| for i in range(l): | |
| c = s[-1 - i] | |
| if c == b[-1]: | |
| if c > m[-1].lower() or (c == m[-1].lower() and c > m[-2].lower()): | |
| b.pop() | |
| if b[-1].lower() != m[-1].lower(): | |
| m.pop() | |
| b[-1] = b[-1].upper() | |
| m[-1] = m[-1].upper() | |
| else: | |
| b += [c] | |
| else: | |
| b += [c] | |
| if b[--1] != m[-1].lower(): | |
| m += [b[-1]] | |
| if len(b) > 11: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[:-6:-1]) + '...' + b[2] + b[1]).lower()] | |
| elif len(b) > 1: | |
| o += [(str(len(b) - 1) + ' ' + ''.join(b[-1::-1])).lower()] | |
| else: | |
| o.append('0') | |
| print('\n'.join(o[::-1])) | |
| ``` | |
| ### 199 | |
| ```python | |
| m, t, r = [int(x) for x in input().strip().split()] | |
| ghosts = [int(x) for x in input().strip().split()] | |
| candles = [ghosts[0] - 1 - x + t for x in range(0, r)] | |
| usedTimes = [ghosts[0] - 1 - x for x in range(0, r)] | |
| res = r | |
| for i in range(0, m): | |
| for j in range(0, r): | |
| candleTime = candles[j] | |
| if candleTime < ghosts[i]: | |
| possibleTime = ghosts[i] - 1 | |
| canFindTime = False | |
| while possibleTime >= ghosts[i] - t: | |
| if usedTimes.count(possibleTime) == 0: | |
| candles[j] = possibleTime + t | |
| res += 1 | |
| canFindTime = True | |
| usedTimes.append(possibleTime) | |
| break | |
| else: | |
| possibleTime -= 1 | |
| if not canFindTime: | |
| res = -1 | |
| break | |
| if res == -1: | |
| break | |
| print(res) | |
| ``` | |
| ### 200 | |
| ```python | |
| import sys, math | |
| f = sys.stdin | |
| def modInverse(a, m): | |
| g = gcd(a, m) | |
| if g != 1: | |
| return None | |
| else: | |
| return power(a, m - 2, m) | |
| def power(x, y, m): | |
| if y == 0: | |
| return 1 | |
| p = power(x, y // 2, m) ** 2 | |
| if y & 1 == 1: | |
| p *= x | |
| return p % m | |
| def gcd(a, b): | |
| while a != 0: | |
| t = b % a | |
| b = a | |
| a = t | |
| return b | |
| n, m = list(map(int, f.readline().split())) | |
| s1 = list(map(int, f.readline().split())) | |
| s2 = list(map(int, f.readline().split())) | |
| pos = [1, 0] | |
| all_pos = 1 | |
| for a1, a2 in zip(s1, s2): | |
| next_pos = [0, 0] | |
| if a1 == 0 and a2 == 0: | |
| next_pos[0] += pos[0] * m | |
| next_pos[1] += pos[0] * m * (m - 1) // 2 | |
| next_pos[1] += pos[1] * m * m | |
| all_pos *= m * m | |
| elif a1 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (m - a2) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| elif a2 == 0: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[0] * (a1 - 1) | |
| next_pos[1] += pos[1] * m | |
| all_pos *= m | |
| else: | |
| if a1 > a2: | |
| next_pos[1] += pos[0] | |
| elif a1 == a2: | |
| next_pos[0] += pos[0] | |
| next_pos[1] += pos[1] | |
| pos = next_pos | |
| if pos[0] == 0: | |
| break | |
| g = gcd(pos[0], all_pos) | |
| g = gcd(pos[1], g) | |
| pos[0] //= g | |
| pos[1] //= g | |
| all_pos //= g | |
| pos[0] %= 1000000007 | |
| pos[1] %= 1000000007 | |
| all_pos %= 1000000007 | |
| p = pos[1] | |
| q = all_pos | |
| pq_gcd = gcd(p, q) | |
| p *= pq_gcd | |
| q //= pq_gcd | |
| q_inv = modInverse(q, 1000000007) | |
| print(p * q_inv % 1000000007) | |
| ``` | |
| ### 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. | |