|
Download tasks/equivalent-program-pair-search/contexts/sample-00014.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 225 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00014.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00014.md
-
curl -L -o sample-00014.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00014.md
225 kB
| ### 001 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in reversed(range(0, n)): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 002 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if not ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 003 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 004 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch != 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 005 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 2 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 006 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[--1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 007 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1 + 1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 008 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1 - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 009 | |
| ```python | |
| from sys import stdin | |
| def create_dp1(matrix, n, m): | |
| dp = [[2 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| elif i - 1 < 0 and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp2(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i + 1 >= n and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp3(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| if i + 1 >= n and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp4(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m - 1, -1, -1): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i - 1 < 0 and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| n, m = list(map(int, stdin.readline().split())) | |
| matrix = [] | |
| for _ in range(n): | |
| matrix.append(list(map(int, stdin.readline().split()))) | |
| dp1 = create_dp1(matrix, n, m) | |
| dp2 = create_dp2(matrix, n, m) | |
| dp3 = create_dp3(matrix, n, m) | |
| dp4 = create_dp4(matrix, n, m) | |
| total = -float('inf') | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| first = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1] | |
| second = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j] | |
| total = max(total, first, second) | |
| print(total) | |
| ``` | |
| ### 010 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(1, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 011 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= -1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 012 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & -1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 013 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -1 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 014 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][0] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 015 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n - 1): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 016 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1 - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 017 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in reversed(range(1, M + 1)): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in range(1, N + 1): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 0 | |
| for r in range(2, N): | |
| for c in range(2, M): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 018 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1 - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 019 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = min(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 020 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 3 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 021 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in range(1, M + 1): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in range(1, N + 1): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 0 | |
| for r in range(2, N): | |
| for c in range(2, M + 1): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 022 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 023 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 024 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(0): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 025 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 * tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 026 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 2 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 027 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 6 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 028 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= -1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 029 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][0] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 030 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j - 1]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 031 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > -4: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 032 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 4: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 033 | |
| ```python | |
| from sys import stdin | |
| def create_dp1(matrix, n, m): | |
| dp = [[3 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| elif i - 1 < 0 and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp2(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i + 1 >= n and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp3(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| if i + 1 >= n and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp4(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m - 1, -1, -1): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i - 1 < 0 and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| n, m = list(map(int, stdin.readline().split())) | |
| matrix = [] | |
| for _ in range(n): | |
| matrix.append(list(map(int, stdin.readline().split()))) | |
| dp1 = create_dp1(matrix, n, m) | |
| dp2 = create_dp2(matrix, n, m) | |
| dp3 = create_dp3(matrix, n, m) | |
| dp4 = create_dp4(matrix, n, m) | |
| total = -float('inf') | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| first = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1] | |
| second = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j] | |
| total = max(total, first, second) | |
| print(total) | |
| ``` | |
| ### 034 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::+1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 035 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query - 1] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 036 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 3: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 037 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] - x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 038 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + -3 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 5 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 040 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (-1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 041 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n - 1): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 042 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1 - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 043 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n - 1): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 044 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] - cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 045 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [-1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 046 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += -1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 047 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1 - 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 048 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1 + 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 049 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch != 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 050 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += -2 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 051 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) != True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 052 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 053 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p >= q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 054 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| dpa = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| dpb = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| for j in reversed(range(1, m + 1)): | |
| dpa[i][j][0] = max(dpa[i - 1][j][0], dpa[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpa[n + 1 - i][m + 1 - j][1] = max(dpa[n + 2 - i][m + 1 - j][1], dpa[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(n, 0, -1): | |
| for j in range(1, m + 1): | |
| dpb[i][j][0] = max(dpb[i + 1][j][0], dpb[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpb[n + 1 - i][m + 1 - j][1] = max(dpb[n - i][m + 1 - j][1], dpb[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(2, n): | |
| for j in range(2, m): | |
| x = dpa[i - 1][j][0] + dpa[i + 1][j][1] + dpb[i][j - 1][0] + dpb[i][j + 1][1] | |
| y = dpb[i + 1][j][0] + dpb[i - 1][j][1] + dpa[i][j - 1][0] + dpa[i][j + 1][1] | |
| ans = max(ans, x, y) | |
| print(ans) | |
| ``` | |
| ### 055 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = min(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 056 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 4]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 057 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (-1, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 058 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 059 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch != left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 060 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (-1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 061 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, -3, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 062 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[3] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 063 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1 + 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 064 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 065 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26 - 1): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 066 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query + 1] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 067 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) // mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 068 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 069 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 0 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 070 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) != True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 071 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 072 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 3 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 073 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(-4) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 074 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -3, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 075 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in reversed(range(sum, down - 1, -1)): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 076 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 077 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[-1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 078 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(2): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 079 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 0 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 080 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 081 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [2, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 082 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1 + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 083 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i - 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 084 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 085 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount -= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 086 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(2, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 087 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) // mod) | |
| main() | |
| ``` | |
| ### 088 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [5]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 089 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in range(1, M + 1): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in range(1, N + 1): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 0 | |
| for r in range(2, N + 1): | |
| for c in range(2, M): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 090 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount += 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count *= numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 091 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][2] = 1 | |
| MOD = 998244355 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 092 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 093 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K * d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 094 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i - 1]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 095 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[2]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 096 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(1) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 097 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 3 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 098 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 1 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 099 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 0: | |
| i += -1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 100 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1 - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 101 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 102 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index - 1] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 103 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 104 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in range(1, M + 1): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in range(1, N + 1): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 0 | |
| for r in range(2, N): | |
| for c in range(2, M): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 105 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j - 1] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 106 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc -= 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 107 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] - x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 108 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 4 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 1 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 110 | |
| ```python | |
| def solve(s): | |
| if len(s) == 2: | |
| return 1 if s[0] == s[1] else 2 | |
| elif len(s) == 3: | |
| if s[0] == s[1] == s[2]: | |
| return 1 | |
| elif s[0] == s[1] or s[1] == s[2]: | |
| return 6 | |
| elif s[0] == s[2]: | |
| return 7 | |
| else: | |
| return 3 | |
| if all((a == b for a, b in zip(s, s[1:]))): | |
| return 1 | |
| dp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| dp[0][0][0] = 1 | |
| dp[0][1][1] = 1 | |
| dp[0][2][-2] = 1 | |
| MOD = 998244353 | |
| for _ in range(len(s) - 1): | |
| ndp = [[[0] * 3 for _ in range(3)] for _ in range(2)] | |
| ndp[0][0][0] = (dp[0][1][0] + dp[0][2][0]) % MOD | |
| ndp[0][0][1] = (dp[0][1][1] + dp[0][2][1]) % MOD | |
| ndp[0][0][2] = (dp[0][1][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][0] = (dp[0][0][2] + dp[0][2][2]) % MOD | |
| ndp[0][1][1] = (dp[0][0][0] + dp[0][2][0]) % MOD | |
| ndp[0][1][2] = (dp[0][0][1] + dp[0][2][1]) % MOD | |
| ndp[0][2][0] = (dp[0][0][1] + dp[0][1][1]) % MOD | |
| ndp[0][2][1] = (dp[0][0][2] + dp[0][1][2]) % MOD | |
| ndp[0][2][2] = (dp[0][0][0] + dp[0][1][0]) % MOD | |
| ndp[1][0][0] = (dp[0][0][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][0][1] = (dp[0][0][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][0][2] = (dp[0][0][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][0] = (dp[0][1][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][1][1] = (dp[0][1][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| ndp[1][1][2] = (dp[0][1][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][0] = (dp[0][2][1] + dp[1][0][1] + dp[1][1][1] + dp[1][2][1]) % MOD | |
| ndp[1][2][1] = (dp[0][2][2] + dp[1][0][2] + dp[1][1][2] + dp[1][2][2]) % MOD | |
| ndp[1][2][2] = (dp[0][2][0] + dp[1][0][0] + dp[1][1][0] + dp[1][2][0]) % MOD | |
| dp = ndp | |
| return (sum((dp[1][x][sum(map(ord, s)) % 3] for x in range(3))) + all((a != b for a, b in zip(s, s[1:])))) % MOD | |
| s = input() | |
| print(solve(s)) | |
| ``` | |
| ### 111 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i - 1] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 112 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 4 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 113 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 114 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 115 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 1 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 116 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] - cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 117 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| a = input().split() | |
| b = input().split() | |
| ans = 0 | |
| for i in range(1, int(k ** 0.5) + 1): | |
| if k % i == 0: | |
| f1 = i | |
| f2 = k // i | |
| if f1 != f2: | |
| temp1 = 0 | |
| count = 2 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| else: | |
| temp1 = 0 | |
| count = 0 | |
| for j in range(n): | |
| if a[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f1: | |
| temp1 += 1 | |
| temp2 = 0 | |
| count = 0 | |
| for j in range(m): | |
| if b[j] == '1': | |
| count += 1 | |
| else: | |
| count = 0 | |
| if count >= f2: | |
| temp2 += 1 | |
| ans += temp1 * temp2 | |
| print(ans) | |
| ``` | |
| ### 118 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[-1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 119 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N - 1): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 120 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [2] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 121 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 * tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m] != -1 else print(n) | |
| ``` | |
| ### 122 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 3 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 123 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m <= w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 124 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i - 1] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 125 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 0 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i - 1]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 126 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(0): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 127 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1 + 1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 128 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[5:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 129 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in range(1, M + 1): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in reversed(range(1, N + 1)): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 0 | |
| for r in range(2, N): | |
| for c in range(2, M): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 130 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(4, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 131 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [0] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] + i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 132 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 1): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 133 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(3, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 134 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == -2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 135 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 0 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 136 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(-2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 137 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 138 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N - 1): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 139 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 0) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 140 | |
| ```python | |
| import sys | |
| n = int(sys.stdin.readline().strip()) | |
| s = sys.stdin.readline().strip() | |
| dp = [[-1] * (n + 1) for i in range(26)] | |
| for c in range(26): | |
| for j in range(n): | |
| tst = 1 if s[j] == chr(c + 97) else 0 | |
| dp[c][1 - tst] = max(dp[c][1 - tst], 1) | |
| for k in range(j + 1, n): | |
| if s[k] == chr(c + 97): | |
| tst += 1 | |
| dp[c][k - j + 1 - tst] = max(dp[c][k - j + 1 - tst], k - j + 1) | |
| q = int(sys.stdin.readline().strip()) | |
| for i in range(q): | |
| m, c = [item for item in sys.stdin.readline().strip().split()] | |
| m = int(m) | |
| print(dp[ord(c) - 97][m]) if dp[ord(c) - 97][m - 1] != -1 else print(n) | |
| ``` | |
| ### 141 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 2 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 142 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in reversed(range(N))] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 143 | |
| ```python | |
| a, b, c = [int(i) for i in input().split()] | |
| length = a * b | |
| numArr = [int(i) for i in input().split()] | |
| numArr.sort() | |
| count = 0 | |
| mmin = numArr[0] | |
| mmax = numArr[a - 1] | |
| if mmax - mmin > c: | |
| print(0) | |
| else: | |
| maxPos = a - 1 | |
| while maxPos + 1 < length and numArr[maxPos + 1] <= mmin + c: | |
| maxPos += 1 | |
| dis = maxPos + 1 - a | |
| bCount = 0 | |
| for i in range(maxPos + 1): | |
| if dis != 0: | |
| bCount *= 1 | |
| dis -= 1 | |
| if bCount % b == 1: | |
| count += numArr[i] | |
| dis += 1 | |
| else: | |
| count += numArr[i] | |
| print(count) | |
| ``` | |
| ### 144 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] >= upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 145 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i - 1] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 146 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| dpa = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| dpb = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| ans = 0 | |
| for i in reversed(range(1, n + 1)): | |
| for j in range(1, m + 1): | |
| dpa[i][j][0] = max(dpa[i - 1][j][0], dpa[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpa[n + 1 - i][m + 1 - j][1] = max(dpa[n + 2 - i][m + 1 - j][1], dpa[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(n, 0, -1): | |
| for j in range(1, m + 1): | |
| dpb[i][j][0] = max(dpb[i + 1][j][0], dpb[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpb[n + 1 - i][m + 1 - j][1] = max(dpb[n - i][m + 1 - j][1], dpb[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(2, n): | |
| for j in range(2, m): | |
| x = dpa[i - 1][j][0] + dpa[i + 1][j][1] + dpb[i][j - 1][0] + dpb[i][j + 1][1] | |
| y = dpb[i + 1][j][0] + dpb[i - 1][j][1] + dpa[i][j - 1][0] + dpa[i][j + 1][1] | |
| ans = max(ans, x, y) | |
| print(ans) | |
| ``` | |
| ### 147 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c == '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 148 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| N = int(input()) | |
| S = list(map(lambda x: ord(x) - ord('a'), list(input())[:-1])) | |
| res = [[0] * (N + 1) for _ in range(26)] | |
| for c in range(26): | |
| for l in range(N - 1): | |
| x = 0 | |
| for r in range(l + 1, N + 1): | |
| x += S[r - 1] != c | |
| res[c][x] = max(res[c][x], r - l) | |
| for i in range(N): | |
| res[c][i + 1] = max(res[c][i + 1], res[c][i]) | |
| for _ in range(int(input())): | |
| x, s = input().split() | |
| x = int(x) | |
| s = ord(s) - ord('a') | |
| print(res[s][x]) | |
| ``` | |
| ### 149 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == -1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 150 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 151 | |
| ```python | |
| M = 998244353 | |
| s = input() | |
| n = len(s) - 1 | |
| v = sum(map(ord, s)) | |
| print((pow(3, n, M) - pow(2, n, M) + pow(2, n // 3, M) * (n % 3 == 2) * (1 - 3 * (v % 3 == 0)) + all((s[i] != s[i + 1 - 1] for i in range(n))) - (v == 294) - 1) * any((s[0] != k for k in s)) % M + 1) | |
| ``` | |
| ### 152 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1 - 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 153 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1 - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 154 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 155 | |
| ```python | |
| M = 998244353 | |
| def powmod(a, x, m=M): | |
| y = 1 | |
| while 0 < x: | |
| if x % 2 == 1: | |
| y *= a | |
| y %= m | |
| x //= 2 | |
| a = a ** 2 | |
| a %= m | |
| return y | |
| def solve(s): | |
| n = len(s) | |
| nb = nc = 0 | |
| ch = s[0] | |
| if ch == 'b': | |
| nb += 3 | |
| elif ch == 'c': | |
| nc += 1 | |
| sf = True | |
| tf = True | |
| left = ch | |
| for ch in s[1:]: | |
| if ch == 'b': | |
| nb += 1 | |
| elif ch == 'c': | |
| nc += 1 | |
| if ch == left: | |
| sf = False | |
| else: | |
| tf = False | |
| left = ch | |
| if tf: | |
| return 1 | |
| if n == 3: | |
| if (nb + nc * 2) % 3: | |
| return 7 if sf else 6 | |
| else: | |
| return 3 | |
| if n % 3: | |
| return (powmod(3, n - 1) + M - powmod(2, n - 1) + (1 if sf else 0)) % M | |
| elif (nb + nc * 2) % 3: | |
| return (powmod(3, n - 1) + M - (powmod(2, n - 1) - powmod(2, n // 3 - 1)) + (1 if sf else 0)) % M | |
| else: | |
| return (powmod(3, n - 1) + M - (powmod(2, n // 3) + 4 * powmod(8, n // 3 - 1)) + (1 if sf else 0)) % M | |
| def main(): | |
| s = input() | |
| print(solve(s)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 156 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 1) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 157 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| dpa = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| dpb = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| for j in range(1, m + 1): | |
| dpa[i][j][0] = max(dpa[i - 1][j][0], dpa[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpa[n + 1 - i][m + 1 - j][1] = max(dpa[n + 2 - i][m + 1 - j][1], dpa[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(n, 0, -1): | |
| for j in reversed(range(1, m + 1)): | |
| dpb[i][j][0] = max(dpb[i + 1][j][0], dpb[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpb[n + 1 - i][m + 1 - j][1] = max(dpb[n - i][m + 1 - j][1], dpb[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(2, n): | |
| for j in range(2, m): | |
| x = dpa[i - 1][j][0] + dpa[i + 1][j][1] + dpb[i][j - 1][0] + dpb[i][j + 1][1] | |
| y = dpb[i + 1][j][0] + dpb[i - 1][j][1] + dpa[i][j - 1][0] + dpa[i][j + 1][1] | |
| ans = max(ans, x, y) | |
| print(ans) | |
| ``` | |
| ### 158 | |
| ```python | |
| import sys | |
| ii = lambda: sys.stdin.readline().strip() | |
| idata = lambda: [int(x) for x in ii().split()] | |
| n = int(ii()) | |
| s = ii() | |
| slov = {} | |
| for i in range(97, 97 + 26): | |
| slov[chr(i)] = [[], [1]] | |
| slov[s[0]] = [[1], [0, 0]] | |
| for j in range(-1, n): | |
| if slov[s[j]][1][-1] == 0: | |
| slov[s[j]][0][-1] += 1 | |
| else: | |
| slov[s[j]][0] += [1] | |
| slov[s[j]][1] += [0] | |
| for i in range(97, 97 + 26): | |
| if chr(i) != s[j]: | |
| slov[chr(i)][1][-1] += 1 | |
| for t in range(int(ii())): | |
| m, c = ii().split() | |
| m = int(m) | |
| a, b = slov[c] | |
| if sum(b) <= m: | |
| print(n) | |
| elif not bool(a): | |
| print(m) | |
| elif len(a) == 1: | |
| print(a[0] + m) | |
| else: | |
| l, r = (0, 0) | |
| ans = 0 | |
| summ_a, summ_b = (0, 0) | |
| used = 0 | |
| b1 = b[:] | |
| b1[0], b1[-1] = (0, 0) | |
| count = 0 | |
| while r != len(a): | |
| if summ_b + b1[r] <= m: | |
| summ_b += b1[r] | |
| summ_a += a[r] | |
| r += 1 | |
| ans = max(ans, m + summ_a) | |
| else: | |
| summ_a -= a[l] | |
| l += 1 | |
| summ_b -= b1[l] | |
| print(ans) | |
| ``` | |
| ### 159 | |
| ```python | |
| from sys import stdin | |
| def create_dp1(matrix, n, m): | |
| dp = [[4 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| elif i - 1 < 0 and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp2(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i + 1 >= n and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp3(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| if i + 1 >= n and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp4(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m - 1, -1, -1): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i - 1 < 0 and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| n, m = list(map(int, stdin.readline().split())) | |
| matrix = [] | |
| for _ in range(n): | |
| matrix.append(list(map(int, stdin.readline().split()))) | |
| dp1 = create_dp1(matrix, n, m) | |
| dp2 = create_dp2(matrix, n, m) | |
| dp3 = create_dp3(matrix, n, m) | |
| dp4 = create_dp4(matrix, n, m) | |
| total = -float('inf') | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| first = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1] | |
| second = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j] | |
| total = max(total, first, second) | |
| print(total) | |
| ``` | |
| ### 160 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1 + 1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 1 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 162 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n & i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 163 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1 + 1] = nc | |
| print(res) | |
| ``` | |
| ### 164 | |
| ```python | |
| n, k, l = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr = sorted(arr) | |
| if arr[n - 1] - arr[0] > l: | |
| print(0) | |
| else: | |
| maxVol = arr[0] + l | |
| index = 0 | |
| while index < n * k - 1 and arr[index + 1] <= maxVol: | |
| index += 1 | |
| s = arr[0] | |
| i = 0 | |
| remaining = n - 1 | |
| while remaining > 0 and i + k + remaining - 1 <= index: | |
| i += k | |
| remaining -= 1 | |
| s += arr[i] | |
| if remaining > 0 and index - (remaining - 1) > 1 + i: | |
| i = index - (remaining - 1) | |
| s += arr[i] | |
| remaining -= 1 | |
| while remaining > 3: | |
| i += 1 | |
| s += arr[i] | |
| remaining -= 1 | |
| print(s) | |
| ``` | |
| ### 165 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j - 1] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 166 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == -1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 167 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| def solve(N, M, A): | |
| dpa = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpb = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpc = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| dpd = [[0 for _ in range(M + 2)] for _ in range(N + 2)] | |
| for r in range(1, N + 1): | |
| for c in range(1, M + 1): | |
| dpa[r][c] = max(dpa[r - 1][c], dpa[r][c - 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(M, 0, -1): | |
| dpb[r][c] = max(dpb[r + 1][c], dpb[r][c + 1]) + A[r][c] | |
| for r in range(N, 0, -1): | |
| for c in range(1, M + 1): | |
| dpc[r][c] = max(dpc[r + 1][c], dpc[r][c - 1]) + A[r][c] | |
| for r in range(1, N + 1): | |
| for c in range(M, 0, -1): | |
| dpd[r][c] = max(dpd[r - 1][c], dpd[r][c + 1]) + A[r][c] | |
| ans = 3 | |
| for r in range(2, N): | |
| for c in range(2, M): | |
| a = dpa[r][c - 1] + dpb[r][c + 1] + dpc[r + 1][c] + dpd[r - 1][c] | |
| b = dpc[r][c - 1] + dpd[r][c + 1] + dpa[r - 1][c] + dpb[r + 1][c] | |
| ans = max(ans, a, b) | |
| return ans | |
| N, M = map(int, input().split()) | |
| A = [[0 for _ in range(M + 2)]] | |
| for i in range(N): | |
| row = [0] + [int(x) for x in input().split()] + [0] | |
| A.append(row) | |
| A.append([0 for _ in range(M + 2)]) | |
| print(solve(N, M, A)) | |
| ``` | |
| ### 168 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if not pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X * Y | |
| print(ans) | |
| ``` | |
| ### 169 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [] | |
| for i in range(n): | |
| a.append(list(map(int, input().split()))) | |
| dpa = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| dpb = [[[0, 0] for i in range(m + 2)] for i in range(n + 2)] | |
| ans = 0 | |
| for i in range(1, n + 1): | |
| for j in range(1, m + 1): | |
| dpa[i][j][0] = max(dpa[i - 1][j][0], dpa[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpa[n + 1 - i][m + 1 - j][1] = max(dpa[n + 2 - i][m + 1 - j][1], dpa[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(n, 0, -5): | |
| for j in range(1, m + 1): | |
| dpb[i][j][0] = max(dpb[i + 1][j][0], dpb[i][j - 1][0]) + a[i - 1][j - 1] | |
| dpb[n + 1 - i][m + 1 - j][1] = max(dpb[n - i][m + 1 - j][1], dpb[n + 1 - i][m + 2 - j][1]) + a[n - i][m - j] | |
| for i in range(2, n): | |
| for j in range(2, m): | |
| x = dpa[i - 1][j][0] + dpa[i + 1][j][1] + dpb[i][j - 1][0] + dpb[i][j + 1][1] | |
| y = dpb[i + 1][j][0] + dpb[i - 1][j][1] + dpa[i][j - 1][0] + dpa[i][j + 1][1] | |
| ans = max(ans, x, y) | |
| print(ans) | |
| ``` | |
| ### 170 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 171 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 5 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 172 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 0 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C - 1]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 173 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = -1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 174 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [4] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 175 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1 - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 176 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1 - 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 177 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[-1] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == 0: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 178 | |
| ```python | |
| import string | |
| import bisect | |
| import sys | |
| def main(): | |
| lines = sys.stdin.readlines() | |
| n = int(lines[0]) | |
| s = lines[1] | |
| vals = {} | |
| for c in string.ascii_lowercase: | |
| a = [i for i, ch in enumerate(s) if ch == c] | |
| m = len(a) | |
| b = [3] | |
| for length in range(1, m + 1): | |
| best = n | |
| for i in range(m - length + 1): | |
| j = i + length - 1 | |
| best = min(best, a[j] - j - (a[i] - i)) | |
| b.append(best) | |
| vals[c] = b | |
| q = int(lines[2]) | |
| r = [] | |
| idx = 3 | |
| while q > 0: | |
| q -= 1 | |
| query = lines[idx].split() | |
| idx += 1 | |
| m = int(query[0]) | |
| c = query[1] | |
| i = bisect.bisect_right(vals[c], m) | |
| r.append(str(min(n, i + m - 1))) | |
| print('\n'.join(r)) | |
| main() | |
| ``` | |
| ### 179 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 2 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 180 | |
| ```python | |
| def main(): | |
| a = input().strip() | |
| l = len(a) | |
| if l < 6: | |
| s = set() | |
| s.add(a) | |
| q = [a] | |
| oa = ord('a') | |
| while q: | |
| a = q.pop() | |
| for i in range(l - 1): | |
| if a[i] != a[i + 1]: | |
| t = chr(oa + 3 - (ord(a[i]) - oa) - (ord(a[i + 1]) - oa)) | |
| na = a[:i] + t * 2 + a[i + 2:] | |
| if na not in s: | |
| s.add(na) | |
| q.append(na) | |
| print(len(s)) | |
| return | |
| t = a[0] * l | |
| if t == a: | |
| print(1) | |
| return | |
| x = 0 | |
| for c in a: | |
| if c == 'b': | |
| x += 1 | |
| elif c == 'c': | |
| x += 2 | |
| ans = 0 | |
| if all((a[i] != a[i + 1] for i in range(l - 1))): | |
| ans += 1 | |
| mod = 998244353 | |
| dp = (1, 0, -3, 0, 1, 0, 0, 0, 1, 0, 0, 0) | |
| for i in range(l - 1): | |
| s = (dp[-1] + dp[-2] + dp[-3]) % mod | |
| dp = ((dp[3] + dp[6]) % mod, (dp[4] + dp[7]) % mod, (dp[5] + dp[8]) % mod, (dp[2] + dp[8]) % mod, (dp[0] + dp[6]) % mod, (dp[1] + dp[7]) % mod, (dp[1] + dp[4]) % mod, (dp[2] + dp[5]) % mod, (dp[0] + dp[3]) % mod, (dp[0] + dp[5] + dp[7] + s) % mod, (dp[1] + dp[3] + dp[8] + s) % mod, (dp[2] + dp[4] + dp[6] + s) % mod) | |
| print((ans + dp[9 + x % 3]) % mod) | |
| main() | |
| ``` | |
| ### 181 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - -1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 182 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = 3 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 183 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1 - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 0 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 184 | |
| ```python | |
| N, M, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| pA = [0] * (N + 1) | |
| for i in range(N): | |
| pA[i + 1] = pA[i] + A[i] | |
| pB = [0] * (M + 1) | |
| for i in range(M): | |
| pB[i + 1] = pB[i] + B[i] | |
| ans = 0 | |
| for p in range(1, int(K ** 0.5) + 1): | |
| if K % p > 0: | |
| continue | |
| q = K // p | |
| if p > q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - p): | |
| if pA[i + p] - pA[i] == p: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - q): | |
| if pB[i + q] - pB[i] == q: | |
| Y += 1 | |
| ans += X * Y | |
| if p == q: | |
| break | |
| X = 0 | |
| for i in range(N + 1 - q): | |
| if pA[i + q] - pA[i] == q: | |
| X += 1 | |
| Y = 0 | |
| for i in range(M + 1 - p): | |
| if pB[i + p] - pB[i] == p: | |
| Y += 1 | |
| ans += X // Y | |
| print(ans) | |
| ``` | |
| ### 185 | |
| ```python | |
| import bisect | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| def getTransIntList(n): | |
| first = getIntList() | |
| m = len(first) | |
| result = [[0] * n for _ in range(m)] | |
| for i in range(m): | |
| result[i][0] = first[i] | |
| for j in range(1, n): | |
| curr = getIntList() | |
| for i in range(m): | |
| result[i][j] = curr[i] | |
| return result | |
| n, k, l = getIntList() | |
| m = n * k | |
| a = getIntList() | |
| a.sort() | |
| lowLim = a[0] | |
| upLim = lowLim + l | |
| def solve(): | |
| if a[n - 1] > upLim: | |
| return 0 | |
| i0 = bisect.bisect_right(a, upLim) | |
| nbBad = m - i0 | |
| i0 -= 1 | |
| value = 0 | |
| for i in range(n - 1, -1, -1): | |
| diff = min(nbBad, k - 1) | |
| rest = k - diff | |
| nbBad -= diff | |
| value += a[i0 - rest + 1] | |
| i0 -= rest | |
| return value | |
| print(solve()) | |
| ``` | |
| ### 186 | |
| ```python | |
| from sys import stdin | |
| def create_dp1(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| elif i - 1 < 0 and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp2(matrix, n, m): | |
| dp = [[4 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i + 1 >= n and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp3(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m): | |
| if i + 1 < n: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i + 1][j]) | |
| if j - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j - 1]) | |
| if i + 1 >= n and j - 1 < 0: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| def create_dp4(matrix, n, m): | |
| dp = [[0 for i in range(m)] for j in range(n)] | |
| for i in range(n): | |
| for j in range(m - 1, -1, -1): | |
| if i - 1 >= 0: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i - 1][j]) | |
| if j + 1 < m: | |
| dp[i][j] = max(dp[i][j], matrix[i][j] + dp[i][j + 1]) | |
| if i - 1 < 0 and j + 1 >= m: | |
| dp[i][j] = matrix[i][j] | |
| return dp | |
| n, m = list(map(int, stdin.readline().split())) | |
| matrix = [] | |
| for _ in range(n): | |
| matrix.append(list(map(int, stdin.readline().split()))) | |
| dp1 = create_dp1(matrix, n, m) | |
| dp2 = create_dp2(matrix, n, m) | |
| dp3 = create_dp3(matrix, n, m) | |
| dp4 = create_dp4(matrix, n, m) | |
| total = -float('inf') | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| first = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1] | |
| second = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j] | |
| total = max(total, first, second) | |
| print(total) | |
| ``` | |
| ### 187 | |
| ```python | |
| N = int(input()) | |
| first = [] | |
| second = [] | |
| for i in range(N): | |
| first.append([s for s in input()]) | |
| for i in range(N): | |
| second.append([s for s in input()]) | |
| def rotate_90(matrix): | |
| return list(zip(*matrix[::-1])) | |
| def flip(matrix): | |
| return matrix[::-1] | |
| def compare_matrices(first, second): | |
| for i in range(N): | |
| for j in range(N): | |
| if first[i][j] != second[i][j]: | |
| return False | |
| return True | |
| def wrap(first, second): | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| hold_first = first[:] | |
| for _ in range(3): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| first = hold_first | |
| first = flip(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| for _ in range(0): | |
| first = rotate_90(first) | |
| if compare_matrices(first, second) == True: | |
| return 'Yes' | |
| return 'No' | |
| print(wrap(first, second)) | |
| ``` | |
| ### 188 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query - 1] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 189 | |
| ```python | |
| n = int(input()) | |
| ns = [] | |
| for i in range(n): | |
| s = input() | |
| ns.append(s) | |
| ns2 = [] | |
| for i in range(n): | |
| s = input() | |
| ns2.append(s) | |
| def rotate(i, j): | |
| return (j, n - 1 - i) | |
| def flip(i, j): | |
| return (j, i) | |
| def main(): | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| if ns[i][j] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = flip(i, j) | |
| if ns[a][b] != ns2[i][j]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| same = True | |
| for i in range(n): | |
| for j in range(n): | |
| a, b = rotate(i, j) | |
| a, b = rotate(a, b) | |
| a, b = rotate(a, b) | |
| a, b = flip(a, b) | |
| if not ns[i][j] != ns2[a][b]: | |
| same = False | |
| break | |
| if same == False: | |
| break | |
| if same: | |
| return True | |
| else: | |
| return False | |
| if main(): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 190 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const - 1): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 191 | |
| ```python | |
| import sys | |
| from math import * | |
| def minp(): | |
| return sys.stdin.readline().strip() | |
| def mint(): | |
| return int(minp()) | |
| def mints(): | |
| return list(map(int, minp().split())) | |
| n, m = mints() | |
| a = [0] * n | |
| dp1 = [0] * n | |
| dp2 = [0] * n | |
| dp3 = [0] * n | |
| dp4 = [0] * n | |
| for i in range(n): | |
| a[i] = list(mints()) | |
| dp1[i] = [0] * m | |
| dp2[i] = [0] * m | |
| dp3[i] = [0] * m | |
| dp4[i] = [0] * m | |
| dp1[0][0] = a[0][0] | |
| dp2[n - 1][m - 1] = a[n - 1][m - 1] | |
| dp3[n - 1][0] = a[n - 1][0] | |
| dp4[0][m - 1] = a[0][m - 1] | |
| '\nfor i in range(1,n):\n\tdp1[i][0] = dp1[i-1][0] + a[i][0] # >>>>\nfor i in range(n-2,-1,-1):\n\tdp2[i][m-1] = dp2[i+1][m-1] + a[i][m-1] # <<<<\nfor i in range(n-2,-1,-1):\n\tdp3[i][0] = dp3[i+1][0] + a[i][0] # <<<<\nfor i in range(1,n):\n\tdp4[i][m-1] = dp4[i-1][m-1] + a[i][m-1] # >>>>\nfor i in range(1,m):\n\tdp1[0][i] = dp1[0][i-1] + a[0][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp2[n-1][i] = dp2[n-1][i+1] + a[n-1][i] # <<<<\nfor i in range(1,m):\n\tdp3[n-1][i] = dp3[n-1][i-1] + a[n-1][i] # >>>>\nfor i in range(m-2,-1,-1):\n\tdp4[0][i] = dp4[0][i+1] + a[0][i] # >>>>\n' | |
| for i in range(0, n): | |
| for j in range(0, m): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp1[i - 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp1[i][j - 1]) | |
| dp1[i][j] = z + a[i][j] | |
| for i in range(n - 1, -1, -1): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp2[i + 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp2[i][j + 1]) | |
| dp2[i][j] = z + a[i][j] | |
| for i in range(n - 1 - 1, -1, -1): | |
| for j in range(0, m): | |
| z = 0 | |
| if i + 1 < n: | |
| z = dp3[i + 1][j] | |
| if j - 1 >= 0: | |
| z = max(z, dp3[i][j - 1]) | |
| dp3[i][j] = z + a[i][j] | |
| for i in range(0, n): | |
| for j in range(m - 1, -1, -1): | |
| z = 0 | |
| if i - 1 >= 0: | |
| z = dp4[i - 1][j] | |
| if j + 1 < m: | |
| z = max(z, dp4[i][j + 1]) | |
| dp4[i][j] = z + a[i][j] | |
| 'for i in dp1:\n\tprint(i)\nprint()\nfor i in dp2:\n\tprint(i)\nprint()\nfor i in dp3:\n\tprint(i)\nprint()\nfor i in dp4:\n\tprint(i)\nprint()\n' | |
| r = 0 | |
| for i in range(1, n - 1): | |
| for j in range(1, m - 1): | |
| r = max(r, dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]) | |
| r = max(r, dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]) | |
| print(r) | |
| ``` | |
| ### 192 | |
| ```python | |
| import copy | |
| n = int(input()) | |
| flg = 0 | |
| mat1 = [] | |
| mat2 = [] | |
| mats = [] | |
| for i in range(0, n): | |
| mat1.append(tuple(input().strip())) | |
| for i in range(0, n): | |
| mat2.append(tuple(input().strip())) | |
| mats.append(mat2) | |
| matu = copy.copy(mat2) | |
| matv = copy.copy(mat2) | |
| matv = list(zip(*matv)) | |
| mats.append(matv) | |
| mattem = copy.copy(matu) | |
| for i in range(0, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| mattem = copy.copy(matv) | |
| for i in range(1, 3): | |
| mattem = list(zip(*list(reversed(mattem)))) | |
| mats.append(mattem) | |
| flg = 0 | |
| for cmat in mats: | |
| flg2 = 1 | |
| for ri in range(0, n): | |
| if cmat[ri] != mat1[ri]: | |
| flg2 = 0 | |
| break | |
| if flg2 == 1: | |
| flg = 1 | |
| break | |
| if flg == 1: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 193 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(2, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 194 | |
| ```python | |
| def get_dividers(k): | |
| dividers = set() | |
| for i in range(1, int(k ** 0.5) + 2): | |
| if k % i == 0: | |
| dividers.add(i) | |
| dividers.add(k // i) | |
| return sorted(list(dividers)) | |
| n, m, k = map(int, input().split()) | |
| dividers = get_dividers(k) | |
| A = list(map(int, input().split())) | |
| B = list(map(int, input().split())) | |
| def count(A, d): | |
| in_a_row = 0 | |
| res = 0 | |
| for a in A: | |
| if a == 1: | |
| in_a_row += 1 | |
| if in_a_row >= d: | |
| res += 1 | |
| else: | |
| in_a_row = -2 | |
| return res | |
| a_d = {} | |
| b_d = {} | |
| for d in dividers: | |
| a_d[d] = count(A, d) | |
| b_d[d] = count(B, d) | |
| res = 0 | |
| for d in dividers: | |
| res += a_d[d] * b_d[k // d] | |
| print(res) | |
| ``` | |
| ### 195 | |
| ```python | |
| N = int(input()) | |
| map_1 = [list(input()) for i in range(N)] | |
| map_2 = [list(input()) for i in range(N)] | |
| maps = list() | |
| maps.append([[map_2[i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - i][N - 1 - j] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[j][N - 1 - i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][i] for j in range(N)] for i in range(N)]) | |
| maps.append([[map_2[N - 1 - j][N - 1 - i] for j in reversed(range(N))] for i in range(N)]) | |
| print(('No', 'Yes')[any((map_1 == el for el in maps))]) | |
| ``` | |
| ### 196 | |
| ```python | |
| n, k, l = map(int, input().split()) | |
| barrels = [] | |
| for i in range(n): | |
| barrels.append([]) | |
| for j in range(k): | |
| barrels[i].append(-1) | |
| boards = sorted(list(map(int, input().split()))) | |
| ans = 0 | |
| if boards[n - 1] - boards[0] <= l: | |
| i = n * k - 1 | |
| ba, bo = (n - 1, k) | |
| while boards[i] > boards[0] + l: | |
| bo -= 1 | |
| if bo == -2: | |
| ba, bo = (ba - 1, k - 1) | |
| barrels[ba][bo] = boards[i] | |
| i -= 1 | |
| ba, bo = (0, -1) | |
| j = 0 | |
| flag = True | |
| while j <= i: | |
| bo += 1 | |
| if bo == k or barrels[ba][bo] != -1: | |
| ba, bo = (ba + 1, 0) | |
| flag = True | |
| barrels[ba][bo] = boards[j] | |
| if flag: | |
| ans += barrels[ba][bo] | |
| flag = False | |
| j += 1 | |
| print(ans) | |
| ``` | |
| ### 197 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1 - 1] = nc | |
| print(res) | |
| ``` | |
| ### 198 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [1] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 199 | |
| ```python | |
| import bisect | |
| import sys | |
| from collections import Counter | |
| sys.setrecursionlimit(100000) | |
| input = sys.stdin.readline | |
| INF = 2 ** 62 - 1 | |
| def read_int(): | |
| return int(input()) | |
| def read_int_n(): | |
| return list(map(int, input().split())) | |
| def read_float(): | |
| return float(input()) | |
| def read_float_n(): | |
| return list(map(float, input().split())) | |
| def read_str(): | |
| return input().strip() | |
| def read_str_n(): | |
| return list(map(str, input().split())) | |
| def error_print(*args): | |
| print(*args, file=sys.stderr) | |
| def mt(f): | |
| import time | |
| def wrap(*args, **kwargs): | |
| s = time.time() | |
| ret = f(*args, **kwargs) | |
| e = time.time() | |
| error_print(e - s, 'sec') | |
| return ret | |
| return wrap | |
| def divisor(n): | |
| for i in range(1, int(n ** 0.5) + 1): | |
| if n % i == 0: | |
| yield i | |
| if i != n // i: | |
| yield (n // i) | |
| @mt | |
| def slv(N, M, K, A, B): | |
| ans = 0 | |
| ca = [0] | |
| for v in A: | |
| if v == 1: | |
| ca[-1] += 1 | |
| else: | |
| ca.append(0) | |
| cb = [0] | |
| for v in B: | |
| if v == 1: | |
| cb[-1] += 1 | |
| else: | |
| cb.append(0) | |
| ca = Counter(ca) | |
| cb = Counter(cb) | |
| ans += 0 | |
| for d in divisor(K): | |
| e = K // d | |
| for i, x in ca.items(): | |
| for j, y in cb.items(): | |
| if i < d or j < e: | |
| continue | |
| ans += (i - d + 1) * (j - e + 1) * x * y | |
| return ans | |
| def main(): | |
| N, M, K = read_int_n() | |
| A = read_int_n() | |
| B = read_int_n() | |
| print(slv(N, M, K, A, B)) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 200 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 2 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |