|
Download tasks/equivalent-program-pair-search/contexts/sample-00038.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 239 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00038.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00038.md
-
curl -L -o sample-00038.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00038.md
239 kB
| ### 001 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [0] * (n + 1) | |
| for i in range(m): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 2: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 0: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 002 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start - 1] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 003 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += 1 | |
| print(total) | |
| ``` | |
| ### 004 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] - x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 005 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 1) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 006 | |
| ```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() | |
| ``` | |
| ### 007 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p <= inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 008 | |
| ```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() | |
| ``` | |
| ### 009 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 010 | |
| ```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 3) | |
| ``` | |
| ### 011 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n - 1): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 012 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - -1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 013 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 014 | |
| ```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) | |
| ``` | |
| ### 015 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1 - 1] = nc | |
| print(res) | |
| ``` | |
| ### 016 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c - 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 017 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x + 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 018 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [0] * (n + 1) | |
| for i in range(m): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 0: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 0: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if not c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 019 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [0] * (n + 1) | |
| for i in range(m - 1): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 0: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 0: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 020 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1 - 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 021 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1 - 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 022 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 023 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(0): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 024 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] + prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 025 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1 + 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 026 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 1 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 027 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 0 | |
| for i in range(n): | |
| sun -= acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 028 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = -1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 029 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in reversed(range(k)): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 030 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 0 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 031 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((-1 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 032 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1 + 1] = nc | |
| print(res) | |
| ``` | |
| ### 033 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1 + 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 034 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1 + 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 035 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [2, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 036 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == -1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 037 | |
| ```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() | |
| ``` | |
| ### 038 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 4 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index - 1] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 040 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 2, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 041 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] - x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 042 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(2): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 043 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc -= length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 044 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1 - 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 045 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x - 1] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 046 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 0, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n - 1:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 047 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[-1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 048 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c - 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 049 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i - 1] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 050 | |
| ```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() | |
| ``` | |
| ### 051 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 3, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 052 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m - 1): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 053 | |
| ```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) | |
| ``` | |
| ### 054 | |
| ```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) | |
| ``` | |
| ### 055 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[2]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 056 | |
| ```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) | |
| ``` | |
| ### 057 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if not newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 058 | |
| ```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() | |
| ``` | |
| ### 059 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1 - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == 1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 060 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = -1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 061 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index - 1]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 063 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] * prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 064 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -4): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 065 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 4, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 066 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 2 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 067 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[--1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 068 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1 - 1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1 - 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 070 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if not col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 071 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + -1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 072 | |
| ```python | |
| def dfs(v, g, vis): | |
| count = 1 | |
| first = v | |
| st = [v] | |
| vis[v] = True | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| if len(g[v]) != 2 or (vis[g[v][0]] and g[v][0] != first and (g[v][1] != first)): | |
| count = 0 | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| if v == first and count: | |
| break | |
| if len(g[first]) == 2 and (not vis[g[first][1]]): | |
| st.append(g[first][1]) | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| return count | |
| v, e = list(map(int, input().split())) | |
| g = [[] for i in range(v)] | |
| for i in range(e): | |
| f, to = list(map(int, input().split())) | |
| g[f - 1].append(to - 1) | |
| g[to - 1].append(f - 1) | |
| vis = [False] * v | |
| ans = 0 | |
| for i in range(v): | |
| if not vis[i]: | |
| ans += dfs(i, g, vis) | |
| print(ans) | |
| ``` | |
| ### 073 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 0 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 074 | |
| ```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) | |
| ``` | |
| ### 075 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 3: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] - cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 077 | |
| ```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)) | |
| ``` | |
| ### 078 | |
| ```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) | |
| ``` | |
| ### 079 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1 - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 080 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1 - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 081 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r + 1])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 082 | |
| ```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) | |
| ``` | |
| ### 083 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 084 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[R - 1] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 085 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - -1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else -1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 086 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] - cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 087 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 0 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i - 1]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 088 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if not c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 089 | |
| ```python | |
| def dfs(v, g, vis): | |
| count = 1 | |
| first = v | |
| st = [v] | |
| vis[v] = True | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| if len(g[v]) != 2 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)): | |
| count = 0 | |
| for i in range(len(g[v]) - 1): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| if v == first and count: | |
| break | |
| if len(g[first]) == 2 and (not vis[g[first][1]]): | |
| st.append(g[first][1]) | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| return count | |
| v, e = list(map(int, input().split())) | |
| g = [[] for i in range(v)] | |
| for i in range(e): | |
| f, to = list(map(int, input().split())) | |
| g[f - 1].append(to - 1) | |
| g[to - 1].append(f - 1) | |
| vis = [False] * v | |
| ans = 0 | |
| for i in range(v): | |
| if not vis[i]: | |
| ans += dfs(i, g, vis) | |
| print(ans) | |
| ``` | |
| ### 090 | |
| ```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) | |
| ``` | |
| ### 091 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-3) | |
| ``` | |
| ### 092 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if not c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 093 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [1] * (n + 1) | |
| for i in range(m): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 0: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 0: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 094 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| n, m = list(map(int, input().split())) | |
| s = list(((0, 1)[c == '.'] for c in list(input()))) | |
| res = 0 | |
| cnt = 0 | |
| for k in s: | |
| if k == 1: | |
| cnt += 1 | |
| else: | |
| res += max(cnt - 1, 0) | |
| cnt = 0 | |
| res += max(0, cnt - 1) | |
| for i in range(m): | |
| x, c = input().split() | |
| x = int(x) | |
| nc = (0, 1)[c == '.'] | |
| if s[x - 1] != nc: | |
| diff = 0 | |
| if x < n: | |
| if s[int(x)] == 1: | |
| diff += 1 | |
| if x > 1: | |
| if s[int(x - 2)] == 1: | |
| diff += 1 | |
| if nc == -1: | |
| res += diff | |
| else: | |
| res -= diff | |
| s[x - 1] = nc | |
| print(res) | |
| ``` | |
| ### 095 | |
| ```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) | |
| ``` | |
| ### 096 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 1) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 097 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [0] * (n + 1) | |
| for i in range(m): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 0: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 3: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 098 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 099 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 3 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 100 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i - 1]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 101 | |
| ```python | |
| def main(): | |
| n, m, k = list(map(int, input().split())) | |
| c, *cc = list(map(int, input().split())) | |
| pp, *ppp = (list(map(float, input().split())) for _ in range(n)) | |
| inf = 2.0 ** 47 | |
| nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp] | |
| for c, pp in zip(cc, ppp): | |
| newrow = [inf] * m | |
| cur, nxt = (nxt, [newrow]) | |
| if c: | |
| c -= 1 | |
| for row in cur: | |
| p = row[c] | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| row[c] = inf | |
| newrow = [inf] * m | |
| newrow[c] = min(row) | |
| nxt.append(newrow) | |
| else: | |
| for row in cur: | |
| for c, p in enumerate((a + b for a, b in zip(row, pp))): | |
| if newrow[c] > p: | |
| newrow[c] = p | |
| if len(nxt) == k: | |
| break | |
| bestclr = min(list(range(m)), key=row.__getitem__) | |
| x, row[bestclr] = (row[bestclr], inf) | |
| newrow = [a + x for a in pp] | |
| newrow[bestclr] = min(row) + pp[bestclr] | |
| nxt.append(newrow) | |
| p = min(nxt[-1]) | |
| print(int(p) if p < inf else --1) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 102 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - -1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j < (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 103 | |
| ```python | |
| import sys | |
| input = sys.stdin.readline | |
| for f in range(int(input())): | |
| n, m = map(int, input().split()) | |
| a = [0] * m | |
| b = [0] * m | |
| acop = [0] * m | |
| bcop = [0] * m | |
| for i in range(m): | |
| a[i], b[i] = map(int, input().split()) | |
| acop[i] = a[i] | |
| bcop[i] = [b[i], i] | |
| acop.sort(reverse=True) | |
| bcop.sort(reverse=True) | |
| mx = 0 | |
| i = 0 | |
| su = 0 | |
| for j in range(m): | |
| bi = bcop[j][0] | |
| ii = bcop[j][1] | |
| ai = a[ii] | |
| while i < m and acop[i] > bi: | |
| su += acop[i] | |
| i += 1 | |
| maxi = ai | |
| if i < n: | |
| maxi += su | |
| if i + 1 < n: | |
| maxi += (n - i - 1) * bi | |
| if ai > bi: | |
| maxi += bi | |
| maxi -= ai | |
| mx = max(maxi, mx) | |
| if n <= m: | |
| sun = 1 | |
| for i in range(n): | |
| sun += acop[i] | |
| mx = max(mx, sun) | |
| print(mx) | |
| blank = input() | |
| ``` | |
| ### 104 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i - 1] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 105 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += -1 | |
| print(total) | |
| ``` | |
| ### 106 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx < i: | |
| temp += flowers_b[i] * remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 107 | |
| ```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) | |
| ``` | |
| ### 108 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in range(len(ops)): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == -2: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index - 1] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 110 | |
| ```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() | |
| ``` | |
| ### 111 | |
| ```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) | |
| ``` | |
| ### 112 | |
| ```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)) | |
| ``` | |
| ### 113 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end - 1] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 114 | |
| ```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) | |
| ``` | |
| ### 115 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == -1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 116 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 1, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 117 | |
| ```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) | |
| ``` | |
| ### 118 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i - 1] = lst[start] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 119 | |
| ```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)) | |
| ``` | |
| ### 120 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const - 1): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 121 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc -= 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 122 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1 - 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 123 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 1): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 124 | |
| ```python | |
| import sys | |
| n, m = [int(x) for x in input().split()] | |
| notes = [int(x) for x in input().split()] | |
| managers = [] | |
| for line in sys.stdin: | |
| managers.append(tuple((int(x) for x in line.split()))) | |
| bearing_managers = [] | |
| M, T = (0, 0) | |
| for t, r in reversed(managers): | |
| if r > M: | |
| if t == T: | |
| bearing_managers[-1] = (t, r) | |
| else: | |
| T = t | |
| bearing_managers.append((t, r)) | |
| M = r | |
| bearing_managers.reverse() | |
| t, r = bearing_managers[0] | |
| bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0)) | |
| L, R = (0, r - 1) | |
| sorted_part = list(sorted(notes[:r])) | |
| for t1, r1 in bearing_managers: | |
| if t1 == 2: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i - 1] = sorted_part[R] | |
| R -= 1 | |
| else: | |
| for i in range(r - 1, r1 - 1, -1): | |
| notes[i] = sorted_part[L] | |
| L += 1 | |
| r = r1 | |
| for x in notes: | |
| sys.stdout.write(str(x) + ' ') | |
| ``` | |
| ### 125 | |
| ```python | |
| n = int(input()) | |
| a = list(map(int, input().split())) | |
| r, b, c = (0, [a[0]], 0) | |
| for x, y, z in zip(a, a[1:], a[2:]): | |
| if x != y != z: | |
| c += 1 | |
| else: | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c + 1) // 2) | |
| c = 0 | |
| y = a[-1] | |
| if c & 1: | |
| b.extend([y] * (c + 1)) | |
| else: | |
| b.extend([1 - y] * (c // 2) + [y] * (c // 2 + 1)) | |
| r = max(r, (c - 1) // 2) | |
| print(r) | |
| print(*b) | |
| ``` | |
| ### 126 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += 5 | |
| print(total) | |
| ``` | |
| ### 127 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i - 1], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 128 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][-1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 129 | |
| ```python | |
| N = int(input()) | |
| seq = [i for i in input().split()] | |
| def end_lst(i): | |
| while i < N - 1 and seq[i] != seq[i + 1]: | |
| i = i + 1 | |
| return i | |
| def reorder(lst, start, end): | |
| if start == end - 1: | |
| return 0 | |
| if lst[start] == lst[end]: | |
| for i in range(start, end + 1): | |
| lst[i] = lst[start] | |
| return (end - start) // 2 | |
| mid = (start + end) // 2 | |
| for i in range(start, mid + 1): | |
| lst[i] = lst[start - 1] | |
| for i in range(mid + 1, end + 1): | |
| lst[i] = lst[end] | |
| return (end - start + 1) // 2 - 1 | |
| i, ans = (0, 0) | |
| while i < N - 1: | |
| if seq[i] != seq[i + 1]: | |
| end = end_lst(i) | |
| ans = max(reorder(seq, i, end), ans) | |
| i = end | |
| else: | |
| i += 1 | |
| print(ans) | |
| print(' '.join(seq)) | |
| ``` | |
| ### 130 | |
| ```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() | |
| ``` | |
| ### 131 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in reversed(range(sum, down - 1, -1)): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 132 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] > k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += -1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 133 | |
| ```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) | |
| ``` | |
| ### 134 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 3 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 135 | |
| ```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) | |
| ``` | |
| ### 136 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query - 1] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 137 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(2, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 138 | |
| ```python | |
| 3 | |
| class StdReader: | |
| def read_int(self): | |
| return int(self.read_string()) | |
| def read_ints(self, sep=None): | |
| return [int(i) for i in self.read_strings(sep)] | |
| def read_float(self): | |
| return float(self.read_string()) | |
| def read_floats(self, sep=None): | |
| return [float(i) for i in self.read_strings(sep)] | |
| def read_string(self): | |
| return input() | |
| def read_strings(self, sep=None): | |
| return self.read_string().split(sep) | |
| reader = StdReader() | |
| def part_sort(a, i, j, reverse=False): | |
| a[i:j] = sorted(a[i:j], reverse=reverse) | |
| def part_sorted(b, i, j, reverse=False): | |
| a = list(b) | |
| part_sort(a, i, j, reverse) | |
| return a | |
| def main(): | |
| n, m = reader.read_ints() | |
| a = reader.read_ints() | |
| ops = [] | |
| for i in range(m): | |
| t, r = reader.read_ints() | |
| op = (t, r) | |
| while ops and op[1] >= ops[-1][1]: | |
| ops.pop() | |
| ops.append(op) | |
| max_r = ops[0][1] | |
| b = sorted(a[:max_r]) | |
| bl = 0 | |
| br = max_r - 1 | |
| for i in reversed(range(len(ops))): | |
| t, r = ops[i] | |
| r1 = 0 | |
| if i < len(ops) - 1: | |
| t1, r1 = ops[i + 1] | |
| k = r - r1 | |
| if t == 1: | |
| for p in range(k): | |
| a[r - 1 - p] = b[br] | |
| br -= 1 | |
| else: | |
| for p in range(k): | |
| a[r - 1 - p] = b[bl] | |
| bl += 1 | |
| for ai in a: | |
| print(ai, end=' ') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 139 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1 - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 140 | |
| ```python | |
| def dfs(v, g, vis): | |
| count = 1 | |
| first = v | |
| st = [v] | |
| vis[v] = True | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| if len(g[v]) != 2 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)): | |
| count = 0 | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| if v == first and count: | |
| break | |
| if len(g[first]) == 2 and (not vis[g[first][1]]): | |
| st.append(g[first][1]) | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| return count | |
| v, e = list(map(int, input().split())) | |
| g = [[] for i in range(v)] | |
| for i in range(e): | |
| f, to = list(map(int, input().split())) | |
| g[f - 1].append(to - 1) | |
| g[to - 1].append(f - 5) | |
| vis = [False] * v | |
| ans = 0 | |
| for i in range(v): | |
| if not vis[i]: | |
| ans += dfs(i, g, vis) | |
| print(ans) | |
| ``` | |
| ### 141 | |
| ```python | |
| import operator | |
| data = list(map(int, input().split(' '))) | |
| n = data[0] | |
| m = data[1] | |
| edge = {} | |
| for i in range(n): | |
| edge[i + 1] = [] | |
| for _ in range(m): | |
| data = list(map(int, input().split(' '))) | |
| i = data[0] | |
| j = data[1] | |
| edge[i].append(j) | |
| edge[j].append(i) | |
| candidate = [k for k, v in list(edge.items()) if len(v) == 2] | |
| result = 0 | |
| used = {} | |
| def _iscircle(c, result): | |
| n = edge[c][0] | |
| used[c] = 1 | |
| last = c | |
| while n not in used: | |
| used[n] = 1 | |
| if len(edge[n]) != 2: | |
| return result | |
| t = edge[n][0] | |
| if not t != last: | |
| last = n | |
| if t == c: | |
| return result + 1 | |
| n = t | |
| continue | |
| last = n | |
| t = edge[n][1] | |
| if t == c: | |
| return result + 1 | |
| n = t | |
| return result | |
| for i in candidate: | |
| result = _iscircle(i, result) | |
| print(result) | |
| ``` | |
| ### 142 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > 0: | |
| cur_max = min(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 143 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 2 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 144 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 0 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C - 1]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 145 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[2] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 146 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 0 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 147 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [0] * (5 * 10 ** 5 + 1) | |
| for i in reversed(range(0, n)): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 148 | |
| ```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() | |
| ``` | |
| ### 149 | |
| ```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() | |
| ``` | |
| ### 150 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 0: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] + 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 151 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 0 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 2) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 152 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(-1, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 153 | |
| ```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) | |
| ``` | |
| ### 154 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 0, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[+n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 155 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 1 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 156 | |
| ```python | |
| import operator | |
| data = list(map(int, input().split(' '))) | |
| n = data[0] | |
| m = data[1] | |
| edge = {} | |
| for i in range(n): | |
| edge[i + 1] = [] | |
| for _ in range(m): | |
| data = list(map(int, input().split(' '))) | |
| i = data[0] | |
| j = data[1] | |
| edge[i].append(j) | |
| edge[j].append(i) | |
| candidate = [k for k, v in list(edge.items()) if len(v) == 2] | |
| result = 0 | |
| used = {} | |
| def _iscircle(c, result): | |
| n = edge[c][0] | |
| used[c] = 1 | |
| last = c | |
| while n not in used: | |
| used[n] = 1 | |
| if len(edge[n]) != 2: | |
| return result | |
| t = edge[n][0] | |
| if t != last: | |
| last = n | |
| if t == c: | |
| return result + 1 | |
| n = t | |
| continue | |
| last = n | |
| t = edge[n][1] | |
| if t == c: | |
| return result + -2 | |
| n = t | |
| return result | |
| for i in candidate: | |
| result = _iscircle(i, result) | |
| print(result) | |
| ``` | |
| ### 157 | |
| ```python | |
| MAX = 10 ** 18 + 5 | |
| n, m, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| cost = [list(map(int, input().split())) for _ in range(n)] | |
| min1 = [[MAX] * (k + 1) for _ in range(n)] | |
| min2 = [[MAX] * (k + 1) for _ in range(n)] | |
| idx = [[-1] * (k + 1) for _ in range(n)] | |
| for i in cost: | |
| i.insert(0, 0) | |
| dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)] | |
| if a[0] == 0: | |
| for i in range(3, m + 1): | |
| if min1[0][1] >= cost[0][i]: | |
| if min1[0][1] == cost[0][i]: | |
| idx[0][1] = -2 | |
| else: | |
| idx[0][1] = i | |
| min2[0][1] = min1[0][1] | |
| min1[0][1] = cost[0][i] | |
| elif min2[0][1] >= cost[0][i]: | |
| min2[0][1] = cost[0][i] | |
| dp[0][1][i] = cost[0][i] | |
| else: | |
| dp[0][1][a[0]] = 0 | |
| min1[0][1] = 0 | |
| idx[0][1] = a[0] | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if a[i] == 0: | |
| for l in range(1, m + 1): | |
| dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l]) | |
| if l == idx[i - 1][j - 1]: | |
| dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l]) | |
| else: | |
| for l in range(1, m + 1): | |
| dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]]) | |
| if l != a[i]: | |
| dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]]) | |
| for l in range(1, m + 1): | |
| if min1[i][j] >= dp[i][j][l]: | |
| if min1[i][j] == dp[i][j][l]: | |
| idx[i][j] = -2 | |
| else: | |
| idx[i][j] = l | |
| min2[i][j] = min1[i][j] | |
| min1[i][j] = dp[i][j][l] | |
| elif min2[i][j] >= dp[i][j][l]: | |
| min2[i][j] = dp[i][j][l] | |
| ans = MAX | |
| for i in dp[-1][-1]: | |
| ans = min(ans, i) | |
| print(ans if ans < MAX else -1) | |
| ``` | |
| ### 158 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 1] = -2 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 159 | |
| ```python | |
| n, m, k = map(int, input().split()) | |
| col = list(map(int, input().split())) | |
| p = [] | |
| for i in range(n): | |
| p.append(list(map(int, input().split()))) | |
| dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)] | |
| if col[0] == 0: | |
| for i in range(m): | |
| dp[0][1][i] = p[0][i] | |
| else: | |
| dp[0][1][col[0] - 2] = 0 | |
| for i in range(1, n): | |
| for j in range(1, k + 1): | |
| if j == 1: | |
| if col[i]: | |
| dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1] | |
| else: | |
| for c in range(m): | |
| dp[i][j][c] = dp[i - 1][j][c] + p[i][c] | |
| elif col[i]: | |
| x = dp[i - 1][j][col[i] - 1] | |
| for t in range(m): | |
| if t != col[i] - 1: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][col[i] - 1] = x | |
| else: | |
| for c in range(m): | |
| x = dp[i - 1][j][c] | |
| for t in range(m): | |
| if t != c: | |
| x = min(x, dp[i - 1][j - 1][t]) | |
| dp[i][j][c] = x + p[i][c] | |
| ans = min(dp[-1][-1]) | |
| if ans != float('inf'): | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 160 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-3] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 161 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query + 1] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 162 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= -1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 163 | |
| ```python | |
| from collections import defaultdict | |
| def solve(): | |
| N, C = map(int, input().split()) | |
| arr = [int(k) for k in input().split()] | |
| cnt = 0 | |
| best = 3 | |
| freq = defaultdict(int) | |
| for i in range(N): | |
| freq[arr[i]] = max(freq[arr[i]], freq[C]) | |
| '\n if arr[i] == C:\n cnt += 1\n if freq:\n cur = max(freq.values())\n if cur > best:\n best = cur\n \n freq = defaultdict(int)\n continue\n ' | |
| freq[arr[i]] += 1 | |
| cur = freq[arr[i]] - freq[C] | |
| if cur > best: | |
| best = cur | |
| print(best + freq[C]) | |
| solve() | |
| ``` | |
| ### 164 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| seq = stdin.readline().split() | |
| result = [0] * n | |
| result[0] = seq[0] | |
| mark = False | |
| cur_len = 0 | |
| max_len = 0 | |
| carry = 0 | |
| carry_id = 0 | |
| i = 1 | |
| while i < len(seq) - 1: | |
| if mark: | |
| if seq[i] != seq[i + 1]: | |
| cur_len += 1 | |
| else: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i:1] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2:1] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i:1] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| mark = False | |
| cur_len = 0 | |
| elif seq[i] != seq[i - 1] and seq[i] != seq[i + 1]: | |
| mark = True | |
| cur_len = 1 | |
| carry = seq[i - 1] | |
| carry_id = i | |
| else: | |
| result[i] = seq[i] | |
| i += 1 | |
| if mark: | |
| if cur_len > max_len: | |
| max_len = cur_len | |
| if seq[i] == carry: | |
| result[carry_id:i] = [carry] * cur_len | |
| else: | |
| result[carry_id:carry_id + cur_len // 2] = [carry] * (cur_len // 2) | |
| result[carry_id + cur_len // 2:i] = [seq[i]] * (cur_len // 2) | |
| result[i] = seq[i] | |
| print((max_len + 1) // 3) | |
| for x in result: | |
| print(x, end=' ') | |
| ``` | |
| ### 165 | |
| ```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) | |
| ``` | |
| ### 166 | |
| ```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) | |
| ``` | |
| ### 167 | |
| ```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) | |
| ``` | |
| ### 168 | |
| ```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) | |
| ``` | |
| ### 169 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m <= w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 170 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += 3 | |
| print(total) | |
| ``` | |
| ### 171 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += 2 | |
| print(total) | |
| ``` | |
| ### 172 | |
| ```python | |
| import sys | |
| def input(): | |
| return sys.stdin.readline().rstrip() | |
| def input_split(): | |
| return [int(i) for i in input().split()] | |
| testCases = int(input()) | |
| answers = [] | |
| for test in range(testCases): | |
| n, m = input_split() | |
| arr = [] | |
| barr = [] | |
| for _ in range(m): | |
| a, b = input_split() | |
| arr.append(a) | |
| barr.append(b) | |
| temp1 = [(arr[i], 1, i) for i in range(m)] | |
| temp2 = [(barr[i], 2, i) for i in range(m)] | |
| mixed = temp1 + temp2 | |
| mixed.sort() | |
| mixed.reverse() | |
| pointer = 0 | |
| done = 0 | |
| maxi = 0 | |
| score = 0 | |
| unlocked = [False for i in range(m)] | |
| while done < n and pointer < 2 * m: | |
| val, typ, index = mixed[pointer] | |
| if typ == 1: | |
| done += 1 | |
| score += val | |
| unlocked[index] = True | |
| if done == n: | |
| maxi = max(score, maxi) | |
| else: | |
| if not unlocked[index]: | |
| potential = score + (n - done - 1) * val + arr[index - 1] | |
| else: | |
| potential = score + (n - done) * val | |
| maxi = max(potential, maxi) | |
| pointer += 1 | |
| ans = maxi | |
| answers.append(ans) | |
| if test != testCases - 1: | |
| blank = input() | |
| print(*answers, sep='\n') | |
| ``` | |
| ### 173 | |
| ```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) | |
| ``` | |
| ### 174 | |
| ```python | |
| def connected_components(neighbors): | |
| seen = set() | |
| def component(node): | |
| nodes = set([node]) | |
| while nodes: | |
| node = nodes.pop() | |
| seen.add(node) | |
| nodes |= neighbors[node] - seen | |
| yield node | |
| for node in neighbors: | |
| if node not in seen: | |
| yield component(node) | |
| from collections import defaultdict | |
| graph = defaultdict(set) | |
| n, m = map(int, input().split()) | |
| for _ in range(m): | |
| u, v = map(int, input().split()) | |
| graph[u].add(v) | |
| graph[v].add(u) | |
| total = 0 | |
| for component in connected_components(graph): | |
| nodes = list(component) | |
| size = len(nodes) | |
| seen = set() | |
| current = nodes[0] | |
| while len(seen) < size: | |
| choice = list(graph[current]) | |
| if len(choice) != 2: | |
| break | |
| seen.add(current) | |
| possible = [c for c in choice if c not in seen] | |
| if not not possible: | |
| break | |
| current = possible[0] | |
| if len(seen) == size: | |
| total += 1 | |
| print(total) | |
| ``` | |
| ### 175 | |
| ```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() | |
| ``` | |
| ### 176 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(1000000) | |
| def solve(): | |
| n, m = rv() | |
| s = list(input()) | |
| res = [0] * m | |
| helper = list() | |
| for i in range(n): | |
| if s[i] == '.': | |
| if i == 0: | |
| helper.append(1) | |
| elif s[i - 1] == '.': | |
| helper[-1] += 1 | |
| else: | |
| helper.append(1) | |
| initval = 0 | |
| for val in helper: | |
| initval += val - 1 | |
| for query in range(m): | |
| index, replace = input().split() | |
| index = int(index) - 1 | |
| if s[index] == '.' and replace == '.' or (s[index] != '.' and replace != '.'): | |
| res[query - 1] = initval | |
| else: | |
| sidedots = 0 | |
| if index > 0: | |
| if s[index - 1] == '.': | |
| sidedots += 1 | |
| if index < n - 1: | |
| if s[index + 1] == '.': | |
| sidedots += 1 | |
| if s[index] == '.': | |
| res[query] = initval - sidedots | |
| initval -= sidedots | |
| else: | |
| res[query] = initval + sidedots | |
| initval += sidedots | |
| s[index] = replace | |
| print('\n'.join(map(str, res))) | |
| def rv(): | |
| return list(map(int, input().split())) | |
| def rl(n): | |
| return [list(map(int, input().split())) for _ in range(n)] | |
| if sys.hexversion == 50594544: | |
| sys.stdin = open('test.txt') | |
| solve() | |
| ``` | |
| ### 177 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| B = [0] * (n + 1) | |
| S1 = [0] * (n + 1) | |
| S2 = [0] * (n + 1) | |
| for i in range(m): | |
| S = input().split(' ') | |
| a = int(S[0]) | |
| b = int(S[1]) | |
| if S1[a] == 0: | |
| S1[a] = b | |
| elif S2[a] == 0: | |
| S2[a] = b | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| if S1[b] == 0: | |
| S1[b] = a | |
| elif S2[b] == 0: | |
| S2[b] = a | |
| else: | |
| B[a] = 1 | |
| B[b] = 1 | |
| for i in range(1, n + 1): | |
| if S1[i] == 0 or S2[i] == 0: | |
| B[i] = 1 | |
| sol = 0 | |
| for ind in range(1, n + 1): | |
| if B[ind] == 0: | |
| v = ind | |
| w = S2[ind] | |
| c = True | |
| while w != ind: | |
| B[v] = 1 | |
| if B[w] == 0: | |
| u = w | |
| if S1[w] == v: | |
| w = S2[w] | |
| else: | |
| w = S1[w] | |
| else: | |
| B[ind] = 0 | |
| c = False | |
| break | |
| v = u | |
| if c: | |
| B[v] = 1 | |
| sol += 1 | |
| print(sol) | |
| ``` | |
| ### 178 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(2, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 179 | |
| ```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() | |
| ``` | |
| ### 180 | |
| ```python | |
| import operator | |
| data = list(map(int, input().split(' '))) | |
| n = data[0] | |
| m = data[1] | |
| edge = {} | |
| for i in range(n): | |
| edge[i + 1] = [] | |
| for _ in range(m): | |
| data = list(map(int, input().split(' '))) | |
| i = data[0] | |
| j = data[1] | |
| edge[i].append(j) | |
| edge[j].append(i) | |
| candidate = [k for k, v in list(edge.items()) if len(v) == 2] | |
| result = 0 | |
| used = {} | |
| def _iscircle(c, result): | |
| n = edge[c][0] | |
| used[c] = 1 | |
| last = c | |
| while n not in used: | |
| used[n] = 1 | |
| if len(edge[n]) != 2: | |
| return result | |
| t = edge[n][0] | |
| if t != last: | |
| last = n | |
| if t == c: | |
| return result + 1 | |
| n = t | |
| continue | |
| last = n | |
| t = edge[n][1] | |
| if t == c: | |
| return result + 4 | |
| n = t | |
| return result | |
| for i in candidate: | |
| result = _iscircle(i, result) | |
| print(result) | |
| ``` | |
| ### 181 | |
| ```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) | |
| ``` | |
| ### 182 | |
| ```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) | |
| ``` | |
| ### 183 | |
| ```python | |
| def maxseg(a): | |
| cur_max = 0 | |
| global_max = 0 | |
| pre_ = 0 | |
| for i, x in enumerate(a): | |
| if x > -4: | |
| cur_max = max(x, cur_max + pre_ + x) | |
| global_max = max(global_max, cur_max) | |
| else: | |
| pre_ = x | |
| return global_max | |
| def push(d, u, i): | |
| if u not in d: | |
| d[u] = [] | |
| d[u].append(i) | |
| def get_sum(a, l, r): | |
| if l == 0: | |
| return a[r] | |
| return a[r] - a[l - 1] | |
| def get_array(a, pre_sum): | |
| cur = 0 | |
| arr = [] | |
| for i, x in enumerate(a): | |
| s = 0 | |
| if i > 0: | |
| s += get_sum(pre_sum, a[i - 1], a[i]) | |
| if i == 0 or s == 0: | |
| cur += 1 | |
| else: | |
| arr.append(cur) | |
| arr.append(-s) | |
| cur = 1 | |
| arr.append(cur) | |
| return arr | |
| n, c = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| d = {} | |
| pre_sum = [0] * len(a) | |
| pre = 0 | |
| for i, x in enumerate(a): | |
| if x == c: | |
| pre += 1 | |
| else: | |
| push(d, x, i) | |
| pre_sum[i] = pre | |
| max_ = 0 | |
| for pos_arr in d.values(): | |
| arr = get_array(pos_arr, pre_sum) | |
| max_ = max(max_, maxseg(arr)) | |
| print(max_ + pre_sum[-1]) | |
| ``` | |
| ### 184 | |
| ```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() | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| fin = sys.stdin | |
| [n, m] = [int(x) for x in fin.readline().split()] | |
| s = [x == '.' for x in fin.readline().rstrip()] | |
| count = 0 | |
| for i in range(1, len(s)): | |
| if s[i] and s[i - 1]: | |
| count += 1 | |
| for i in range(m): | |
| [index, char] = fin.readline().split() | |
| index = int(index) - 1 | |
| if s[index] and char != '.': | |
| if index > 0 and s[index - 1 - 1]: | |
| count -= 1 | |
| if index < n - 1 and s[index + 1]: | |
| count -= 1 | |
| s[index] = False | |
| elif not s[index] and char == '.': | |
| if index > 0 and s[index - 1]: | |
| count += 1 | |
| if index < n - 1 and s[index + 1]: | |
| count += 1 | |
| s[index] = True | |
| print(count) | |
| ``` | |
| ### 186 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 0) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-0] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 187 | |
| ```python | |
| import math | |
| dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)] | |
| n, m, k = list(map(int, input().split())) | |
| k += 1 | |
| z = list(map(int, input().split())) | |
| matrix = [] | |
| for i in range(n): | |
| ans = list(map(int, input().split())) | |
| matrix.append(ans) | |
| for i in range(len(z)): | |
| if i == 0: | |
| if z[i] != 0: | |
| dp[0][1][z[i] - 1] = 0 | |
| else: | |
| for x in range(m): | |
| dp[0][1][x] = matrix[i][x] | |
| elif z[i] != 4: | |
| col = z[i] - 1 | |
| if z[i - 1] != 0: | |
| col2 = z[i - 1] - 1 | |
| if col2 == col: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col]) | |
| else: | |
| for j in range(k): | |
| if j == 0: | |
| continue | |
| dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col]) | |
| else: | |
| pq = [] | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| col = z[i] - 1 | |
| if col != pq[0][1] and pq[0][0] != math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col]) | |
| elif pq[0][0] != math.inf and col == pq[0][1]: | |
| dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col]) | |
| elif pq[0][0] == math.inf: | |
| dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col]) | |
| elif z[i - 1] != 0: | |
| col = z[i - 1] - 1 | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| for x in range(m): | |
| if x != col: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x]) | |
| else: | |
| dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x]) | |
| else: | |
| for t in range(k): | |
| if t == 0: | |
| continue | |
| pq = [] | |
| for x in range(m): | |
| pq.append([dp[i - 1][t - 1][x], x]) | |
| pq.append([math.inf, math.inf]) | |
| pq.sort() | |
| for v in range(m): | |
| if v != pq[0][1]: | |
| dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v]) | |
| else: | |
| dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v]) | |
| mini = math.inf | |
| for i in range(m): | |
| mini = min(mini, dp[n - 1][k - 1][i]) | |
| if mini == math.inf: | |
| print(-1) | |
| else: | |
| print(mini) | |
| ``` | |
| ### 188 | |
| ```python | |
| from bisect import bisect_left | |
| t = int(input()) | |
| for case in range(t): | |
| n, m = list(map(int, input().split())) | |
| ab = [list(map(int, input().split())) for _ in range(m)] | |
| aa = [e for e, _ in ab] | |
| aa.sort() | |
| acc = [0] * (m + 1) | |
| for i in range(m, 3, -1): | |
| acc[i - 1] = acc[i] + aa[i - 1] | |
| if n > m: | |
| ans = 0 | |
| else: | |
| ans = sum(aa[-n:]) | |
| for a, b in ab: | |
| i = bisect_left(aa, b) | |
| cnt = min(m - i, n) | |
| sm = acc[m - cnt] | |
| sm += b * (n - cnt) | |
| if a < b: | |
| sm -= b | |
| sm += a | |
| ans = max(ans, sm) | |
| print(ans) | |
| if case != t - 1: | |
| input() | |
| ``` | |
| ### 189 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c == '.' and s[j] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 190 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 191 | |
| ```python | |
| def dfs(v, g, vis): | |
| count = 1 | |
| first = v | |
| st = [v] | |
| vis[v] = True | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| if len(g[v]) != 4 or (vis[g[v][0]] and vis[g[v][1]] and (g[v][0] != first) and (g[v][1] != first)): | |
| count = 0 | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| if v == first and count: | |
| break | |
| if len(g[first]) == 2 and (not vis[g[first][1]]): | |
| st.append(g[first][1]) | |
| while st: | |
| v = st[-1] | |
| st.pop() | |
| for i in range(len(g[v])): | |
| if not vis[g[v][i]]: | |
| vis[g[v][i]] = True | |
| st.append(g[v][i]) | |
| return count | |
| v, e = list(map(int, input().split())) | |
| g = [[] for i in range(v)] | |
| for i in range(e): | |
| f, to = list(map(int, input().split())) | |
| g[f - 1].append(to - 1) | |
| g[to - 1].append(f - 1) | |
| vis = [False] * v | |
| ans = 0 | |
| for i in range(v): | |
| if not vis[i]: | |
| ans += dfs(i, g, vis) | |
| print(ans) | |
| ``` | |
| ### 192 | |
| ```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)) | |
| ``` | |
| ### 193 | |
| ```python | |
| import bisect | |
| t = int(input()) | |
| for _ in range(t): | |
| n, m = list(map(int, input().split())) | |
| flowers = [] | |
| flowers_a = [] | |
| max_b = 0 | |
| for i in range(m): | |
| a, b = list(map(int, input().split())) | |
| flowers.append((a, b)) | |
| flowers.sort() | |
| flowers_b = [] | |
| for i in range(m): | |
| flowers_a.append(flowers[i][0]) | |
| flowers_b.append(flowers[i][1]) | |
| val = 0 | |
| prefix_sum = [0] | |
| for x in flowers_a: | |
| prefix_sum.append(prefix_sum[-1] + x) | |
| for i in range(m): | |
| idx = bisect.bisect_left(flowers_a, flowers_b[i]) | |
| options = m - idx | |
| if options >= n: | |
| temp = prefix_sum[-1] - prefix_sum[-n - 1] | |
| val = max(val, temp) | |
| continue | |
| else: | |
| temp = prefix_sum[-1] - prefix_sum[-options - 1] | |
| remaining = n - options | |
| if idx <= i: | |
| temp += flowers_b[i] + remaining | |
| else: | |
| temp += flowers_a[i] + flowers_b[i] * (remaining - 1) | |
| val = max(val, temp) | |
| print(val) | |
| if _ != t - 1: | |
| blank_line = input() | |
| ``` | |
| ### 194 | |
| ```python | |
| a, b = [int(i) for i in input().split()] | |
| q = [int(i) for i in input().split()] | |
| s = [] | |
| for p in range(b): | |
| s += [[int(i) for i in input().split()]] | |
| S = [] | |
| k = 0 | |
| for p in range(b - 1, -1, -1): | |
| if s[p][1] >= k: | |
| S += [s[p]] | |
| k = s[p][1] | |
| Q1 = sorted(q[:S[-1][1]]) | |
| x, y = (0, S[-1][1] - 1) | |
| S = [[0, 0]] + S | |
| l = 1 | |
| for j in range(S[-1][1] - 1, -1, -1): | |
| if x > y: | |
| break | |
| if j < S[-l][1]: | |
| k = S[-l][0] | |
| l += 1 | |
| if k == 1: | |
| q[j] = Q1[y] | |
| y -= 1 | |
| if k == 2: | |
| q[j] = Q1[x] | |
| x += 1 | |
| print(' '.join(list(map(str, q)))) | |
| ``` | |
| ### 195 | |
| ```python | |
| n, c = list(map(int, input().split(' '))) | |
| nums = list(map(int, input().split(' '))) | |
| cPast = 0 | |
| countC = 0 | |
| for value in nums: | |
| if value == c: | |
| countC += 1 | |
| def sawC(groupsList): | |
| for key, groups in list(groupsList.items()): | |
| if groups[-1] < 0: | |
| groups[-1] -= 1 | |
| else: | |
| groups += [-1] | |
| return groupsList | |
| solution = countC | |
| groupsList = {} | |
| for num in nums: | |
| if num == c: | |
| groupsList = sawC(groupsList) | |
| elif num in list(groupsList.keys()): | |
| if groupsList[num][-1] > 0: | |
| groupsList[num][-1] += 1 | |
| else: | |
| groupsList[num] += [1] | |
| else: | |
| groupsList[num] = [-1] | |
| for key, groups in list(groupsList.items()): | |
| maxDiff = 1 | |
| currDiff = 0 | |
| newDiff = 0 | |
| for group in groups: | |
| currDiff += group | |
| if group > currDiff: | |
| currDiff = group | |
| if currDiff > maxDiff: | |
| maxDiff = currDiff | |
| if maxDiff + countC > solution: | |
| solution = countC + maxDiff | |
| print(solution) | |
| ``` | |
| ### 196 | |
| ```python | |
| import operator | |
| data = list(map(int, input().split(' '))) | |
| n = data[0] | |
| m = data[1] | |
| edge = {} | |
| for i in range(n): | |
| edge[i + 1] = [] | |
| for _ in range(m): | |
| data = list(map(int, input().split(' '))) | |
| i = data[0] | |
| j = data[1] | |
| edge[i].append(j) | |
| edge[j].append(i) | |
| candidate = [k for k, v in list(edge.items()) if len(v) == 2] | |
| result = 0 | |
| used = {} | |
| def _iscircle(c, result): | |
| n = edge[c][0] | |
| used[c] = 1 | |
| last = c | |
| while n not in used: | |
| used[n] = 1 | |
| if len(edge[n]) != 2: | |
| return result | |
| t = edge[n][0] | |
| if t != last: | |
| last = n | |
| if t == c: | |
| return result + 1 | |
| n = t | |
| continue | |
| last = n | |
| t = edge[n][1] | |
| if not t == c: | |
| return result + 1 | |
| n = t | |
| return result | |
| for i in candidate: | |
| result = _iscircle(i, result) | |
| print(result) | |
| ``` | |
| ### 197 | |
| ```python | |
| import sys | |
| line = sys.stdin.readline().strip().split() | |
| n = int(line[0]) | |
| c = int(line[1]) | |
| a = list(map(int, sys.stdin.readline().strip().split())) | |
| cright = [0] * (n + 1) | |
| maxfreq = [1] * (5 * 10 ** 5 + 1) | |
| for i in range(0, n): | |
| if a[n - 1 - i] == c: | |
| cright[n - 1 - i] = cright[n - i] + 1 | |
| else: | |
| cright[n - 1 - i] = cright[n - i] | |
| result = cright[0] | |
| cleft = 0 | |
| for i in range(0, n): | |
| if a[i] != c: | |
| maxfreq[a[i]] = max([maxfreq[a[i]], cleft]) + 1 | |
| else: | |
| cleft = cleft + 1 | |
| if maxfreq[a[i]] + cright[i + 1] > result: | |
| result = maxfreq[a[i]] + cright[i + 1] | |
| print(result) | |
| ``` | |
| ### 198 | |
| ```python | |
| import collections | |
| import bisect | |
| def f(): | |
| n, m = [int(c) for c in input().split()] | |
| s = list(input()) | |
| ll = [] | |
| start = 0 | |
| length = 0 | |
| inside = False | |
| cc = 0 | |
| for i in range(len(s)): | |
| if s[i] == '.': | |
| if not inside: | |
| inside = True | |
| length += 1 | |
| elif inside: | |
| cc += length - 1 | |
| length = 0 | |
| inside = False | |
| if inside: | |
| cc += length - 1 | |
| res = [0] * m | |
| for i in range(m): | |
| line = input().split() | |
| j = int(line[0]) - 1 | |
| c = line[1] | |
| if c == '.' and s[j] == '.' or (c != '.' and s[j - 1] != '.'): | |
| res[i] = cc | |
| else: | |
| e1 = False | |
| e2 = False | |
| if j > 0 and s[j - 1] == '.': | |
| e1 = True | |
| if j < n - 1 and s[j + 1] == '.': | |
| e2 = True | |
| if e1 and e2: | |
| cc += 2 if c == '.' else -2 | |
| elif e1 or e2: | |
| cc += 1 if c == '.' else -1 | |
| s[j] = c | |
| res[i] = cc | |
| for r in res: | |
| print(r) | |
| f() | |
| ``` | |
| ### 199 | |
| ```python | |
| from math import ceil | |
| def __starting_point(): | |
| n = int(input()) | |
| arr = list(map(int, input().split())) | |
| narr = [True] | |
| for i in range(1, n - 1): | |
| x = arr[i] | |
| narr.append(x == arr[i - 1] or x == arr[i + 1]) | |
| narr.append(True) | |
| cnt = 0 | |
| mc = 0 | |
| for x in narr: | |
| if not x: | |
| cnt += 1 | |
| if x and cnt: | |
| mc = max(mc, cnt) | |
| cnt = 0 | |
| if cnt: | |
| mc = max(mc, cnt) | |
| print(ceil(mc / 2)) | |
| ss = None | |
| for i, x in enumerate(arr): | |
| if not narr[i]: | |
| if ss is None: | |
| ss = i | |
| elif ss is not None: | |
| if arr[ss - 1] == x: | |
| for j in range(ss, i): | |
| arr[j] = x | |
| else: | |
| for j in range(ss, i): | |
| arr[j] = arr[ss - 1] if j <= (i + ss) / 2 else x | |
| ss = None | |
| print(*arr) | |
| __starting_point() | |
| ``` | |
| ### 200 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i - 1] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 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. | |