|
Download tasks/equivalent-program-pair-search/contexts/sample-00037.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 241 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00037.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00037.md
-
curl -L -o sample-00037.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00037.md
241 kB
| ### 001 | |
| ```python | |
| def get_relation(synonyms, antonyms, word1, word2): | |
| if word1 not in synonyms or word2 not in synonyms: | |
| return '3' | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 == group2: | |
| return '1' | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| return '2' | |
| return '3' | |
| def unify_synonyms(synonyms, groups, group1, group2): | |
| min_group = min(group1, group2) | |
| max_group = max(group1, group2) | |
| max_synonyms = groups[max_group] | |
| for synonym in max_synonyms: | |
| synonyms[synonym] = min_group | |
| update_groups(groups, min_group, max_synonyms) | |
| return (min_group, max_group) | |
| def make_synonyms(synonyms, antonyms, groups, group1, group2): | |
| min_group1, max_group1 = unify_synonyms(synonyms, groups, group1, group2) | |
| if group1 in antonyms and group2 in antonyms: | |
| min_group2, max_group2 = unify_synonyms(synonyms, groups, antonyms[group1], antonyms[group2]) | |
| del antonyms[min_group1] | |
| del antonyms[max_group1] | |
| del antonyms[min_group2] | |
| del antonyms[max_group2] | |
| antonyms[min_group1] = min_group2 | |
| antonyms[min_group2] = min_group1 | |
| return min_group1 | |
| if max_group1 in antonyms: | |
| antonym_group = antonyms[max_group1] | |
| del antonyms[max_group1] | |
| antonyms[min_group1] = antonym_group | |
| antonyms[antonym_group] = min_group1 | |
| return min_group1 | |
| def update_groups(groups, group, words): | |
| if group in groups: | |
| groups[group].update(words) | |
| else: | |
| groups.update({group: set(words)}) | |
| def make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group): | |
| if relation == '1': | |
| if word1 not in synonyms and word2 not in synonyms: | |
| current_group += 1 | |
| synonyms[word1] = current_group | |
| synonyms[word2] = current_group | |
| update_groups(groups, current_group, [word1, word2]) | |
| return current_group | |
| if word1 not in synonyms: | |
| synonyms[word1] = synonyms[word2] | |
| update_groups(groups, synonyms[word2], [word1]) | |
| return current_group | |
| if word2 not in synonyms: | |
| synonyms[word2] = synonyms[word1] | |
| update_groups(groups, synonyms[word1], [word2]) | |
| return current_group | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 != group2: | |
| make_synonyms(synonyms, antonyms, groups, group1, group2) | |
| return current_group | |
| if relation == '2': | |
| if word1 not in synonyms: | |
| current_group += 1 | |
| group1 = current_group | |
| synonyms[word1] = group1 | |
| update_groups(groups, group1, [word1]) | |
| else: | |
| group1 = synonyms[word1] | |
| if word2 not in synonyms: | |
| current_group += 1 | |
| group2 = current_group | |
| synonyms[word2] = group2 | |
| update_groups(groups, group2, [word2]) | |
| else: | |
| group2 = synonyms[word2] | |
| if group1 not in antonyms and group2 not in antonyms: | |
| antonyms[group1] = group2 | |
| antonyms[group2] = group1 | |
| return current_group | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| antonym_group = antonyms[group1] | |
| group2 = make_synonyms(synonyms, antonyms, groups, group2, antonym_group) | |
| if group2 in antonyms and antonyms[group2] != group1: | |
| antonym_group = antonyms[group2] | |
| make_synonyms(synonyms, antonyms, groups, group1, antonym_group) | |
| return current_group | |
| def make_relations(relation_no): | |
| synonyms = dict() | |
| antonyms = dict() | |
| groups = dict() | |
| current_group = 0 | |
| for relation in range(relation_no): | |
| relation, word1, word2 = input().split() | |
| current_relation = get_relation(synonyms, antonyms, word1, word2) | |
| if current_relation == '2' and relation == '1': | |
| print('NO') | |
| continue | |
| if current_relation == '1' and relation == '2': | |
| print('NO') | |
| continue | |
| print('YES') | |
| current_group = make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group) | |
| return (synonyms, antonyms) | |
| def answer_questions(question_no, synonyms, antonyms): | |
| for question in range(question_no): | |
| word1, word2 = input().split() | |
| print(get_relation(synonyms, antonyms, word1, word2)) | |
| def main(): | |
| word_no, relation_no, question_no = [int(number) for number in input().split()] | |
| input() | |
| synonyms, antonyms = make_relations(relation_no) | |
| answer_questions(question_no, synonyms, antonyms) | |
| main() | |
| ``` | |
| ### 002 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u * v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 003 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 004 | |
| ```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() | |
| ``` | |
| ### 005 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 4): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 006 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // -2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 007 | |
| ```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() | |
| ``` | |
| ### 008 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= -1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 009 | |
| ```python | |
| def get_relation(synonyms, antonyms, word1, word2): | |
| if word1 not in synonyms or word2 not in synonyms: | |
| return '3' | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 == group2: | |
| return '1' | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| return '2' | |
| return '3' | |
| def unify_synonyms(synonyms, groups, group1, group2): | |
| min_group = min(group1, group2) | |
| max_group = max(group1, group2) | |
| max_synonyms = groups[max_group] | |
| for synonym in max_synonyms: | |
| synonyms[synonym] = min_group | |
| update_groups(groups, min_group, max_synonyms) | |
| return (min_group, max_group) | |
| def make_synonyms(synonyms, antonyms, groups, group1, group2): | |
| min_group1, max_group1 = unify_synonyms(synonyms, groups, group1, group2) | |
| if group1 in antonyms and group2 in antonyms: | |
| min_group2, max_group2 = unify_synonyms(synonyms, groups, antonyms[group1], antonyms[group2]) | |
| del antonyms[min_group1] | |
| del antonyms[max_group1] | |
| del antonyms[min_group2] | |
| del antonyms[max_group2] | |
| antonyms[min_group1] = min_group2 | |
| antonyms[min_group2] = min_group1 | |
| return min_group1 | |
| if max_group1 in antonyms: | |
| antonym_group = antonyms[max_group1] | |
| del antonyms[max_group1] | |
| antonyms[min_group1] = antonym_group | |
| antonyms[antonym_group] = min_group1 | |
| return min_group1 | |
| def update_groups(groups, group, words): | |
| if group in groups: | |
| groups[group].update(words) | |
| else: | |
| groups.update({group: set(words)}) | |
| def make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group): | |
| if relation == '1': | |
| if word1 not in synonyms and word2 not in synonyms: | |
| current_group += 1 | |
| synonyms[word1] = current_group | |
| synonyms[word2] = current_group | |
| update_groups(groups, current_group, [word1, word2]) | |
| return current_group | |
| if word1 not in synonyms: | |
| synonyms[word1] = synonyms[word2] | |
| update_groups(groups, synonyms[word2], [word1]) | |
| return current_group | |
| if word2 not in synonyms: | |
| synonyms[word2] = synonyms[word1] | |
| update_groups(groups, synonyms[word1], [word2]) | |
| return current_group | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 != group2: | |
| make_synonyms(synonyms, antonyms, groups, group1, group2) | |
| return current_group | |
| if relation == '2': | |
| if word1 not in synonyms: | |
| current_group += -1 | |
| group1 = current_group | |
| synonyms[word1] = group1 | |
| update_groups(groups, group1, [word1]) | |
| else: | |
| group1 = synonyms[word1] | |
| if word2 not in synonyms: | |
| current_group += 1 | |
| group2 = current_group | |
| synonyms[word2] = group2 | |
| update_groups(groups, group2, [word2]) | |
| else: | |
| group2 = synonyms[word2] | |
| if group1 not in antonyms and group2 not in antonyms: | |
| antonyms[group1] = group2 | |
| antonyms[group2] = group1 | |
| return current_group | |
| if group1 in antonyms and antonyms[group1] != group2: | |
| antonym_group = antonyms[group1] | |
| group2 = make_synonyms(synonyms, antonyms, groups, group2, antonym_group) | |
| if group2 in antonyms and antonyms[group2] != group1: | |
| antonym_group = antonyms[group2] | |
| make_synonyms(synonyms, antonyms, groups, group1, antonym_group) | |
| return current_group | |
| def make_relations(relation_no): | |
| synonyms = dict() | |
| antonyms = dict() | |
| groups = dict() | |
| current_group = 0 | |
| for relation in range(relation_no): | |
| relation, word1, word2 = input().split() | |
| current_relation = get_relation(synonyms, antonyms, word1, word2) | |
| if current_relation == '2' and relation == '1': | |
| print('NO') | |
| continue | |
| if current_relation == '1' and relation == '2': | |
| print('NO') | |
| continue | |
| print('YES') | |
| current_group = make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group) | |
| return (synonyms, antonyms) | |
| def answer_questions(question_no, synonyms, antonyms): | |
| for question in range(question_no): | |
| word1, word2 = input().split() | |
| print(get_relation(synonyms, antonyms, word1, word2)) | |
| def main(): | |
| word_no, relation_no, question_no = [int(number) for number in input().split()] | |
| input() | |
| synonyms, antonyms = make_relations(relation_no) | |
| answer_questions(question_no, synonyms, antonyms) | |
| main() | |
| ``` | |
| ### 010 | |
| ```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() | |
| ``` | |
| ### 011 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][-2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 012 | |
| ```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) | |
| ``` | |
| ### 013 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 014 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not (not stack or arr[i] > stack[-1]): | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 015 | |
| ```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) | |
| ``` | |
| ### 016 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 017 | |
| ```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) | |
| ``` | |
| ### 018 | |
| ```python | |
| class Union: | |
| def __init__(self, n): | |
| self.p = {i: i for i in range(n)} | |
| self.rank = {i: 1 for i in range(n)} | |
| def find(self, x): | |
| if x < 0: | |
| return x | |
| if self.p[x] != x: | |
| self.p[x] = self.find(self.p[x]) | |
| return self.p[x] | |
| def union(self, x, y): | |
| if x < 0 or y < 0: | |
| return | |
| x = self.find(x) | |
| y = self.find(y) | |
| if x != y: | |
| if self.rank[x] < self.rank[y]: | |
| self.p[x] = y | |
| self.rank[y] += self.rank[x] | |
| else: | |
| self.p[y] = x | |
| self.rank[x] += self.rank[y] | |
| n, m, q = map(int, input().split()) | |
| listW = input().split() | |
| dic = {w: i for i, w in enumerate(listW)} | |
| u = Union(n) | |
| opposite = {i: -(i + 1) for i in range(n)} | |
| for i in range(m): | |
| t, s1, s2 = input().split() | |
| x = u.find(dic[s1]) | |
| y = u.find(dic[s2]) | |
| if t == '1': | |
| if opposite[x + 1] == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if x != y: | |
| ox = opposite[x] | |
| oy = opposite[y] | |
| op = max(ox, oy) | |
| u.union(x, y) | |
| u.union(ox, oy) | |
| op = u.find(op) | |
| p = u.find(x) | |
| if op > 0: | |
| opposite[p] = op | |
| opposite[op] = p | |
| elif x == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if opposite[x] != y: | |
| u.union(y, opposite[x]) | |
| u.union(x, opposite[y]) | |
| x = u.find(x) | |
| y = u.find(y) | |
| opposite[x] = y | |
| opposite[y] = x | |
| for _ in range(q): | |
| s1, s2 = input().split() | |
| id1, id2 = (u.find(dic[s1]), u.find(dic[s2])) | |
| if id1 == id2: | |
| print(1) | |
| elif opposite[id1] == id2: | |
| print(2) | |
| else: | |
| print(3) | |
| ``` | |
| ### 019 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps // n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 020 | |
| ```python | |
| import sys | |
| dr = [0, 1, -1, -1] | |
| dc = [1, 0, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 021 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| u = list(range(n + 1)) | |
| v = [n] * n | |
| s = {q: i for i, q in zip(u, f())} | |
| p = ['YES'] * m | |
| def g(i): | |
| k = u[i] | |
| while k != u[k]: | |
| k = u[k] | |
| while u[i] != k: | |
| i, u[i] = (u[i - 1], k) | |
| return k | |
| for l in range(m): | |
| r, x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, b = (g(i), g(j)) | |
| i, j = (v[a], v[b]) | |
| c, d = (g(i), g(j)) | |
| if r == '2': | |
| b, d = (d, b) | |
| if a == d: | |
| p[l] = 'NO' | |
| elif c == b == n: | |
| v[a], v[d] = (d, a) | |
| elif c == b: | |
| p[l] = 'NO' | |
| elif c == n: | |
| u[a] = b | |
| elif b == n: | |
| u[d] = c | |
| elif d == n: | |
| u[b] = a | |
| else: | |
| u[a], u[c] = (b, d) | |
| u = [g(q) for q in u] | |
| v = [u[q] for q in v] | |
| for l in range(k): | |
| x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, c = (u[i], u[j]) | |
| p.append(str(3 - 2 * (a == c) - (a == v[c]))) | |
| print('\n'.join(p)) | |
| ``` | |
| ### 022 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += -3 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 023 | |
| ```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) | |
| ``` | |
| ### 024 | |
| ```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) | |
| ``` | |
| ### 025 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi *= ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 026 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 027 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = -2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 028 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) + inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 029 | |
| ```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) | |
| ``` | |
| ### 030 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 031 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return --1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 032 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i - 1]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 033 | |
| ```python | |
| l = input().split() | |
| n = int(l[1]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 034 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c == 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 035 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(2): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 036 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 3) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 037 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 038 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[2]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 039 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (4, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 040 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| c = {x: ([x], []) for x in f()} | |
| for i in range(m): | |
| t, x, y = f() | |
| s = 2 - int(t) | |
| cx, cy = (c[x], c[y]) | |
| if cx[0] is cy[s]: | |
| print('NO') | |
| continue | |
| print('YES') | |
| if cx[0] is cy[1 - s]: | |
| continue | |
| if len(cx[0]) + len(cx[1]) < len(cy[0]) + len(cy[1]): | |
| cx, cy = (cy, cx) | |
| ux, vx = cx | |
| if s: | |
| uy, vy = cy | |
| else: | |
| vy, uy = cy | |
| ux += uy | |
| vx += vy | |
| for x in uy: | |
| c[x] = (ux, vx) | |
| for x in vy: | |
| c[x] = (vx, ux) | |
| for i in range(k): | |
| x, y = f() | |
| print(1 if c[x][0] is c[y][0] else 2 if c[x][0] is c[y][1] else 6) | |
| ``` | |
| ### 041 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 0 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i * (total % i)) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 042 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| u = list(range(n + 1)) | |
| v = [n] * n | |
| s = {q: i for i, q in zip(u, f())} | |
| p = ['YES'] * m | |
| def g(i): | |
| k = u[i] | |
| while k != u[k]: | |
| k = u[k] | |
| while u[i] != k: | |
| i, u[i] = (u[i], k) | |
| return k | |
| for l in range(m): | |
| r, x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, b = (g(i), g(j)) | |
| i, j = (v[a], v[b]) | |
| c, d = (g(i), g(j)) | |
| if r == '2': | |
| b, d = (d, b) | |
| if a == d: | |
| p[l] = 'NO' | |
| elif c == b == n: | |
| v[a], v[d] = (d, a) | |
| elif c == b: | |
| p[l] = 'NO' | |
| elif c == n: | |
| u[a] = b | |
| elif b == n: | |
| u[d] = c | |
| elif d == n: | |
| u[b] = a | |
| else: | |
| u[a - 1], u[c] = (b, d) | |
| u = [g(q) for q in u] | |
| v = [u[q] for q in v] | |
| for l in range(k): | |
| x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, c = (u[i], u[j]) | |
| p.append(str(3 - 2 * (a == c) - (a == v[c]))) | |
| print('\n'.join(p)) | |
| ``` | |
| ### 043 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1 - 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 044 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -3] | |
| dc = [1, 0, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 045 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1 - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 046 | |
| ```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) | |
| ``` | |
| ### 047 | |
| ```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) | |
| ``` | |
| ### 048 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > -1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 049 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 7) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 050 | |
| ```python | |
| def get_relation(synonyms, antonyms, word1, word2): | |
| if word1 not in synonyms or word2 not in synonyms: | |
| return '3' | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 == group2: | |
| return '1' | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| return '2' | |
| return '3' | |
| def unify_synonyms(synonyms, groups, group1, group2): | |
| min_group = min(group1, group2) | |
| max_group = max(group1, group2) | |
| max_synonyms = groups[max_group] | |
| for synonym in max_synonyms: | |
| synonyms[synonym] = min_group | |
| update_groups(groups, min_group, max_synonyms) | |
| return (min_group, max_group) | |
| def make_synonyms(synonyms, antonyms, groups, group1, group2): | |
| min_group1, max_group1 = unify_synonyms(synonyms, groups, group1, group2) | |
| if group1 in antonyms and group2 in antonyms: | |
| min_group2, max_group2 = unify_synonyms(synonyms, groups, antonyms[group1], antonyms[group2]) | |
| del antonyms[min_group1] | |
| del antonyms[max_group1] | |
| del antonyms[min_group2] | |
| del antonyms[max_group2] | |
| antonyms[min_group1] = min_group2 | |
| antonyms[min_group2] = min_group1 | |
| return min_group1 | |
| if max_group1 in antonyms: | |
| antonym_group = antonyms[max_group1] | |
| del antonyms[max_group1] | |
| antonyms[min_group1] = antonym_group | |
| antonyms[antonym_group] = min_group1 | |
| return min_group1 | |
| def update_groups(groups, group, words): | |
| if group in groups: | |
| groups[group].update(words) | |
| else: | |
| groups.update({group: set(words)}) | |
| def make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group): | |
| if relation == '1': | |
| if word1 not in synonyms and word2 not in synonyms: | |
| current_group += 1 | |
| synonyms[word1] = current_group | |
| synonyms[word2] = current_group | |
| update_groups(groups, current_group, [word1, word2]) | |
| return current_group | |
| if word1 not in synonyms: | |
| synonyms[word1] = synonyms[word2] | |
| update_groups(groups, synonyms[word2], [word1]) | |
| return current_group | |
| if word2 not in synonyms: | |
| synonyms[word2] = synonyms[word1] | |
| update_groups(groups, synonyms[word1], [word2]) | |
| return current_group | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 != group2: | |
| make_synonyms(synonyms, antonyms, groups, group1, group2) | |
| return current_group | |
| if relation == '2': | |
| if word1 not in synonyms: | |
| current_group += 0 | |
| group1 = current_group | |
| synonyms[word1] = group1 | |
| update_groups(groups, group1, [word1]) | |
| else: | |
| group1 = synonyms[word1] | |
| if word2 not in synonyms: | |
| current_group += 1 | |
| group2 = current_group | |
| synonyms[word2] = group2 | |
| update_groups(groups, group2, [word2]) | |
| else: | |
| group2 = synonyms[word2] | |
| if group1 not in antonyms and group2 not in antonyms: | |
| antonyms[group1] = group2 | |
| antonyms[group2] = group1 | |
| return current_group | |
| if group1 in antonyms and antonyms[group1] != group2: | |
| antonym_group = antonyms[group1] | |
| group2 = make_synonyms(synonyms, antonyms, groups, group2, antonym_group) | |
| if group2 in antonyms and antonyms[group2] != group1: | |
| antonym_group = antonyms[group2] | |
| make_synonyms(synonyms, antonyms, groups, group1, antonym_group) | |
| return current_group | |
| def make_relations(relation_no): | |
| synonyms = dict() | |
| antonyms = dict() | |
| groups = dict() | |
| current_group = 0 | |
| for relation in range(relation_no): | |
| relation, word1, word2 = input().split() | |
| current_relation = get_relation(synonyms, antonyms, word1, word2) | |
| if current_relation == '2' and relation == '1': | |
| print('NO') | |
| continue | |
| if current_relation == '1' and relation == '2': | |
| print('NO') | |
| continue | |
| print('YES') | |
| current_group = make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group) | |
| return (synonyms, antonyms) | |
| def answer_questions(question_no, synonyms, antonyms): | |
| for question in range(question_no): | |
| word1, word2 = input().split() | |
| print(get_relation(synonyms, antonyms, word1, word2)) | |
| def main(): | |
| word_no, relation_no, question_no = [int(number) for number in input().split()] | |
| input() | |
| synonyms, antonyms = make_relations(relation_no) | |
| answer_questions(question_no, synonyms, antonyms) | |
| main() | |
| ``` | |
| ### 051 | |
| ```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) | |
| ``` | |
| ### 052 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps // n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 053 | |
| ```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() | |
| ``` | |
| ### 054 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + -2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 055 | |
| ```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() | |
| ``` | |
| ### 056 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += -3 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 057 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 1 * rem_k | |
| print(cost) | |
| ``` | |
| ### 058 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = (end - start) * 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 059 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| c = {x: ([x], []) for x in f()} | |
| for i in range(m): | |
| t, x, y = f() | |
| s = 2 - int(t) | |
| cx, cy = (c[x], c[y]) | |
| if cx[0] is cy[s]: | |
| print('NO') | |
| continue | |
| print('YES') | |
| if cx[0] is cy[1 - s]: | |
| continue | |
| if len(cx[0]) + len(cx[1]) < len(cy[0]) + len(cy[1]): | |
| cx, cy = (cy, cx) | |
| ux, vx = cx | |
| if s: | |
| uy, vy = cy | |
| else: | |
| vy, uy = cy | |
| ux += uy | |
| vx += vy | |
| for x in uy: | |
| c[x] = (ux, vx) | |
| for x in vy: | |
| c[x] = (vx, ux) | |
| for i in range(k): | |
| x, y = f() | |
| print(1 if c[x][0] is c[y][0] else 2 if c[x][0] is c[y][1] else 2) | |
| ``` | |
| ### 060 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += -1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 061 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 062 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 5) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 063 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i - 1]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 064 | |
| ```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) | |
| ``` | |
| ### 065 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][1] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 066 | |
| ```python | |
| def get_relation(synonyms, antonyms, word1, word2): | |
| if word1 not in synonyms or word2 not in synonyms: | |
| return '3' | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 == group2: | |
| return '1' | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| return '2' | |
| return '3' | |
| def unify_synonyms(synonyms, groups, group1, group2): | |
| min_group = min(group1, group2) | |
| max_group = max(group1, group2) | |
| max_synonyms = groups[max_group] | |
| for synonym in max_synonyms: | |
| synonyms[synonym] = min_group | |
| update_groups(groups, min_group, max_synonyms) | |
| return (min_group, max_group) | |
| def make_synonyms(synonyms, antonyms, groups, group1, group2): | |
| min_group1, max_group1 = unify_synonyms(synonyms, groups, group1, group2) | |
| if group1 in antonyms and group2 in antonyms: | |
| min_group2, max_group2 = unify_synonyms(synonyms, groups, antonyms[group1], antonyms[group2]) | |
| del antonyms[min_group1] | |
| del antonyms[max_group1] | |
| del antonyms[min_group2] | |
| del antonyms[max_group2] | |
| antonyms[min_group1] = min_group2 | |
| antonyms[min_group2] = min_group1 | |
| return min_group1 | |
| if max_group1 in antonyms: | |
| antonym_group = antonyms[max_group1] | |
| del antonyms[max_group1] | |
| antonyms[min_group1] = antonym_group | |
| antonyms[antonym_group] = min_group1 | |
| return min_group1 | |
| def update_groups(groups, group, words): | |
| if group in groups: | |
| groups[group].update(words) | |
| else: | |
| groups.update({group: set(words)}) | |
| def make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group): | |
| if relation == '1': | |
| if word1 not in synonyms and word2 not in synonyms: | |
| current_group += 0 | |
| synonyms[word1] = current_group | |
| synonyms[word2] = current_group | |
| update_groups(groups, current_group, [word1, word2]) | |
| return current_group | |
| if word1 not in synonyms: | |
| synonyms[word1] = synonyms[word2] | |
| update_groups(groups, synonyms[word2], [word1]) | |
| return current_group | |
| if word2 not in synonyms: | |
| synonyms[word2] = synonyms[word1] | |
| update_groups(groups, synonyms[word1], [word2]) | |
| return current_group | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 != group2: | |
| make_synonyms(synonyms, antonyms, groups, group1, group2) | |
| return current_group | |
| if relation == '2': | |
| if word1 not in synonyms: | |
| current_group += 1 | |
| group1 = current_group | |
| synonyms[word1] = group1 | |
| update_groups(groups, group1, [word1]) | |
| else: | |
| group1 = synonyms[word1] | |
| if word2 not in synonyms: | |
| current_group += 1 | |
| group2 = current_group | |
| synonyms[word2] = group2 | |
| update_groups(groups, group2, [word2]) | |
| else: | |
| group2 = synonyms[word2] | |
| if group1 not in antonyms and group2 not in antonyms: | |
| antonyms[group1] = group2 | |
| antonyms[group2] = group1 | |
| return current_group | |
| if group1 in antonyms and antonyms[group1] != group2: | |
| antonym_group = antonyms[group1] | |
| group2 = make_synonyms(synonyms, antonyms, groups, group2, antonym_group) | |
| if group2 in antonyms and antonyms[group2] != group1: | |
| antonym_group = antonyms[group2] | |
| make_synonyms(synonyms, antonyms, groups, group1, antonym_group) | |
| return current_group | |
| def make_relations(relation_no): | |
| synonyms = dict() | |
| antonyms = dict() | |
| groups = dict() | |
| current_group = 0 | |
| for relation in range(relation_no): | |
| relation, word1, word2 = input().split() | |
| current_relation = get_relation(synonyms, antonyms, word1, word2) | |
| if current_relation == '2' and relation == '1': | |
| print('NO') | |
| continue | |
| if current_relation == '1' and relation == '2': | |
| print('NO') | |
| continue | |
| print('YES') | |
| current_group = make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group) | |
| return (synonyms, antonyms) | |
| def answer_questions(question_no, synonyms, antonyms): | |
| for question in range(question_no): | |
| word1, word2 = input().split() | |
| print(get_relation(synonyms, antonyms, word1, word2)) | |
| def main(): | |
| word_no, relation_no, question_no = [int(number) for number in input().split()] | |
| input() | |
| synonyms, antonyms = make_relations(relation_no) | |
| answer_questions(question_no, synonyms, antonyms) | |
| main() | |
| ``` | |
| ### 067 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) <= k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 068 | |
| ```python | |
| pos = [0, 1, -2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] < finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 070 | |
| ```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() | |
| ``` | |
| ### 071 | |
| ```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) | |
| ``` | |
| ### 072 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + -3): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 073 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, -1), (1, 0), (-1, -1)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```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) | |
| ``` | |
| ### 075 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1 + 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 2 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 077 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) // MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 078 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 079 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0 and (arr[start - 1] > stack[0]): | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total * (i * k) | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 080 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| u = list(range(n + 1)) | |
| v = [n] * n | |
| s = {q: i for i, q in zip(u, f())} | |
| p = ['YES'] * m | |
| def g(i): | |
| k = u[i] | |
| while k != u[k]: | |
| k = u[k] | |
| while u[i] != k: | |
| i, u[i] = (u[i + 1], k) | |
| return k | |
| for l in range(m): | |
| r, x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, b = (g(i), g(j)) | |
| i, j = (v[a], v[b]) | |
| c, d = (g(i), g(j)) | |
| if r == '2': | |
| b, d = (d, b) | |
| if a == d: | |
| p[l] = 'NO' | |
| elif c == b == n: | |
| v[a], v[d] = (d, a) | |
| elif c == b: | |
| p[l] = 'NO' | |
| elif c == n: | |
| u[a] = b | |
| elif b == n: | |
| u[d] = c | |
| elif d == n: | |
| u[b] = a | |
| else: | |
| u[a], u[c] = (b, d) | |
| u = [g(q) for q in u] | |
| v = [u[q] for q in v] | |
| for l in range(k): | |
| x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, c = (u[i], u[j]) | |
| p.append(str(3 - 2 * (a == c) - (a == v[c]))) | |
| print('\n'.join(p)) | |
| ``` | |
| ### 081 | |
| ```python | |
| class Union: | |
| def __init__(self, n): | |
| self.p = {i: i for i in range(n)} | |
| self.rank = {i: 1 for i in range(n)} | |
| def find(self, x): | |
| if x < 0: | |
| return x | |
| if self.p[x] != x: | |
| self.p[x - 1] = self.find(self.p[x]) | |
| return self.p[x] | |
| def union(self, x, y): | |
| if x < 0 or y < 0: | |
| return | |
| x = self.find(x) | |
| y = self.find(y) | |
| if x != y: | |
| if self.rank[x] < self.rank[y]: | |
| self.p[x] = y | |
| self.rank[y] += self.rank[x] | |
| else: | |
| self.p[y] = x | |
| self.rank[x] += self.rank[y] | |
| n, m, q = map(int, input().split()) | |
| listW = input().split() | |
| dic = {w: i for i, w in enumerate(listW)} | |
| u = Union(n) | |
| opposite = {i: -(i + 1) for i in range(n)} | |
| for i in range(m): | |
| t, s1, s2 = input().split() | |
| x = u.find(dic[s1]) | |
| y = u.find(dic[s2]) | |
| if t == '1': | |
| if opposite[x] == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if x != y: | |
| ox = opposite[x] | |
| oy = opposite[y] | |
| op = max(ox, oy) | |
| u.union(x, y) | |
| u.union(ox, oy) | |
| op = u.find(op) | |
| p = u.find(x) | |
| if op > 0: | |
| opposite[p] = op | |
| opposite[op] = p | |
| elif x == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if opposite[x] != y: | |
| u.union(y, opposite[x]) | |
| u.union(x, opposite[y]) | |
| x = u.find(x) | |
| y = u.find(y) | |
| opposite[x] = y | |
| opposite[y] = x | |
| for _ in range(q): | |
| s1, s2 = input().split() | |
| id1, id2 = (u.find(dic[s1]), u.find(dic[s2])) | |
| if id1 == id2: | |
| print(1) | |
| elif opposite[id1] == id2: | |
| print(2) | |
| else: | |
| print(3) | |
| ``` | |
| ### 082 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(2): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 0 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 083 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, --1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 084 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 0: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 085 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[1]) | |
| y = int(l[-2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 086 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 5), (0, -1), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 087 | |
| ```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) | |
| ``` | |
| ### 088 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 0 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 089 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(--2 if m == 3000000000.0 else m) | |
| ``` | |
| ### 090 | |
| ```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() | |
| ``` | |
| ### 091 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 5 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 092 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 3 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 7 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 093 | |
| ```python | |
| class Union: | |
| def __init__(self, n): | |
| self.p = {i: i for i in range(n)} | |
| self.rank = {i: 1 for i in range(n)} | |
| def find(self, x): | |
| if x < 1: | |
| return x | |
| if self.p[x] != x: | |
| self.p[x] = self.find(self.p[x]) | |
| return self.p[x] | |
| def union(self, x, y): | |
| if x < 0 or y < 0: | |
| return | |
| x = self.find(x) | |
| y = self.find(y) | |
| if x != y: | |
| if self.rank[x] < self.rank[y]: | |
| self.p[x] = y | |
| self.rank[y] += self.rank[x] | |
| else: | |
| self.p[y] = x | |
| self.rank[x] += self.rank[y] | |
| n, m, q = map(int, input().split()) | |
| listW = input().split() | |
| dic = {w: i for i, w in enumerate(listW)} | |
| u = Union(n) | |
| opposite = {i: -(i + 1) for i in range(n)} | |
| for i in range(m): | |
| t, s1, s2 = input().split() | |
| x = u.find(dic[s1]) | |
| y = u.find(dic[s2]) | |
| if t == '1': | |
| if opposite[x] == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if x != y: | |
| ox = opposite[x] | |
| oy = opposite[y] | |
| op = max(ox, oy) | |
| u.union(x, y) | |
| u.union(ox, oy) | |
| op = u.find(op) | |
| p = u.find(x) | |
| if op > 0: | |
| opposite[p] = op | |
| opposite[op] = p | |
| elif x == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if opposite[x] != y: | |
| u.union(y, opposite[x]) | |
| u.union(x, opposite[y]) | |
| x = u.find(x) | |
| y = u.find(y) | |
| opposite[x] = y | |
| opposite[y] = x | |
| for _ in range(q): | |
| s1, s2 = input().split() | |
| id1, id2 = (u.find(dic[s1]), u.find(dic[s2])) | |
| if id1 == id2: | |
| print(1) | |
| elif opposite[id1] == id2: | |
| print(2) | |
| else: | |
| print(3) | |
| ``` | |
| ### 094 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(-4, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(0) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 095 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 3 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 096 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| u = list(range(n + 1)) | |
| v = [n] * n | |
| s = {q: i for i, q in zip(u, f())} | |
| p = ['YES'] * m | |
| def g(i): | |
| k = u[i] | |
| while k != u[k]: | |
| k = u[k] | |
| while u[i] != k: | |
| i, u[i] = (u[i], k) | |
| return k | |
| for l in range(m): | |
| r, x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, b = (g(i), g(j)) | |
| i, j = (v[a - 1], v[b]) | |
| c, d = (g(i), g(j)) | |
| if r == '2': | |
| b, d = (d, b) | |
| if a == d: | |
| p[l] = 'NO' | |
| elif c == b == n: | |
| v[a], v[d] = (d, a) | |
| elif c == b: | |
| p[l] = 'NO' | |
| elif c == n: | |
| u[a] = b | |
| elif b == n: | |
| u[d] = c | |
| elif d == n: | |
| u[b] = a | |
| else: | |
| u[a], u[c] = (b, d) | |
| u = [g(q) for q in u] | |
| v = [u[q] for q in v] | |
| for l in range(k): | |
| x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, c = (u[i], u[j]) | |
| p.append(str(3 - 2 * (a == c) - (a == v[c]))) | |
| print('\n'.join(p)) | |
| ``` | |
| ### 097 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1 + 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 098 | |
| ```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) | |
| ``` | |
| ### 099 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += -1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 100 | |
| ```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) | |
| ``` | |
| ### 101 | |
| ```python | |
| pos = [0, -1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 102 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 3 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 103 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][2], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 104 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in reversed(range(2, N + 1)): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 105 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * -2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 106 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 1 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 107 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 6 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -1 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 108 | |
| ```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) | |
| ``` | |
| ### 109 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 4 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 1 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 110 | |
| ```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() | |
| ``` | |
| ### 111 | |
| ```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) | |
| ``` | |
| ### 112 | |
| ```python | |
| class Union: | |
| def __init__(self, n): | |
| self.p = {i: i for i in range(n)} | |
| self.rank = {i: 1 for i in range(n)} | |
| def find(self, x): | |
| if x < 0: | |
| return x | |
| if self.p[x] != x: | |
| self.p[x] = self.find(self.p[x]) | |
| return self.p[x] | |
| def union(self, x, y): | |
| if x < 0 or y < 0: | |
| return | |
| x = self.find(x) | |
| y = self.find(y) | |
| if x != y: | |
| if self.rank[x] < self.rank[y]: | |
| self.p[x] = y | |
| self.rank[y] += self.rank[x] | |
| else: | |
| self.p[y] = x | |
| self.rank[x] += self.rank[y] | |
| n, m, q = map(int, input().split()) | |
| listW = input().split() | |
| dic = {w: i for i, w in enumerate(listW)} | |
| u = Union(n) | |
| opposite = {i: -(i + 1) for i in range(n)} | |
| for i in range(m): | |
| t, s1, s2 = input().split() | |
| x = u.find(dic[s1]) | |
| y = u.find(dic[s2]) | |
| if t == '1': | |
| if opposite[x] == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if x != y: | |
| ox = opposite[x] | |
| oy = opposite[y] | |
| op = max(ox, oy) | |
| u.union(x, y) | |
| u.union(ox, oy) | |
| op = u.find(op) | |
| p = u.find(x) | |
| if op > 0: | |
| opposite[p] = op | |
| opposite[op] = p | |
| elif x == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if opposite[x] != y: | |
| u.union(y, opposite[x]) | |
| u.union(x, opposite[y]) | |
| x = u.find(x) | |
| y = u.find(y) | |
| opposite[x] = y | |
| opposite[y] = x | |
| for _ in range(q): | |
| s1, s2 = input().split() | |
| id1, id2 = (u.find(dic[s1]), u.find(dic[s2])) | |
| if id1 == id2: | |
| print(1) | |
| elif opposite[id1] == id2: | |
| print(2) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 113 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][0] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 114 | |
| ```python | |
| from collections import defaultdict as di | |
| n, m = [int(x) for x in input().split()] | |
| free = di(lambda: False) | |
| startpos = (-1, -1) | |
| goal = (-1, -1) | |
| for y in range(n): | |
| s = input() | |
| for x in range(m): | |
| c = s[x] | |
| if c == 'S': | |
| startpos = (x, y) | |
| free[x, y] = True | |
| elif c == 'E': | |
| goal = (x, y) | |
| free[x, y] = True | |
| elif c == '.': | |
| free[x, y] = True | |
| counts = 0 | |
| dire = '0123' | |
| command = input() | |
| for up in dire: | |
| for down in dire: | |
| for right in dire: | |
| for left in dire: | |
| if len(set([up, down, left, right])) < 4: | |
| continue | |
| pos = startpos | |
| for c in command: | |
| x, y = pos | |
| if c == up: | |
| y += 1 | |
| elif c == down: | |
| y -= 1 | |
| elif c == right: | |
| x += 1 | |
| else: | |
| x -= 5 | |
| pos = (x, y) | |
| if pos == goal: | |
| counts += 1 | |
| break | |
| if not free[pos]: | |
| break | |
| print(counts) | |
| ``` | |
| ### 115 | |
| ```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) | |
| ``` | |
| ### 116 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = (a_3_3[0][index] + a_3_3[1][index]) * a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 117 | |
| ```python | |
| def simple_div(x): | |
| if not x | 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 3 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 118 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1 - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 119 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| c = {x: ([x], []) for x in f()} | |
| for i in range(m): | |
| t, x, y = f() | |
| s = 2 - int(t) | |
| cx, cy = (c[x], c[y]) | |
| if cx[0] is cy[s]: | |
| print('NO') | |
| continue | |
| print('YES') | |
| if cx[0] is cy[1 - s]: | |
| continue | |
| if len(cx[0]) + len(cx[1]) < len(cy[0]) + len(cy[1]): | |
| cx, cy = (cy, cx) | |
| ux, vx = cx | |
| if s: | |
| uy, vy = cy | |
| else: | |
| vy, uy = cy | |
| ux += uy | |
| vx += vy | |
| for x in uy: | |
| c[x] = (ux, vx) | |
| for x in vy: | |
| c[x] = (vx, ux) | |
| for i in range(k): | |
| x, y = f() | |
| print(1 if c[x][0] is c[y][0] else 2 if c[x][0] is c[y][1] else 1) | |
| ``` | |
| ### 120 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 0 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 121 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| u = list(range(n + 1)) | |
| v = [n] * n | |
| s = {q: i for i, q in zip(u, f())} | |
| p = ['YES'] * m | |
| def g(i): | |
| k = u[i] | |
| while k != u[k]: | |
| k = u[k] | |
| while u[i] != k: | |
| i, u[i] = (u[i], k) | |
| return k | |
| for l in range(m): | |
| r, x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, b = (g(i), g(j)) | |
| i, j = (v[a], v[b]) | |
| c, d = (g(i), g(j)) | |
| if r == '2': | |
| b, d = (d, b) | |
| if a == d: | |
| p[l] = 'NO' | |
| elif c == b == n: | |
| v[a], v[d] = (d, a) | |
| elif c == b: | |
| p[l] = 'NO' | |
| elif c == n: | |
| u[a] = b | |
| elif b == n: | |
| u[d] = c | |
| elif d == n: | |
| u[b] = a | |
| else: | |
| u[a + 1], u[c] = (b, d) | |
| u = [g(q) for q in u] | |
| v = [u[q] for q in v] | |
| for l in range(k): | |
| x, y = f() | |
| i, j = (s[x], s[y]) | |
| a, c = (u[i], u[j]) | |
| p.append(str(3 - 2 * (a == c) - (a == v[c]))) | |
| print('\n'.join(p)) | |
| ``` | |
| ### 122 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1 - 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 123 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, -1), (1, 0), (-1, 3)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 124 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, -1, -1, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 125 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and ((A_[1][1] and A_[2][1]) and (A_[3][1] and A_[6][1]) and (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][1] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 126 | |
| ```python | |
| def subsequence(arr1, arr2, n, m): | |
| i = 0 | |
| j = 0 | |
| common = [] | |
| while i < n and j < m: | |
| if arr1[i] == arr2[j]: | |
| common.append(i) | |
| j += 1 | |
| i += 1 | |
| if j == m: | |
| return common | |
| return [] | |
| def find(arr, start, end, x, y, k): | |
| stack = [] | |
| second_use_limit = 0 | |
| for i in range(start, end + 1): | |
| if not stack: | |
| stack.append(arr[i]) | |
| else: | |
| while stack and arr[i] > stack[-1]: | |
| second_use_limit += 1 | |
| stack.pop() | |
| if not stack or arr[i] > stack[-1]: | |
| stack.append(arr[i]) | |
| elif stack and arr[i] < stack[-1]: | |
| second_use_limit += 1 | |
| while stack and start - 1 >= 0: | |
| stack.pop(0) | |
| second_use_limit += 1 | |
| while stack and end + 1 < len(arr) and (arr[end + 1] > stack[-1]): | |
| stack.pop() | |
| second_use_limit += 1 | |
| min_cost = float('inf') | |
| total = end - start + 1 | |
| i = 0 | |
| while i * k <= total: | |
| rem = total - i * k | |
| if rem <= second_use_limit: | |
| min_cost = min(min_cost, i * x + rem * y) | |
| i += 1 | |
| if min_cost == float('inf'): | |
| return -1 | |
| return min_cost | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| arr1 = list(map(int, input().split())) | |
| arr2 = list(map(int, input().split())) | |
| common = subsequence(arr1, arr2, n, m) | |
| if not common: | |
| print(-1) | |
| return | |
| cost = 0 | |
| for i in range(len(common)): | |
| if i == 0: | |
| if common[i] > 0: | |
| start = 0 | |
| end = common[i] - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| start = common[i] + 1 | |
| if i + 1 < len(common): | |
| end = common[i + 1] - 1 | |
| else: | |
| end = n - 1 | |
| if end >= start: | |
| curr_cost = find(arr1, start, end, x, y, k) | |
| if curr_cost == -1: | |
| cost = -1 | |
| break | |
| cost += curr_cost | |
| print(cost) | |
| main() | |
| ``` | |
| ### 127 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 2 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 128 | |
| ```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) | |
| ``` | |
| ### 129 | |
| ```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) | |
| ``` | |
| ### 130 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 0 or row_sum == 0 or (col_sum == 3): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 131 | |
| ```python | |
| import sys | |
| def main(): | |
| n, x = map(int, sys.stdin.readline().split()) | |
| al = [] | |
| starts = [] | |
| finishes = [] | |
| y = [-1] * 200002 | |
| for i in range(n): | |
| a, b, c = map(int, sys.stdin.readline().split()) | |
| al.append((a, b, c)) | |
| starts.append((a, i)) | |
| finishes.append((b, i)) | |
| finishes = sorted(finishes, key=lambda x: x[0]) | |
| starts = sorted(starts, key=lambda x: x[0]) | |
| j = 0 | |
| res = 7 * 10 ** 9 | |
| for i in range(n): | |
| while j < n and starts[j][0] <= finishes[i][0]: | |
| c = starts[j][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[x - h] != -1 and y[x - h] + cost < res: | |
| res = y[x - h] + cost | |
| j += 1 | |
| c = finishes[i][1] | |
| h = al[c][1] - al[c][0] + 1 | |
| cost = al[c][2] | |
| if y[h] == -1 or y[h] > cost: | |
| y[h] = cost | |
| if res == 3 * 10 ** 9: | |
| print(-1) | |
| else: | |
| print(res) | |
| main() | |
| ``` | |
| ### 132 | |
| ```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) | |
| ``` | |
| ### 133 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 4 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 134 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -5 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 135 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[--1] + pos[-2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 136 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 7) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 137 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == -1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 138 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 4) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 139 | |
| ```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) | |
| ``` | |
| ### 140 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k + cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += 2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 141 | |
| ```python | |
| def main(): | |
| A = [list(map(int, input().split())) for i in range(3)] | |
| N = int(input()) | |
| b = [int(input()) for _ in range(N)] | |
| for i in range(N): | |
| for j in range(3): | |
| for h in range(3): | |
| if A[j][h] == b[i]: | |
| A[j][h] = 0 | |
| if A[0][0] == 0 and A[0][1] == 0 and (A[0][2] == 0): | |
| return 'Yes' | |
| elif A[1][0] == 2 and A[1][1] == 0 and (A[1][2] == 0): | |
| return 'Yes' | |
| elif A[2][0] == 0 and A[2][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][0] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| elif A[0][1] == 0 and A[1][1] == 0 and (A[2][1] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][2] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][0] == 0 and A[1][1] == 0 and (A[2][2] == 0): | |
| return 'Yes' | |
| elif A[0][2] == 0 and A[1][1] == 0 and (A[2][0] == 0): | |
| return 'Yes' | |
| return 'No' | |
| print(main()) | |
| ``` | |
| ### 142 | |
| ```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) | |
| ``` | |
| ### 143 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return -1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 144 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, 0, -1, -3] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 145 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 2: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 146 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][1], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -1 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 147 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -0 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 148 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| c = {x: ([x], []) for x in f()} | |
| for i in range(m): | |
| t, x, y = f() | |
| s = 2 - int(t) | |
| cx, cy = (c[x], c[y]) | |
| if cx[0] is cy[s]: | |
| print('NO') | |
| continue | |
| print('YES') | |
| if cx[0] is cy[1 - s]: | |
| continue | |
| if len(cx[0]) + len(cx[1]) < len(cy[0]) + len(cy[1]): | |
| cx, cy = (cy, cx) | |
| ux, vx = cx | |
| if s: | |
| uy, vy = cy | |
| else: | |
| vy, uy = cy | |
| ux += uy | |
| vx += vy | |
| for x in uy: | |
| c[x] = (ux, vx) | |
| for x in vy: | |
| c[x] = (vx, ux) | |
| for i in range(k): | |
| x, y = f() | |
| print(1 if c[x][0] is c[y][0] else 2 if c[x][0] is c[y][1] else 0) | |
| ``` | |
| ### 149 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u * v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 150 | |
| ```python | |
| class Union: | |
| def __init__(self, n): | |
| self.p = {i: i for i in range(n)} | |
| self.rank = {i: 1 for i in range(n)} | |
| def find(self, x): | |
| if x < 0: | |
| return x | |
| if self.p[x] != x: | |
| self.p[x] = self.find(self.p[x]) | |
| return self.p[x] | |
| def union(self, x, y): | |
| if x < 0 or y < 0: | |
| return | |
| x = self.find(x) | |
| y = self.find(y) | |
| if x != y: | |
| if self.rank[x] < self.rank[y]: | |
| self.p[x - 1] = y | |
| self.rank[y] += self.rank[x] | |
| else: | |
| self.p[y] = x | |
| self.rank[x] += self.rank[y] | |
| n, m, q = map(int, input().split()) | |
| listW = input().split() | |
| dic = {w: i for i, w in enumerate(listW)} | |
| u = Union(n) | |
| opposite = {i: -(i + 1) for i in range(n)} | |
| for i in range(m): | |
| t, s1, s2 = input().split() | |
| x = u.find(dic[s1]) | |
| y = u.find(dic[s2]) | |
| if t == '1': | |
| if opposite[x] == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if x != y: | |
| ox = opposite[x] | |
| oy = opposite[y] | |
| op = max(ox, oy) | |
| u.union(x, y) | |
| u.union(ox, oy) | |
| op = u.find(op) | |
| p = u.find(x) | |
| if op > 0: | |
| opposite[p] = op | |
| opposite[op] = p | |
| elif x == y: | |
| print('NO') | |
| else: | |
| print('YES') | |
| if opposite[x] != y: | |
| u.union(y, opposite[x]) | |
| u.union(x, opposite[y]) | |
| x = u.find(x) | |
| y = u.find(y) | |
| opposite[x] = y | |
| opposite[y] = x | |
| for _ in range(q): | |
| s1, s2 = input().split() | |
| id1, id2 = (u.find(dic[s1]), u.find(dic[s2])) | |
| if id1 == id2: | |
| print(1) | |
| elif opposite[id1] == id2: | |
| print(2) | |
| else: | |
| print(3) | |
| ``` | |
| ### 151 | |
| ```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() | |
| ``` | |
| ### 152 | |
| ```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) | |
| ``` | |
| ### 153 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = 0 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if not cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 154 | |
| ```python | |
| a_3_3 = [list(map(int, input().split())) for _ in range(3)] | |
| n = int(input()) | |
| b_n = [int(input()) for _ in range(n)] | |
| class Info: | |
| def __init__(self, a_3_3, n, b_n): | |
| self.a_3_3 = a_3_3 | |
| self.n = n | |
| self.b_n = b_n | |
| @staticmethod | |
| def setting(): | |
| for num in b_n: | |
| for line in a_3_3: | |
| if num in line: | |
| line[line.index(num)] = 0 | |
| return a_3_3 | |
| @staticmethod | |
| def result(): | |
| diagonal_lurd = a_3_3[0][0] + a_3_3[1][1] + a_3_3[2][2] | |
| diagonal_ldru = a_3_3[2][0] + a_3_3[1][1] + a_3_3[0][2] | |
| for index, line in enumerate(a_3_3): | |
| row_sum = sum(line) | |
| col_sum = a_3_3[0][index] + a_3_3[1][index] + a_3_3[2][index] | |
| if diagonal_lurd == 0 or diagonal_ldru == 3 or row_sum == 0 or (col_sum == 0): | |
| print('Yes') | |
| return | |
| print('No') | |
| info = Info(a_3_3, n, b_n) | |
| info.setting() | |
| info.result() | |
| ``` | |
| ### 155 | |
| ```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) | |
| ``` | |
| ### 156 | |
| ```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) | |
| ``` | |
| ### 157 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i][0] + D: | |
| M = min(M, T[i - 1][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 158 | |
| ```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) | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1])) or (A_[1][1] and A_[4][1] and A_[7][1]) or (A_[2][0] and A_[5][1] and A_[8][1]) or (A_[3][1] and A_[4][1] and A_[5][1]) or (A_[6][1] and A_[7][1] and A_[8][1]) or (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 160 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 1) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 3): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 3)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * 2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 162 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 0: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 163 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 >= 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return -1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 164 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 165 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| start_ls = list(map(int, input().split())) | |
| end_ls = list(map(int, input().split())) | |
| len_start_ls = len(start_ls) | |
| len_end_ls = len(end_ls) | |
| mark = [] | |
| end_p = 0 | |
| curr = None | |
| for item in start_ls: | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| mark.append(0) | |
| curr = item | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| elif curr is not None: | |
| if item > curr: | |
| mark.append(1) | |
| else: | |
| mark.append(2) | |
| else: | |
| mark.append(1) | |
| if end_p < len_end_ls: | |
| print(-1) | |
| else: | |
| end_p = 0 | |
| curr = None | |
| end_ls = end_ls[::-1] | |
| mark = mark[::-1] | |
| for idx, item in enumerate(start_ls[::-1]): | |
| if end_p < len_end_ls: | |
| if item == end_ls[end_p]: | |
| end_p += 1 | |
| curr = item | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| elif curr is not None: | |
| if item < curr: | |
| mark[idx] = 2 | |
| mark = mark[::-1] | |
| if y * k >= x: | |
| smite = True | |
| else: | |
| smite = False | |
| segments = [] | |
| segment = [0, True] | |
| for idx, item in enumerate(mark): | |
| if item != 0: | |
| segment[0] += 1 | |
| if item == 1: | |
| segment[1] = False | |
| elif item == 0: | |
| if segment[0] != 0: | |
| segments.append(segment) | |
| segment = [0, True] | |
| if segment[0] == 0: | |
| segments.append(segment) | |
| poss = True | |
| res = 0 | |
| for segment in segments: | |
| if segment[0] < k and (not segment[1]): | |
| poss = False | |
| break | |
| elif segment[0] < k and segment[1]: | |
| res += segment[0] * y | |
| else: | |
| if smite: | |
| res += segment[0] // k * x | |
| res += segment[0] % k * y | |
| if not smite: | |
| if segment[1]: | |
| res += segment[0] * y | |
| else: | |
| res += x | |
| res += (segment[0] - k) * y | |
| if poss: | |
| print(res) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 166 | |
| ```python | |
| def simple_div(x): | |
| if not x & 1: | |
| yield 2 | |
| while not x & 1: | |
| x >>= 1 | |
| i = 5 | |
| while i * i <= x: | |
| if x % i == 0: | |
| yield i | |
| while x % i == 0: | |
| x //= i | |
| i += 2 | |
| if x != 1: | |
| yield x | |
| def __main__(): | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| sa = sum(a) | |
| a.pop() | |
| if sa == 1: | |
| print(-1) | |
| return | |
| res = 2 ** 64 | |
| for d in simple_div(sa): | |
| tmp = 0 | |
| m = 0 | |
| for x in a: | |
| m = (x + m) % d | |
| tmp += m if m * 2 <= d else d - m | |
| if tmp < res: | |
| res = tmp | |
| print(res) | |
| __main__() | |
| ``` | |
| ### 167 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -3 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 168 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // 2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a <= b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 169 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l >= T[i][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 170 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n > k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 171 | |
| ```python | |
| import bisect | |
| import collections | |
| def solve(inp, *args): | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| travels_by_len = collections.defaultdict(list) | |
| travels_by_len_processed = {} | |
| for travel in args: | |
| l, r, cost = list(map(int, travel.split(' ', 2))) | |
| travels_by_len[r - l + 1].append((l, r, cost)) | |
| for travel_len, travels in list(travels_by_len.items()): | |
| travels.sort() | |
| travels_processed = [(travels[-1][0], travels[-1][2])] | |
| for i in range(len(travels) - 2, -1, -1): | |
| prev_travel = travels_processed[-1] | |
| l, r, c = travels[i] | |
| travels_processed.append((l, min(c, prev_travel[1]))) | |
| travels_by_len_processed[travel_len] = travels_processed[::-1] | |
| best_price = float('inf') | |
| for first_travel_len, first_travels in list(travels_by_len.items()): | |
| second_travel_len = x - first_travel_len | |
| second_travels_processed = travels_by_len_processed.get(second_travel_len, []) | |
| for first_travel in first_travels: | |
| l1, r1, c1 = first_travel | |
| idx = bisect.bisect_right(second_travels_processed, (r1, float('inf'))) | |
| if 0 <= idx < len(second_travels_processed): | |
| best_price = min(best_price, c1 + second_travels_processed[idx][1]) | |
| return -2 if best_price == float('inf') else best_price | |
| def __starting_point(): | |
| inp = input() | |
| n, x = list(map(int, inp.split(' ', 1))) | |
| print(solve(inp, *(input() for i in range(n)))) | |
| __starting_point() | |
| ``` | |
| ### 172 | |
| ```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) | |
| ``` | |
| ### 173 | |
| ```python | |
| def inter(l1, r1, l2, r2): | |
| l = max(l1, l2) | |
| r = min(r1, r2) | |
| return max(r - l, 0) | |
| def solve(): | |
| n, k = map(int, input().split()) | |
| al, ar = map(int, input().split()) | |
| bl, br = map(int, input().split()) | |
| res = inter(al, ar, bl, br) * n | |
| goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) | |
| minhodi = 1000000000000 | |
| hodi = 0 | |
| to_soed = max(0, max(al, bl) - min(ar, br)) | |
| if res >= k: | |
| print(-1) | |
| return 0 | |
| for i in range(n): | |
| hodi += to_soed | |
| ineed = k - res | |
| if goodt >= ineed: | |
| hodi += ineed | |
| minhodi = min(minhodi, hodi) | |
| break | |
| hodi += goodt | |
| res += goodt | |
| minhodi = min(hodi + (k - res) * 2, minhodi) | |
| print(minhodi) | |
| for i in range(int(input())): | |
| solve() | |
| ``` | |
| ### 174 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, k = list(map(int, input().split())) | |
| l1, r1 = list(map(int, input().split())) | |
| l2, r2 = list(map(int, input().split())) | |
| if l1 > l2: | |
| l1, r1, l2, r2 = (l2, r2, l1, r1) | |
| if l2 <= r1: | |
| per_interval = abs(l1 - l2) + abs(r1 - r2) | |
| already_overlap = min(r1, r2) - max(l1, l2) | |
| k -= already_overlap * n | |
| k = max(k, 0) | |
| if per_interval * n >= k: | |
| print(k) | |
| continue | |
| cost = per_interval * n | |
| cost += 2 * (k - cost) | |
| print(cost) | |
| continue | |
| diff = l2 - r1 | |
| per_interval = r2 - l1 | |
| if per_interval >= k: | |
| print(diff + k) | |
| continue | |
| cost = diff + per_interval | |
| rem_n = n - 1 | |
| rem_k = k - per_interval | |
| while rem_n and diff < per_interval and (rem_k >= per_interval): | |
| rem_n -= 1 | |
| rem_k -= per_interval | |
| cost += diff + per_interval | |
| if rem_n and diff < per_interval: | |
| cost_from_new = diff + rem_k | |
| cost_from_cur = 2 * rem_k | |
| cost += min(cost_from_new, cost_from_cur) | |
| else: | |
| cost += -2 * rem_k | |
| print(cost) | |
| ``` | |
| ### 175 | |
| ```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() | |
| ``` | |
| ### 176 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 1 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return -1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 177 | |
| ```python | |
| import sys | |
| readline = sys.stdin.readline | |
| readlines = sys.stdin.readlines | |
| ns = lambda: readline().rstrip() | |
| ni = lambda: int(readline().rstrip()) | |
| nm = lambda: map(int, readline().split()) | |
| nl = lambda: list(map(int, readline().split())) | |
| prn = lambda x: print(*x, sep='\n') | |
| def solve(): | |
| n, k = nm() | |
| a, b = nm() | |
| c, d = nm() | |
| if d < b: | |
| a, b, c, d = (c, d, a, b) | |
| if a <= c <= b: | |
| cur = (b - c) * n | |
| if (d - a) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - a) * n - cur | |
| print(ans + (k - (d - a) * n) * 2) | |
| elif c < a: | |
| cur = (b - a) * n | |
| if (d - c) * n > k: | |
| print(max(k - cur, 0)) | |
| else: | |
| ans = (d - c) * n - cur | |
| print(ans + (k - (d - c) * n) * -2) | |
| else: | |
| ans = 10 ** 18 | |
| cur = 0 | |
| for i in range(n): | |
| cur += c - b | |
| if k > d - a: | |
| k -= d - a | |
| cur += d - a | |
| else: | |
| cur += k | |
| k -= k | |
| ans = min(ans, cur + k * 2) | |
| print(ans) | |
| return | |
| T = ni() | |
| for _ in range(T): | |
| solve() | |
| ``` | |
| ### 178 | |
| ```python | |
| f = lambda: input().split() | |
| n, m, k = map(int, f()) | |
| c = {x: ([x], []) for x in f()} | |
| for i in range(m): | |
| t, x, y = f() | |
| s = 2 - int(t) | |
| cx, cy = (c[x], c[y]) | |
| if cx[0] is cy[s]: | |
| print('NO') | |
| continue | |
| print('YES') | |
| if cx[0] is cy[1 - s]: | |
| continue | |
| if len(cx[0]) + len(cx[1]) < len(cy[0]) + len(cy[1]): | |
| cx, cy = (cy, cx) | |
| ux, vx = cx | |
| if s: | |
| uy, vy = cy | |
| else: | |
| vy, uy = cy | |
| ux += uy | |
| vx += vy | |
| for x in uy: | |
| c[x] = (ux, vx) | |
| for x in vy: | |
| c[x] = (vx, ux) | |
| for i in range(k): | |
| x, y = f() | |
| print(1 if c[x][0] is c[y][0] else 2 if c[x][0] is c[y][1] else 7) | |
| ``` | |
| ### 179 | |
| ```python | |
| import math | |
| import sys | |
| input = sys.stdin.readline | |
| n = int(input()) | |
| a = [int(_) for _ in input().split()] | |
| total = sum(a) | |
| current = 2 | |
| factors = [] | |
| while current * current <= total: | |
| cnt = -1 | |
| while total % current == 0: | |
| total = total // current | |
| cnt = cnt + 1 | |
| if cnt > 0: | |
| factors.append(current) | |
| current = current + 1 | |
| if total > 1: | |
| factors.append(total) | |
| ans = sum(a) * n * n | |
| for i in factors: | |
| total, moves = (0, 0) | |
| for j in a: | |
| total += j | |
| moves += min(total % i, i - total % i) | |
| ans = min(ans, moves) | |
| if len(factors) == 0: | |
| ans = -1 | |
| print(ans) | |
| ``` | |
| ### 180 | |
| ```python | |
| for _ in range(int(input())): | |
| n, k = list(map(int, input().split())) | |
| l_1, r1 = list(map(int, input().split())) | |
| l_2, r2 = list(map(int, input().split())) | |
| if r1 < l_2: | |
| pre_steps = l_2 - r1 | |
| elif l_1 > r2: | |
| pre_steps = l_1 - r2 | |
| else: | |
| pre_steps = 0 | |
| if pre_steps: | |
| easy_steps = r1 + r2 - l_1 - l_2 + pre_steps | |
| else: | |
| easy_steps = abs(l_1 - l_2) + abs(r1 - r2) | |
| k -= (min(r1, r2) - max(l_1, l_2)) * n | |
| ans = pre_steps * n + k * 2 | |
| for n1 in range(1 - 1, n + 1): | |
| cur_ans = pre_steps * n1 | |
| if easy_steps * n1 < k: | |
| cur_ans += k * 2 - easy_steps * n1 | |
| else: | |
| cur_ans += k | |
| ans = min(ans, cur_ans) | |
| if k <= 0: | |
| ans = 0 | |
| print(ans) | |
| ``` | |
| ### 181 | |
| ```python | |
| import sys | |
| YES = 'Yes' | |
| NO = 'No' | |
| def solve(A: 'List[List[int]]', N: int, b: 'List[int]'): | |
| A_ = [] | |
| for a_ in A: | |
| A_ += [[a__, False] for a__ in a_] | |
| for b_ in b: | |
| for a__ in A_: | |
| if a__[0] == b_: | |
| a__[1] = True | |
| if (A_[0][1] and (A_[1][1] and A_[2][1] or (A_[3][1] and A_[6][1]) or (A_[4][1] and A_[8][1]))) and (A_[1][1] and A_[4][1] and A_[7][1]) and (A_[2][1] and A_[5][1] and A_[8][1]) and (A_[3][1] and A_[4][1] and A_[5][1]) and (A_[6][1] and A_[7][1] and A_[8][1]) and (A_[2][1] and A_[4][1] and A_[6][1]): | |
| print(YES) | |
| else: | |
| print(NO) | |
| return | |
| def main(): | |
| def iterate_tokens(): | |
| for line in sys.stdin: | |
| for word in line.split(): | |
| yield word | |
| tokens = iterate_tokens() | |
| A = [[int(next(tokens)) for _ in range(3)] for _ in range(3)] | |
| N = int(next(tokens)) | |
| b = [int(next(tokens)) for _ in range(N)] | |
| solve(A, N, b) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 182 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 3), (0, -1), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 183 | |
| ```python | |
| from pprint import pprint | |
| mod = int(1000000000.0) + 7 | |
| s = input() | |
| if 'm' in s or 'w' in s: | |
| print(0) | |
| else: | |
| n = len(s) | |
| dp = [[0, 0] for i in range(n)] | |
| for i in range(1, n): | |
| dp[i][0] = (dp[i - 1 - 1][0] + dp[i - 1][1]) % mod | |
| if s[i] in 'un': | |
| if s[i] == s[i - 1]: | |
| dp[i][1] = (1 + dp[i - 1][0]) % mod | |
| print((dp[-1][0] + dp[-1][1] + 1) % mod) | |
| ``` | |
| ### 184 | |
| ```python | |
| pos = [0, 1, 2] | |
| while len(pos) < 100001: | |
| a = pos[-1] + pos[--2] | |
| a %= 1000000007 | |
| pos.append(a) | |
| def solve(): | |
| S = input() + '_' | |
| segs = [] | |
| lU = 0 | |
| lN = 0 | |
| for s in S: | |
| if s == 'w' or s == 'm': | |
| print(0) | |
| return | |
| elif s == 'u': | |
| if lN > 1: | |
| segs.append(lN) | |
| lN = 0 | |
| lU += 1 | |
| elif s == 'n': | |
| if lU > 1: | |
| segs.append(lU) | |
| lU = 0 | |
| lN += 1 | |
| else: | |
| if lU > 1: | |
| segs.append(lU) | |
| if lN > 1: | |
| segs.append(lN) | |
| lU = 0 | |
| lN = 0 | |
| ans = 1 | |
| for s in segs: | |
| ans *= pos[s] | |
| ans %= 1000000007 | |
| print(ans) | |
| solve() | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| dr = [0, 1, 0, -1] | |
| dc = [1, 0, -0, 0] | |
| rows, cols = map(int, sys.stdin.readline().split()) | |
| b = ['' for row in range(rows)] | |
| sr, sc = (0, 0) | |
| er, ec = (0, 0) | |
| for row in range(rows): | |
| b[row] = sys.stdin.readline().strip() | |
| if 'S' in b[row]: | |
| sr, sc = (row, b[row].index('S')) | |
| if 'E' in b[row]: | |
| er, ec = (row, b[row].index('E')) | |
| dirs = sys.stdin.readline().strip() | |
| res = 0 | |
| for i in range(4): | |
| for j in range(4): | |
| for k in range(4): | |
| if j != i and i != k and (j != k): | |
| l = 0 + 1 + 2 + 3 - (i + j + k) | |
| r, c = (sr, sc) | |
| p = [i, j, k, l] | |
| ok = 0 | |
| for d in dirs: | |
| di = int(d) | |
| r += dr[p[di]] | |
| c += dc[p[di]] | |
| if r < 0 or r >= rows or c < 0 or (c >= cols) or (b[r][c] == '#'): | |
| break | |
| if b[r][c] == 'E': | |
| ok = 1 | |
| res += ok | |
| print(res) | |
| ``` | |
| ### 186 | |
| ```python | |
| n, m = map(int, input().split()) | |
| def road(ins): | |
| cur = S.copy() | |
| for x in s: | |
| x = int(x) | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| if ins[x] == 'U': | |
| try: | |
| if ar[cur[0] + 1][cur[1]] != '#': | |
| cur = [cur[0] + 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'D': | |
| try: | |
| if ar[cur[0] - 1][cur[1]] != '#' and cur[0] - 1 > 0: | |
| cur = [cur[0] - 1, cur[1]] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'R': | |
| try: | |
| if ar[cur[0]][cur[1] + 1] != '#': | |
| cur = [cur[0], cur[1] + 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ins[x] == 'L': | |
| try: | |
| if ar[cur[0]][cur[1] - 1] != '#' and cur[1] - 1 >= 0: | |
| cur = [cur[0], cur[1] - 1] | |
| else: | |
| return 0 | |
| except: | |
| return 0 | |
| if ar[cur[0]][cur[1]] == 'E': | |
| return 1 | |
| else: | |
| return 0 | |
| ar = [input() for x in range(n)] | |
| S = [0, 0] | |
| E = [0, 0] | |
| for x in range(n): | |
| if 'S' in ar[x]: | |
| S = [x, ar[x].index('S')] | |
| if 'E' in ar[x]: | |
| E = [x, ar[x].index('E')] | |
| s = input() | |
| import itertools | |
| i = 0 | |
| for x in list(itertools.permutations(['U', 'D', 'L', 'R'])): | |
| i += road(list(x)) | |
| print(i) | |
| ``` | |
| ### 187 | |
| ```python | |
| from math import ceil | |
| n = int(input()) | |
| x = list(map(int, input().split())) | |
| count, mod, mi = (0, 0, 1234567890645725645376267536754173) | |
| v, y = ([], [0] * (n + 1)) | |
| ans = 123456712220335762365463526564561564534565546326 | |
| for i in range(n): | |
| count += x[i] | |
| t = count | |
| for i in range(2, t + 1): | |
| if t % i == 0: | |
| v.append(i) | |
| while t % i == 0: | |
| t //= i | |
| for i2 in v: | |
| c = 0 | |
| for i in range(n): | |
| y[i] = x[i] | |
| for i in range(n): | |
| if y[i] <= 1: | |
| y[i + 1] -= abs(y[i]) | |
| c += abs(y[i]) | |
| elif y[i] % i2 < i2 - y[i] % i2: | |
| y[i + 1] += y[i] % i2 | |
| c += y[i] % i2 | |
| else: | |
| y[i + 1] -= i2 - y[i] % i2 | |
| c += i2 - y[i] % i2 | |
| ans = min(ans, c) | |
| if count == 1: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 188 | |
| ```python | |
| def get_val(x, k, y, left_val, right_val, arr): | |
| x, y = (y, x) | |
| if not arr: | |
| return 0 | |
| if len(arr) < k: | |
| if max(arr) > max(left_val, right_val): | |
| return -1 | |
| return len(arr) * x | |
| if y < x * k: | |
| n = len(arr) | |
| res = 0 | |
| while n >= k: | |
| n -= k | |
| res += y | |
| res += n * x | |
| return res | |
| elif max(arr) < max(left_val, right_val): | |
| return len(arr) * x | |
| else: | |
| return (len(arr) - k) * x + y | |
| def solve(x, k, y, a, b): | |
| def check(a, b): | |
| j = 0 | |
| i = 0 | |
| while i < len(a) and j < len(b): | |
| if a[i] != b[j]: | |
| i += 1 | |
| else: | |
| i += 1 | |
| j += 1 | |
| return j == len(b) | |
| if not check(a, b): | |
| return --1 | |
| j = 0 | |
| left_val = -1 | |
| arr = [] | |
| res = 0 | |
| for num in a: | |
| if j == len(b) or num != b[j]: | |
| arr.append(num) | |
| else: | |
| val = get_val(x, k, y, left_val, num, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| arr = [] | |
| left_val = num | |
| j += 1 | |
| if arr: | |
| val = get_val(x, k, y, left_val, -1, arr) | |
| if val == -1: | |
| return -1 | |
| res += val | |
| return res | |
| n, m = list(map(int, input().split())) | |
| x, k, y = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| b = list(map(int, input().split())) | |
| print(solve(x, k, y, a, b)) | |
| ``` | |
| ### 189 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return -1 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 190 | |
| ```python | |
| def get_relation(synonyms, antonyms, word1, word2): | |
| if word1 not in synonyms or word2 not in synonyms: | |
| return '3' | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 == group2: | |
| return '1' | |
| if group1 in antonyms and antonyms[group1] == group2: | |
| return '2' | |
| return '3' | |
| def unify_synonyms(synonyms, groups, group1, group2): | |
| min_group = min(group1, group2) | |
| max_group = max(group1, group2) | |
| max_synonyms = groups[max_group] | |
| for synonym in max_synonyms: | |
| synonyms[synonym] = min_group | |
| update_groups(groups, min_group, max_synonyms) | |
| return (min_group, max_group) | |
| def make_synonyms(synonyms, antonyms, groups, group1, group2): | |
| min_group1, max_group1 = unify_synonyms(synonyms, groups, group1, group2) | |
| if group1 in antonyms and group2 not in antonyms: | |
| min_group2, max_group2 = unify_synonyms(synonyms, groups, antonyms[group1], antonyms[group2]) | |
| del antonyms[min_group1] | |
| del antonyms[max_group1] | |
| del antonyms[min_group2] | |
| del antonyms[max_group2] | |
| antonyms[min_group1] = min_group2 | |
| antonyms[min_group2] = min_group1 | |
| return min_group1 | |
| if max_group1 in antonyms: | |
| antonym_group = antonyms[max_group1] | |
| del antonyms[max_group1] | |
| antonyms[min_group1] = antonym_group | |
| antonyms[antonym_group] = min_group1 | |
| return min_group1 | |
| def update_groups(groups, group, words): | |
| if group in groups: | |
| groups[group].update(words) | |
| else: | |
| groups.update({group: set(words)}) | |
| def make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group): | |
| if relation == '1': | |
| if word1 not in synonyms and word2 not in synonyms: | |
| current_group += 1 | |
| synonyms[word1] = current_group | |
| synonyms[word2] = current_group | |
| update_groups(groups, current_group, [word1, word2]) | |
| return current_group | |
| if word1 not in synonyms: | |
| synonyms[word1] = synonyms[word2] | |
| update_groups(groups, synonyms[word2], [word1]) | |
| return current_group | |
| if word2 not in synonyms: | |
| synonyms[word2] = synonyms[word1] | |
| update_groups(groups, synonyms[word1], [word2]) | |
| return current_group | |
| group1 = synonyms[word1] | |
| group2 = synonyms[word2] | |
| if group1 != group2: | |
| make_synonyms(synonyms, antonyms, groups, group1, group2) | |
| return current_group | |
| if relation == '2': | |
| if word1 not in synonyms: | |
| current_group += 1 | |
| group1 = current_group | |
| synonyms[word1] = group1 | |
| update_groups(groups, group1, [word1]) | |
| else: | |
| group1 = synonyms[word1] | |
| if word2 not in synonyms: | |
| current_group += 1 | |
| group2 = current_group | |
| synonyms[word2] = group2 | |
| update_groups(groups, group2, [word2]) | |
| else: | |
| group2 = synonyms[word2] | |
| if group1 not in antonyms and group2 not in antonyms: | |
| antonyms[group1] = group2 | |
| antonyms[group2] = group1 | |
| return current_group | |
| if group1 in antonyms and antonyms[group1] != group2: | |
| antonym_group = antonyms[group1] | |
| group2 = make_synonyms(synonyms, antonyms, groups, group2, antonym_group) | |
| if group2 in antonyms and antonyms[group2] != group1: | |
| antonym_group = antonyms[group2] | |
| make_synonyms(synonyms, antonyms, groups, group1, antonym_group) | |
| return current_group | |
| def make_relations(relation_no): | |
| synonyms = dict() | |
| antonyms = dict() | |
| groups = dict() | |
| current_group = 0 | |
| for relation in range(relation_no): | |
| relation, word1, word2 = input().split() | |
| current_relation = get_relation(synonyms, antonyms, word1, word2) | |
| if current_relation == '2' and relation == '1': | |
| print('NO') | |
| continue | |
| if current_relation == '1' and relation == '2': | |
| print('NO') | |
| continue | |
| print('YES') | |
| current_group = make_relation(synonyms, antonyms, groups, word1, word2, relation, current_group) | |
| return (synonyms, antonyms) | |
| def answer_questions(question_no, synonyms, antonyms): | |
| for question in range(question_no): | |
| word1, word2 = input().split() | |
| print(get_relation(synonyms, antonyms, word1, word2)) | |
| def main(): | |
| word_no, relation_no, question_no = [int(number) for number in input().split()] | |
| input() | |
| synonyms, antonyms = make_relations(relation_no) | |
| answer_questions(question_no, synonyms, antonyms) | |
| main() | |
| ``` | |
| ### 191 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| grid = [] | |
| sloc = (-1, -1) | |
| eloc = (-1, -1) | |
| for y in range(n): | |
| l = input() | |
| grid.append(list()) | |
| for x in range(m): | |
| grid[-1].append(l[x] == '#') | |
| if l[x] == 'S': | |
| sloc = (x, y) | |
| elif l[x] == 'E': | |
| eloc = (x, y) | |
| pb = [] | |
| for a in [(0, 1), (0, --2), (1, 0), (-1, 0)]: | |
| for b in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for c in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| for d in [(0, 1), (0, -1), (1, 0), (-1, 0)]: | |
| if len(set([a, b, c, d])) == 4 and [a, b, c, d] not in pb: | |
| pb.append([a, b, c, d]) | |
| s = input() | |
| ans = 0 | |
| for mp in pb: | |
| seen = False | |
| works = True | |
| loc = sloc | |
| for j in s: | |
| j = int(j) | |
| if seen: | |
| break | |
| if not works: | |
| break | |
| nloc = (loc[0] + mp[j][0], loc[1] + mp[j][1]) | |
| if min(nloc) < 0 or nloc[0] >= m or nloc[1] >= n: | |
| works = False | |
| break | |
| if grid[nloc[1]][nloc[0]]: | |
| works = False | |
| break | |
| if nloc == eloc: | |
| seen = True | |
| break | |
| loc = nloc | |
| if seen: | |
| ans += 1 | |
| print(ans) | |
| ``` | |
| ### 192 | |
| ```python | |
| A11, A12, A13 = list(map(int, input().split())) | |
| A21, A22, A23 = list(map(int, input().split())) | |
| A31, A32, A33 = list(map(int, input().split())) | |
| Alist = [] | |
| Alist.append(A11) | |
| Alist.append(A12) | |
| Alist.append(A13) | |
| Alist.append(A21) | |
| Alist.append(A22) | |
| Alist.append(A23) | |
| Alist.append(A31) | |
| Alist.append(A32) | |
| Alist.append(A33) | |
| bingolist = [0] * 9 | |
| N = int(input()) | |
| for i in range(N): | |
| b = int(input()) | |
| for j in range(len(bingolist)): | |
| if b == Alist[j]: | |
| bingolist[j] = 1 | |
| if sum(bingolist[0:3]) == 3 or sum(bingolist[3:6]) == 3 or sum(bingolist[6:9]) == 3 or (bingolist[0] + bingolist[3] + bingolist[6] == 3) or (bingolist[1] + bingolist[4] + bingolist[7] == 3) or (bingolist[2] + bingolist[5] + bingolist[8] == 3) or (bingolist[0] + bingolist[4] + bingolist[8] == 3) or (bingolist[2] + bingolist[4] + bingolist[6] == 1): | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 193 | |
| ```python | |
| 3 | |
| import array | |
| from fractions import Fraction | |
| import functools | |
| import itertools | |
| import math | |
| import os | |
| import sys | |
| def main(): | |
| S = inp() | |
| print(solve(S)) | |
| MOD = 10 ** 9 + 7 | |
| def solve(S): | |
| N = len(S) | |
| fib = [0] * (N + 1) | |
| fib[0] = fib[1] = 1 | |
| for i in range(2 + 1, N + 1): | |
| fib[i] = (fib[i - 2] + fib[i - 1]) % MOD | |
| i = 0 | |
| ans = 1 | |
| while i < N: | |
| c = S[i] | |
| if c == 'm' or c == 'w': | |
| return 0 | |
| if c != 'u' and c != 'n': | |
| i += 1 | |
| continue | |
| j = i | |
| while j < N and S[i] == S[j]: | |
| j += 1 | |
| ans *= fib[j - i] | |
| ans %= MOD | |
| i = j | |
| return ans | |
| DEBUG = 'DEBUG' in os.environ | |
| def inp(): | |
| return sys.stdin.readline().rstrip() | |
| def read_int(): | |
| return int(inp()) | |
| def read_ints(): | |
| return [int(e) for e in inp().split()] | |
| def dprint(*value, sep=' ', end='\n'): | |
| if DEBUG: | |
| print(*value, sep=sep, end=end) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 194 | |
| ```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() | |
| ``` | |
| ### 195 | |
| ```python | |
| import math | |
| fibo = [1, 1, 2] | |
| M = 10 ** 9 + 7 | |
| for i in range(3, 100001): | |
| fibo += [(fibo[i - 1] + fibo[i - 2]) % M] | |
| s = list(input()) | |
| check = [True] * len(s) | |
| cnt = 1 | |
| for i in range(len(s)): | |
| if s[i - 1] == 'u' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'u': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'n' and check[i]: | |
| t = 0 | |
| j = i | |
| while j < len(s) and s[j] == 'n': | |
| check[j] = False | |
| t += 1 | |
| j += 1 | |
| cnt = cnt * fibo[t] % M | |
| if s[i] == 'm' or s[i] == 'w': | |
| cnt = 0 | |
| break | |
| print(cnt) | |
| ``` | |
| ### 196 | |
| ```python | |
| def main(): | |
| n, x = list(map(int, input().split())) | |
| vouchers = [False] * x | |
| for _ in range(n): | |
| lo, hi, cost = list(map(int, input().split())) | |
| w = hi - lo | |
| if w < x: | |
| l = vouchers[w] | |
| if l: | |
| l.append((lo, cost)) | |
| else: | |
| vouchers[w] = [(lo, cost)] | |
| best = inf = 1 << 31 | |
| x -= 2 | |
| if not x & 1 and vouchers[x // -2]: | |
| lh, h = (vouchers[x // 2], None) | |
| for f in (lh.sort, lh.reverse): | |
| l = h | |
| f() | |
| h, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| h.append((lo, b)) | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| for w, l, h in zip(list(range(x)), vouchers, vouchers[x:x // 2:-1]): | |
| if l and h: | |
| m = [] | |
| for lh in (l, h): | |
| for f in (lh.sort, lh.reverse): | |
| f() | |
| t, b = ([], inf) | |
| for lo, cost in lh: | |
| if b > cost: | |
| b = cost | |
| t.append((lo, b)) | |
| m.append(t) | |
| for l, h in ((m[0], m[3]), (m[2], m[1])): | |
| b, v = h.pop() | |
| for a, u in l: | |
| a += w | |
| while a >= b and h: | |
| b, v = h.pop() | |
| if a < b and best > u + v: | |
| best = u + v | |
| w = x - w | |
| print(best if best < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 197 | |
| ```python | |
| l = input().split() | |
| n = int(l[0]) | |
| m = int(l[1]) | |
| l = input().split() | |
| x = int(l[0]) | |
| k = int(l[-1]) | |
| y = int(l[2]) | |
| a = input().split() | |
| ai = [int(i) for i in a] | |
| b = input().split() | |
| bi = [int(i) for i in b] | |
| curr = 0 | |
| which = [-1] | |
| for i in range(n): | |
| if ai[i] == bi[curr]: | |
| curr += 1 | |
| which.append(i) | |
| if curr == m: | |
| break | |
| which.append(n) | |
| ans = 0 | |
| if curr != m: | |
| print(-1) | |
| else: | |
| poss = 1 | |
| for i in range(1, m + 2): | |
| if which[i - 1] + 1 == which[i]: | |
| continue | |
| if i == 1: | |
| z = ai[which[i]] | |
| elif i == m + 1: | |
| z = ai[which[i - 1]] | |
| else: | |
| z = max(ai[which[i]], ai[which[i - 1]]) | |
| maxa = max(ai[which[i - 1] + 1:which[i]]) | |
| num = which[i] - which[i - 1] - 1 | |
| if maxa > z and num < k: | |
| poss = 0 | |
| break | |
| if y * k <= x: | |
| if maxa > z: | |
| ans += x | |
| ans += (num - k) * y | |
| else: | |
| ans += num * y | |
| else: | |
| ans += num // k * x | |
| ans += num % k * y | |
| if poss == 0: | |
| print(-1) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 198 | |
| ```python | |
| import itertools as it | |
| import os | |
| import sys | |
| def calc_cost(cumsum, f): | |
| return sum([min(x % f, f - x % f) for x in cumsum]) | |
| def prime_factors(n): | |
| i = 2 | |
| factors = [] | |
| while i * i <= n: | |
| if n % i == 0: | |
| factors.append(i) | |
| while n % i == 0: | |
| n //= i | |
| else: | |
| i += 1 | |
| if n > 1: | |
| factors.append(n) | |
| return factors | |
| def f(xs): | |
| cumsum = list(it.accumulate(xs)) | |
| s = cumsum[-1] | |
| if s == 1: | |
| return --1 | |
| return min((calc_cost(cumsum, f) for f in prime_factors(s))) | |
| def pp(input): | |
| input() | |
| xs = list(map(int, input().split())) | |
| print(f(xs)) | |
| if 'paalto' in os.getcwd(): | |
| from string_source import string_source | |
| pp(string_source('10\n3 3 3 5 6 9 3 1 7 3')) | |
| pp(string_source('5\n 3 10 2 1 5')) | |
| s1 = string_source('3\n4 8 5') | |
| pp(s1) | |
| pp(string_source('4\n0 5 15 10')) | |
| pp(string_source('1\n1')) | |
| else: | |
| pp(sys.stdin.readline) | |
| ``` | |
| ### 199 | |
| ```python | |
| f = lambda: list(map(int, input().split())) | |
| n, x = f() | |
| s = [[] for i in range(x - 1)] | |
| for d in range(n): | |
| l, r, c = f() | |
| if r - l < x - 1: | |
| s[r - l] += [[l, c]] | |
| for t in s: | |
| t.sort(key=lambda q: q[0]) | |
| m = 3000000000.0 | |
| for d, t in enumerate(s): | |
| D = x - 2 - d | |
| i, T = (0, s[D]) | |
| M = 3000000000.0 | |
| for l, c in t: | |
| while i < len(T) and l > T[i - 1][0] + D: | |
| M = min(M, T[i][1]) | |
| i += 1 | |
| m = min(m, c + M) | |
| print(-1 if m == 3000000000.0 else m) | |
| ``` | |
| ### 200 | |
| ```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) | |
| ``` | |
| ### 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. | |