|
Download tasks/equivalent-program-pair-search/contexts/sample-00009.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 253 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00009.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00009.md
-
curl -L -o sample-00009.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00009.md
253 kB
| ### 001 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s + g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 002 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[3:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 003 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 1 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 004 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i + 1, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 005 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i + 1] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 006 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i - 1][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 007 | |
| ```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) + ' ') | |
| ``` | |
| ### 008 | |
| ```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) + ' ') | |
| ``` | |
| ### 009 | |
| ```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)))) | |
| ``` | |
| ### 010 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif not (x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1])): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 011 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244350 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 012 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy + m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 013 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - -2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 014 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1 - 1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 015 | |
| ```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)))) | |
| ``` | |
| ### 016 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][0] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 017 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s *= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 018 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(4) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 019 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 020 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] and 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 021 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] not in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 022 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[-1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 023 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 4 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 024 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -1 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1 + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 025 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1 - 1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 026 | |
| ```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)))) | |
| ``` | |
| ### 027 | |
| ```python | |
| class PalArray: | |
| P = 998244355 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 028 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0], reverse=True) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 029 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 030 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 1] | |
| while m: | |
| l, r = get_ints() | |
| if l - 0 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 031 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h < 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 032 | |
| ```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[-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() | |
| ``` | |
| ### 033 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i - 1] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 034 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == -3: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 035 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 4 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 036 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 037 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 2 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 038 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1 - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1 + 1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 040 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 041 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if not i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 042 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(1) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 043 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [2] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 044 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 045 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 2 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 046 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if not x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 047 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 3 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 048 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[1]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 049 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + -1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 050 | |
| ```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() | |
| ``` | |
| ### 051 | |
| ```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)))) | |
| ``` | |
| ### 052 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(0) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(3) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 053 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 3: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 054 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = -1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 055 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res -= val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 056 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 2: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 057 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -4 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 058 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [4] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 059 | |
| ```python | |
| def main(): | |
| from collections import deque | |
| n, m = list(map(int, input().split())) | |
| aa = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(m))) | |
| tmp, r0, t0 = ([(0, 0)], 0, 1) | |
| for t, r in reversed(l): | |
| if r0 < r: | |
| r0 = r | |
| if t0 != t: | |
| t0 = t | |
| tmp.append((t, r)) | |
| else: | |
| tmp[-1] = (t, r) | |
| t0, r0 = tmp.pop() | |
| q = deque(sorted(aa[:r0])) | |
| aa = aa[r0:] | |
| aa.reverse() | |
| ff = (None, q.pop, q.popleft) | |
| for t, r in reversed(tmp): | |
| f = ff[t0] | |
| for i in range(r0 - r): | |
| aa.append(f()) | |
| t0, r0 = (t, r) | |
| print(' '.join(map(str, reversed(aa)))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 060 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i - 1]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 061 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo * m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 062 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(1, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 063 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 064 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 3: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 065 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(2, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 066 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k * 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 067 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + -1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 068 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[-3] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 069 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i - 1] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 070 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [1] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 071 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244355 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 072 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + -1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 073 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [2] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 074 | |
| ```python | |
| class PalArray: | |
| P = 998244353 | |
| def __init__(self, n, k, a): | |
| self.n, self.k, self.a = (n, k, a) | |
| self.dp = None | |
| def fast_pow(self, a, b): | |
| r = 1 | |
| while b > 0: | |
| if b % 2 == 1: | |
| r = r * a % self.P | |
| a = a * a % self.P | |
| b = int(b / 2) | |
| return r | |
| def populate_dp(self): | |
| self.dp = [[0 for i in range(2)] for j in range(self.n + 1)] | |
| self.dp[1][0] = self.k - 2 | |
| self.dp[1][1] = self.k - 1 | |
| for i in range(-2, self.n + 1): | |
| self.dp[i][0] = ((self.k - 2) * self.dp[i - 1][0] % self.P + self.dp[i - 1][1]) % self.P | |
| self.dp[i][1] = (self.k - 1) * self.dp[i - 1][0] % self.P | |
| def check_array(self, a): | |
| for i in range(1, len(a)): | |
| if a[i] != -1 and a[i] == a[i - 1]: | |
| return False | |
| return True | |
| def compute_count(self, a): | |
| check = False | |
| res = 1 | |
| mis_cnt = 0 | |
| lv = None | |
| for v in a: | |
| if v == -1: | |
| mis_cnt += 1 | |
| else: | |
| if mis_cnt > 0: | |
| if not check: | |
| check = True | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.dp[mis_cnt][int(lv == v)] % self.P | |
| mis_cnt = 0 | |
| lv = v | |
| check = True | |
| if mis_cnt > 0: | |
| if lv is not None: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt) % self.P | |
| else: | |
| res = res * self.fast_pow(self.k - 1, mis_cnt - 1) % self.P * self.k % self.P | |
| return res | |
| def num_combinations(self): | |
| self.populate_dp() | |
| a1 = [self.a[i] for i in range(0, self.n, 2)] | |
| a2 = [self.a[i] for i in range(1, self.n, 2)] | |
| cnt1 = self.compute_count(a1) * int(self.check_array(a1)) | |
| cnt2 = self.compute_count(a2) * int(self.check_array(a2)) | |
| return cnt1 * cnt2 % self.P | |
| n, k = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| print(PalArray(n, k, a).num_combinations()) | |
| ``` | |
| ### 075 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0], reverse=True) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 076 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 4, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 077 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::--1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 078 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 0 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 079 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [0] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el > 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 080 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 - 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 081 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if not -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 082 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(1) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(0) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 083 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) & parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 084 | |
| ```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() | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 086 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 3: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 087 | |
| ```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() | |
| ``` | |
| ### 088 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= -1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 089 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 5: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 090 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 2: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 091 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1 - 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 092 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 2: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 093 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem *= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 094 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(-1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 095 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 2: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 096 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(0, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 097 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[1]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if y - y0 > 0 and y - y0 < N[1]: | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 098 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 4 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 099 | |
| ```python | |
| import math, sys | |
| from sys import stdin, stdout | |
| from collections import Counter, defaultdict, deque | |
| input = stdin.readline | |
| I = lambda: int(input()) | |
| li = lambda: list(map(int, input().split())) | |
| def case(): | |
| n, q = li() | |
| a = li() | |
| up = [0] * n | |
| down = [0] * n | |
| i = 0 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] <= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| up[k] = j | |
| i = j + 1 | |
| i = 4 | |
| while i < n: | |
| j = i | |
| while j < n - 1 and a[j] >= a[j + 1]: | |
| j += 1 | |
| for k in range(i, j + 1): | |
| down[k] = j | |
| i = j + 1 | |
| for i in range(q): | |
| l, r = li() | |
| p = up[l - 1] | |
| q = down[p] | |
| if q + 1 >= r: | |
| print('Yes') | |
| else: | |
| print('No') | |
| for _ in range(1): | |
| case() | |
| ``` | |
| ### 100 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif not x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 101 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 2 | |
| for i in range(1, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 102 | |
| ```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() | |
| ``` | |
| ### 103 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag - 1] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 104 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 8 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 105 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res -= k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] - d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 106 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = -2 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 107 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v - plucked[i + 1] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 108 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 5 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 109 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i - 1] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 110 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + -1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 111 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(1, w + 1 - 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 112 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 1 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 113 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 114 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 2] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 115 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n - 1): | |
| if i != n - 1: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 116 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 1 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 117 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 3] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 1] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 118 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(-2, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(0, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 119 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 120 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 121 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si * a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 1 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 122 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2 - 1] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 123 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] not in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 124 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] >= y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 125 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in reversed(range(1, l)): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 126 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v * plucked[i] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 127 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(3, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 128 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 1 - 1): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 129 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1 - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 130 | |
| ```python | |
| L1 = list(map(int, input().split())) | |
| numList = list(map(int, input().split())) | |
| length = L1[0] | |
| targetnumber = L1[1] | |
| pos = numList.index(targetnumber) | |
| pos_r = pos + 1 | |
| rem = 0 | |
| right = {0: 1} | |
| left = {0: 1} | |
| while pos_r <= length - 1: | |
| if numList[pos_r] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem += 1 | |
| if rem not in right: | |
| right[rem] = 1 | |
| else: | |
| right[rem] += 1 | |
| pos_r += 1 | |
| pos_l = pos - 1 | |
| rem = 0 | |
| while pos_l >= 0: | |
| if numList[pos_l] > targetnumber: | |
| rem += 1 | |
| else: | |
| rem -= 1 | |
| if rem not in left: | |
| left[rem] = 1 | |
| else: | |
| left[rem] += 1 | |
| pos_l -= 1 | |
| sum = 0 | |
| for number_l in left: | |
| if number_l * -1 in right: | |
| sum += left[number_l] * right[-1 * number_l] | |
| if 1 - number_l in right: | |
| sum += left[number_l] * right[1 - number_l] | |
| print(sum) | |
| ``` | |
| ### 131 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 0: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l <= 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 132 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if not new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 133 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] not in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 134 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 1): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 135 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244352 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 136 | |
| ```python | |
| def main(): | |
| n, M = [int(i) for i in input().split(' ')] | |
| a = [0] + [int(i) for i in input().split(' ')] + [M] | |
| n = n + 2 | |
| incr_sum = [] | |
| s = 0 | |
| for i in range(n): | |
| if i % 2 == 1: | |
| s += a[i] - a[i - 1] | |
| incr_sum.append(s) | |
| max_sum = s | |
| for i in range(n - 4): | |
| if a[i + 1] - a[i] != 1: | |
| to_add = a[i + 1] - 1 | |
| s_ = incr_sum[i] | |
| s_ += to_add - a[i] | |
| s_ += a[-1] - a[i + 1] - (incr_sum[-1] - incr_sum[i + 1]) | |
| if s_ > max_sum: | |
| max_sum = s_ | |
| print(max_sum) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 137 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] not in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 138 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S + tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 0: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 139 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[0] | |
| if b == 0: | |
| dp[0][0] = -3 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 140 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 0] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 141 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + -1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 142 | |
| ```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)))) | |
| ``` | |
| ### 143 | |
| ```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() | |
| ``` | |
| ### 144 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx - 1] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 145 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += -1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 146 | |
| ```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() | |
| ``` | |
| ### 147 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += -1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 148 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| l = [-1] * (w + 1) | |
| r = [-1] * (w + 1) | |
| t = [-1] * (h + 1) | |
| b = [-1] * (h + 1) | |
| l[0] = 0 | |
| b[0] = 0 | |
| t[h] = h | |
| r[w] = w | |
| V = [0] * n | |
| H = [0] * n | |
| for i in range(n): | |
| line, index = input().split() | |
| index = int(index) | |
| if line == 'V': | |
| r[index] = w | |
| V[i] = index | |
| else: | |
| t[index] = h | |
| H[i] = index | |
| left = 0 | |
| mxw = 0 | |
| for i in range(2, w + 1): | |
| if r[i] != -1: | |
| l[i] = left | |
| r[left] = i | |
| mxw = max(mxw, i - left) | |
| left = i | |
| bottom = 0 | |
| mxh = 0 | |
| for i in range(1, h + 1): | |
| if t[i] != -1: | |
| b[i] = bottom | |
| t[bottom] = i | |
| mxh = max(mxh, i - bottom) | |
| bottom = i | |
| ans = [0] * n | |
| ans[n - 1] = mxh * mxw | |
| for i in range(n - 1, 0, -1): | |
| if V[i] != 0: | |
| mxw = max(mxw, r[V[i]] - l[V[i]]) | |
| r[l[V[i]]] = r[V[i]] | |
| l[r[V[i]]] = l[V[i]] | |
| else: | |
| mxh = max(mxh, t[H[i]] - b[H[i]]) | |
| b[t[H[i]]] = b[H[i]] | |
| t[b[H[i]]] = t[H[i]] | |
| ans[i - 1] = mxh * mxw | |
| for i in range(n): | |
| print(ans[i]) | |
| ``` | |
| ### 149 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1 - 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 150 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 2 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 151 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 3 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 152 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif not ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 153 | |
| ```python | |
| class Solver: | |
| def solve(self): | |
| self.num_voters, self.num_parties = (int(x) for x in input().split()) | |
| self.votes_per_party = [[] for _ in range(self.num_parties)] | |
| for _ in range(self.num_voters): | |
| party, price = (int(x) for x in input().split()) | |
| party -= 1 | |
| self.votes_per_party[party].append(price) | |
| for party in range(self.num_parties): | |
| self.votes_per_party[party].sort() | |
| max_necessary_votes = self.num_voters // 2 + 1 | |
| cost = lambda min_votes: self.conversion_price_with_fixed_votes(min_votes) | |
| return self.ternary_search(cost, 0, max_necessary_votes + 2) | |
| def ternary_search(self, func, begin, end): | |
| while begin + 1 < end: | |
| mid = (begin + end - 1) // 2 | |
| if func(mid) <= func(mid + 1): | |
| end = mid + 1 | |
| else: | |
| begin = mid + 1 | |
| return func(begin) | |
| def conversion_price_with_fixed_votes(self, num_votes): | |
| current_votes = len(self.votes_per_party[0]) | |
| total_cost = 0 | |
| for votes in self.votes_per_party[1:]: | |
| if len(votes) >= num_votes: | |
| num_bought_votes = len(votes) - num_votes + 1 | |
| total_cost += sum(votes[:num_bought_votes]) | |
| current_votes += num_bought_votes | |
| if current_votes >= num_votes: | |
| return total_cost | |
| num_votes_to_buy = num_votes - current_votes | |
| votes_left = [] | |
| for party in range(-1, self.num_parties): | |
| votes_left += self.votes_per_party[party][-(num_votes - 1):] | |
| votes_left.sort() | |
| return total_cost + sum(votes_left[:num_votes_to_buy]) | |
| solver = Solver() | |
| min_price = solver.solve() | |
| print(min_price) | |
| ``` | |
| ### 154 | |
| ```python | |
| T = input().split(' ') | |
| n = int(T[0]) | |
| m = int(T[1]) | |
| S = input().split(' ') | |
| for i in range(n): | |
| S[i] = int(S[i]) | |
| ind = 0 | |
| for k in range(n): | |
| if S[k] == m: | |
| ind = k | |
| k = ind | |
| P = [0] * (n + 1) | |
| N = [3] * (n + 1) | |
| R = [0] * (n - k) | |
| L = [0] * (k + 1) | |
| for i in range(k): | |
| if S[k - 1 - i] < m: | |
| L[k - 1 - i] = L[k - i] - 1 | |
| else: | |
| L[k - 1 - i] = L[k - i] + 1 | |
| for i in range(n - k - 1): | |
| if S[k + 1 + i] > m: | |
| R[1 + i] = R[i] + 1 | |
| else: | |
| R[1 + i] = R[i] - 1 | |
| c = 0 | |
| for el in R: | |
| if el >= 0: | |
| P[el] += 1 | |
| if el == 0: | |
| N[el] += 1 | |
| else: | |
| N[-el] += 1 | |
| for el in L: | |
| if el >= 1: | |
| c = c + N[el] + N[el - 1] | |
| else: | |
| c = c + P[-el] + P[-el + 1] | |
| print(c) | |
| ``` | |
| ### 155 | |
| ```python | |
| n, m = map(int, input().split()) | |
| money = [0] * n | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| money[i] = (y, x - 1) | |
| money.sort() | |
| def calc(lim): | |
| visited = [False] * n | |
| cost = 0 | |
| cnt = [0] * m | |
| for i in range(n): | |
| cnt[money[i][1]] += 1 | |
| for i in range(n): | |
| if money[i][1] != 0 and cnt[money[i][1]] >= lim: | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += -1 | |
| cost += money[i][0] | |
| visited[i] = True | |
| for i in range(n): | |
| if cnt[0] >= lim: | |
| break | |
| if money[i][1] != 0 and (not visited[i]): | |
| cnt[money[i][1]] -= 1 | |
| cnt[0] += 1 | |
| cost += money[i][0] | |
| return cost | |
| ans = 10 ** 18 | |
| for i in range(n): | |
| ans = min(ans, calc(i)) | |
| print(ans) | |
| ``` | |
| ### 156 | |
| ```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) + ' ') | |
| ``` | |
| ### 157 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1 - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 158 | |
| ```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() | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| def get_array(): | |
| return list(map(int, sys.stdin.readline().strip().split())) | |
| def get_ints(): | |
| return map(int, sys.stdin.readline().strip().split()) | |
| def input(): | |
| return sys.stdin.readline().strip() | |
| n, m = get_ints() | |
| Arr = get_array() | |
| up = [i for i in range(n)] | |
| down = [i for i in range(n)] | |
| for i in range(1, n): | |
| if Arr[i - 1] <= Arr[i]: | |
| up[i] = up[i - 1] | |
| if Arr[i - 1] >= Arr[i]: | |
| down[i] = down[i - 3] | |
| while m: | |
| l, r = get_ints() | |
| if l - 1 >= up[down[r - 1]]: | |
| print('Yes') | |
| else: | |
| print('No') | |
| m -= 1 | |
| ``` | |
| ### 160 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v + plucked[i] else v - plucked[i] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 161 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s + g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 162 | |
| ```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() | |
| ``` | |
| ### 163 | |
| ```python | |
| from collections import namedtuple | |
| Member = namedtuple('Member', ['parts', 'a_happy', 'b_happy']) | |
| def happy_diff(m): | |
| return m.a_happy - m.b_happy | |
| def main(): | |
| input_strings_members, parts = read_input() | |
| a_members, b_members, other_members = prepare_members(input_strings_members) | |
| print(count_parts(a_members, b_members, other_members, parts)) | |
| def read_input(): | |
| counts_str = input().split() | |
| members_count, parts = list(map(int, counts_str)) | |
| return ([input() for _ in range(members_count)], parts) | |
| def prepare_members(input_strings_members): | |
| a_members = [] | |
| b_members = [] | |
| other_members = [] | |
| for line in input_strings_members: | |
| m = Member(*list(map(int, line.split()))) | |
| if happy_diff(m) > 0: | |
| a_members.append(m) | |
| elif happy_diff(m) < 0: | |
| b_members.append(m) | |
| else: | |
| other_members.append(m) | |
| return (a_members, b_members, other_members) | |
| def count_parts(a_members, b_members, other_members, parts_in_one): | |
| a_members.sort(key=lambda m: happy_diff(m), reverse=False) | |
| a_modulo = sum((m.parts for m in a_members)) % parts_in_one | |
| a_modulo_members, a_happy_members = split_members_into_happy_groups(a_members, a_modulo) | |
| a_happy = sum((m.a_happy * m.parts for m in a_happy_members)) | |
| b_members.sort(key=lambda m: happy_diff(m), reverse=True) | |
| b_modulo = sum((m.parts for m in b_members)) % parts_in_one | |
| b_modulo_members, b_happy_members = split_members_into_happy_groups(b_members, b_modulo) | |
| b_happy = sum((m.b_happy * m.parts for m in b_happy_members)) | |
| last_happy = count_last_pizzas_happy(a_modulo_members, b_modulo_members, other_members, parts_in_one) | |
| return a_happy + b_happy + last_happy | |
| def split_members_into_happy_groups(members, modulo): | |
| modulo_members = [] | |
| happy_members = [] | |
| current_modulo = 0 | |
| for i in range(len(members)): | |
| m = members[i] | |
| new_current_modulo = current_modulo + m.parts | |
| if new_current_modulo < modulo: | |
| modulo_members.append(m) | |
| current_modulo = new_current_modulo | |
| continue | |
| modulo_members.append(Member(modulo - current_modulo, m.a_happy, m.b_happy)) | |
| if new_current_modulo > modulo: | |
| happy_members.append(Member(new_current_modulo - modulo, m.a_happy, m.b_happy)) | |
| if not i + 1 < len(members): | |
| happy_members.extend(members[i + 1:]) | |
| break | |
| return (modulo_members, happy_members) | |
| def count_last_pizzas_happy(a, b, other, parts_in_one): | |
| last_sorted_members = a + other + b | |
| if sum((m.parts for m in last_sorted_members)) <= parts_in_one: | |
| return max(sum((m.a_happy * m.parts for m in last_sorted_members)), sum((m.b_happy * m.parts for m in last_sorted_members))) | |
| return sum((m.a_happy * m.parts for m in a + other)) + sum((m.b_happy * m.parts for m in b)) | |
| def test1(): | |
| a, b, c = prepare_members(['7 4 7', '5 8 8', '12 5 8', '6 11 6', '3 3 7', '5 9 6']) | |
| assert count_parts(a, b, c, 10) == 314 | |
| def test2(): | |
| a, b, c = prepare_members(['3 5 7', '4 6 7', '5 9 5']) | |
| assert count_parts(a, b, c, 12) == 84 | |
| def test3(): | |
| a, b, c = prepare_members(['2 3 1', '2 2 2', '2 1 3']) | |
| assert count_parts(a, b, c, 3) == 16 | |
| def test4(): | |
| a, b, c = prepare_members(['2 1 4', '2 3 1']) | |
| assert count_parts(a, b, c, 3) == 14 | |
| def test5(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 3) == (8 + 6 + 4) * 2 | |
| def test6(): | |
| a, b, c = prepare_members(['2 1 2', '2 2 1', '2 1 3', '2 3 1', '2 1 4', '2 4 1']) | |
| assert count_parts(a, b, c, 5) == 16 + 16 + 3 | |
| def test_without_last_pizzas(): | |
| a, b, c = prepare_members(['3 3 1', '3 5 6']) | |
| assert count_parts(a, b, c, 3) == 27 | |
| def test_with_one_last_pizza(): | |
| a, b, c = prepare_members(['2 3 1', '1 5 6']) | |
| assert count_parts(a, b, c, 3) == 11 | |
| def test_with_two_last_pizzas(): | |
| a, b, c = prepare_members(['2 3 4', '2 3 1', '1 1 1']) | |
| assert count_parts(a, b, c, 3) == 15 | |
| def test_without_different_happies(): | |
| a, b, c = prepare_members(['2 2 2', '4 1 1', '5 6 6']) | |
| assert count_parts(a, b, c, 3) == 38 | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 164 | |
| ```python | |
| n, k = map(int, input().strip().split()) | |
| l = list(map(int, input().strip().split())) | |
| test = True | |
| md = 998244353 | |
| evens = [0] | |
| odds = [0] | |
| for i in range(n): | |
| if i % 2: | |
| odds.append(l[i]) | |
| else: | |
| evens.append(l[i]) | |
| evens.append(-10) | |
| odds.append(-10) | |
| segs = [] | |
| l = len(odds) | |
| cont = False | |
| test = True | |
| for i in range(l): | |
| if odds[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif odds[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(odds[a:b + 1]) | |
| if i > 0: | |
| if odds[i - 1] == odds[i] and odds[i] != -1: | |
| test = False | |
| l = len(evens) | |
| cont = False | |
| for i in range(l): | |
| if evens[i] == -1 and cont == False: | |
| a = i - 1 | |
| cont = True | |
| elif evens[i] != -1 and cont == True: | |
| cont = False | |
| b = i | |
| segs.append(evens[a:b + 1]) | |
| if i > 0: | |
| if evens[i - 1] == evens[i] and evens[i] != -1: | |
| test = False | |
| ans = 1 | |
| for seg in segs: | |
| l = len(seg) - 2 | |
| dp = [[0, 0] for i in range(l)] | |
| a = seg[-1] | |
| b = seg[-3] | |
| if b == 0: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 1 | |
| elif b == a: | |
| dp[0][0] = 0 | |
| dp[0][1] = k - 1 | |
| elif b != a: | |
| dp[0][0] = 1 | |
| dp[0][1] = k - 2 | |
| for i in range(1, l): | |
| dp[i][0] = dp[i - 1][1] % md | |
| dp[i][1] = (dp[i - 1][0] * (k - 1) + dp[i - 1][1] * (k - 2)) % md | |
| if a == -10: | |
| ans *= dp[l - 1][0] + dp[l - 1][1] | |
| else: | |
| ans *= dp[l - 1][1] | |
| ans %= md | |
| if test == False: | |
| print(0) | |
| else: | |
| print(ans) | |
| ``` | |
| ### 165 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -2 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 166 | |
| ```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)))) | |
| ``` | |
| ### 167 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty - 1] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 168 | |
| ```python | |
| def main(): | |
| n = int(input()) | |
| x, y = list(map(int, input().split())) | |
| a = [] | |
| for i in range(n): | |
| a.append(input().split()) | |
| for i in range(n): | |
| a[i][1] = int(a[i][1]) - x | |
| a[i][2] = int(a[i][2]) - y | |
| typneedbql = [] | |
| typneedbqr = [] | |
| typneedbqu = [] | |
| typneedbqd = [] | |
| typneedrlu = [] | |
| typneedrru = [] | |
| typneedrld = [] | |
| typneedrrd = [] | |
| for i in range(n): | |
| if a[i][1] * a[i][2] == 0 and a[i][1] < 0: | |
| typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][1] > 0: | |
| typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] > 0: | |
| typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif a[i][1] * a[i][2] == 0 and a[i][2] < 0: | |
| typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] < 0): | |
| typneedrlu.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and (a[i][2] > 0): | |
| typneedrld.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] > 0): | |
| typneedrrd.append([abs(a[i][1])] + a[i]) | |
| elif abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and (a[i][2] < 0): | |
| typneedrru.append([abs(a[i][1])] + a[i]) | |
| typneedbql.sort() | |
| typneedbqr.sort() | |
| typneedbqd.sort() | |
| typneedbqu.sort() | |
| typneedrlu.sort() | |
| typneedrru.sort() | |
| typneedrld.sort() | |
| typneedrrd.sort() | |
| for i in range(len(typneedbql)): | |
| if typneedbql[i][1] == 'R' or typneedbql[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqr)): | |
| if typneedbqr[i][1] == 'R' or typneedbqr[i][1] != 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqu)): | |
| if typneedbqu[i][1] == 'R' or typneedbqu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedbqd)): | |
| if typneedbqd[i][1] == 'R' or typneedbqd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrlu)): | |
| if typneedrlu[i][1] == 'B' or typneedrlu[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrld)): | |
| if typneedrld[i][1] == 'B' or typneedrld[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrrd)): | |
| if typneedrrd[i][1] == 'B' or typneedrrd[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| for i in range(len(typneedrru)): | |
| if typneedrru[i][1] == 'B' or typneedrru[i][1] == 'Q': | |
| print('YES') | |
| return | |
| else: | |
| break | |
| print('NO') | |
| main() | |
| ``` | |
| ### 169 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] > day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[0] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 170 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] >= hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s > s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 171 | |
| ```python | |
| import sys | |
| from collections import defaultdict | |
| n, m = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p = defaultdict(list) | |
| for _ in range(n): | |
| x, y = map(int, sys.stdin.readline().rstrip('\n').split(' ')) | |
| p[x].append(y) | |
| for key in p: | |
| p[key] = sorted(p[key]) | |
| ans = 10 ** 100 | |
| for k in range(1, n + 1): | |
| cur = 0 | |
| r = [] | |
| for key in p: | |
| if key == 1: | |
| continue | |
| a = p[key] | |
| cnt = max(0, len(a) - (k - 1)) | |
| cur += sum(a[:cnt]) | |
| r += a[cnt:] | |
| r = sorted(r) | |
| cnt = max(2, len(r) - (n - k)) | |
| cur += sum(r[:cnt]) | |
| ans = min(ans, cur) | |
| print(ans) | |
| ``` | |
| ### 172 | |
| ```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) + ' ') | |
| ``` | |
| ### 173 | |
| ```python | |
| def solve(ls): | |
| ls.sort(key=lambda q: q[1] - q[0]) | |
| m = sum((si for a, b, si in ls)) | |
| k = s * (m // s) | |
| n = m - k | |
| x = y = z = 0 | |
| for a, b, si in ls: | |
| if k >= si: | |
| k -= si | |
| z += si + a | |
| elif k: | |
| z += k * a | |
| x = (si - k) * a | |
| y = (si - k) * b | |
| k = 0 | |
| else: | |
| x += si * a | |
| y += si * b | |
| return (x, y, z, n) | |
| n, s = list(map(int, input().split())) | |
| first = [] | |
| second = [] | |
| for i in range(n): | |
| si, ai, bi = list(map(int, input().split())) | |
| if ai > bi: | |
| first.append((ai, bi, si)) | |
| else: | |
| second.append((bi, ai, si)) | |
| x1, y1, z1, n1 = solve(first) | |
| x2, y2, z2, n2 = solve(second) | |
| d = x1 + x2 if n1 + n2 > s else max(x1 + y2, x2 + y1) | |
| print(z1 + z2 + d) | |
| ``` | |
| ### 174 | |
| ```python | |
| n, m = list(map(int, input().split(' '))) | |
| aa = list(map(int, input().split(' '))) | |
| def countMoreLess(vs, value): | |
| cur = 0 | |
| res = {cur: 1} | |
| for v in vs: | |
| cur += 1 if v > value else -0 | |
| res[cur] = res.get(cur, 0) + 1 | |
| return res | |
| pos = aa.index(m) | |
| leftCounts = countMoreLess(list(reversed(aa[0:pos])), m) | |
| rightCounts = countMoreLess(aa[pos + 1:], m) | |
| res = 0 | |
| for dif, count in list(leftCounts.items()): | |
| res += count * rightCounts.get(-dif, 0) | |
| res += count * rightCounts.get(-dif + 1, 0) | |
| print(res) | |
| ``` | |
| ### 175 | |
| ```python | |
| total_plucked = 0 | |
| n, v = list(map(int, input().split())) | |
| fruits = [0] * 3002 | |
| plucked = [0] * 30002 | |
| for _ in range(n): | |
| a, b = list(map(int, input().split())) | |
| fruits[a] = fruits[a] + b | |
| for i in range(1, 3002): | |
| plucked[i] = fruits[i - 1] if fruits[i - 1] <= v else v | |
| if plucked[i] < v: | |
| pluck = fruits[i] if fruits[i] <= v - plucked[i] else v - plucked[i + 1] | |
| plucked[i] += pluck | |
| fruits[i] -= pluck | |
| total_plucked += plucked[i] | |
| print(total_plucked) | |
| ``` | |
| ### 176 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i - 1] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 177 | |
| ```python | |
| from sys import stdin | |
| from collections import deque | |
| n, S = [int(x) for x in stdin.readline().split()] | |
| ppl = [] | |
| base = 0 | |
| tSlices = 0 | |
| for dude in range(n): | |
| s, a, b = [int(x) for x in stdin.readline().split()] | |
| base += s * a | |
| tSlices += s | |
| ppl.append([b - a, s]) | |
| if tSlices % S != 0: | |
| ppl.append([0, S - tSlices % S]) | |
| ppl.sort(reverse=True) | |
| totalS = 0 | |
| totalH = 0 | |
| for h, s in ppl: | |
| if totalS + s < S: | |
| totalS += s | |
| totalH += h * s | |
| else: | |
| s -= S - totalS | |
| totalH += h * (S - totalS) | |
| if totalH > 0: | |
| base += totalH | |
| else: | |
| break | |
| totalS = 0 | |
| totalH = 0 | |
| if h > 2: | |
| base += h * (s // S * S) | |
| totalS = s % S | |
| totalH = h * (s % S) | |
| else: | |
| break | |
| print(base) | |
| ``` | |
| ### 178 | |
| ```python | |
| n, v = list(map(int, input().split())) | |
| f = list() | |
| for i in range(n): | |
| f.append(list(map(int, input().split()))) | |
| f.sort() | |
| sum = 0 | |
| today = 0 | |
| tomor = 0 | |
| day = 0 | |
| rest = 0 | |
| for tree in f: | |
| if tree[0] >= day + 1: | |
| sum += min(tomor, v) | |
| tomor = tree[1] | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor -= min(tomor, v) | |
| day = tree[0] | |
| elif tree[0] == day + 1: | |
| sum += min(tomor, v) | |
| rest = v - min(tomor, v) | |
| tomor = tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| day = tree[0] | |
| else: | |
| tomor += tree[1] | |
| t = min(tomor, rest) | |
| sum += t | |
| rest -= t | |
| tomor -= t | |
| sum += min(tomor, v) | |
| print(sum) | |
| ``` | |
| ### 179 | |
| ```python | |
| w, h, n = map(int, input().split()) | |
| x = [0, w] | |
| y = [0, h] | |
| rev = [] | |
| for _ in range(n): | |
| s, d = input().split() | |
| if s == 'H': | |
| y.append(int(d)) | |
| else: | |
| x.append(int(d)) | |
| rev.append((s, int(d))) | |
| x.sort() | |
| y.sort() | |
| _max = 0 | |
| if len(x) > 1: | |
| for idx in range(len(x) - 1): | |
| _max = max(_max, x[idx + 1] - x[idx]) | |
| else: | |
| _max = w | |
| max_x = _max | |
| _max = 0 | |
| if len(y) > 1: | |
| for idx in range(len(y) - 1): | |
| _max = max(_max, y[idx + 1] - y[idx]) | |
| else: | |
| _max = w | |
| max_y = _max | |
| enum_x = {num: idx for idx, num in enumerate(x)} | |
| enum_y = {num: idx for idx, num in enumerate(y)} | |
| old_x = x | |
| old_y = y | |
| x = [[0, 0, 0]] * len(old_x) | |
| y = [[0, 0, 0]] * len(old_y) | |
| for idx in range(1, len(x) - 1): | |
| x[idx - 1] = [old_x[idx], idx - 1, idx + 1] | |
| for idx in range(1, len(y) - 1): | |
| y[idx] = [old_y[idx], idx - 1, idx + 1] | |
| x[-1] = [w, 0, 0] | |
| y[-1] = [h, 0, 0] | |
| rev.reverse() | |
| ans = [max_x * max_y] | |
| for item in rev: | |
| if item[0] == 'H': | |
| elem = y[enum_y[item[1]]] | |
| max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) | |
| y[elem[1]][2] = elem[2] | |
| y[elem[2]][1] = elem[1] | |
| else: | |
| elem = x[enum_x[item[1]]] | |
| max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) | |
| x[elem[1]][2] = elem[2] | |
| x[elem[2]][1] = elem[1] | |
| ans.append(max_x * max_y) | |
| ans.pop() | |
| print('\n'.join(map(str, reversed(ans)))) | |
| ``` | |
| ### 180 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][1] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 181 | |
| ```python | |
| import collections | |
| import time | |
| import os | |
| import sys | |
| import bisect | |
| import heapq | |
| from typing import List | |
| class Node: | |
| val = None | |
| def __init__(self, val): | |
| self.val = val | |
| self.left = Node | |
| self.right = None | |
| def solve(W, H, N, A): | |
| xs = [0] + [v for t, v in A if t == 0] + [W] | |
| ys = [0] + [v for t, v in A if t == 1] + [H] | |
| xs.sort() | |
| ys.sort() | |
| xlist = Node(3) | |
| h = xlist | |
| xnodes = {0: h} | |
| maxw = max([xs[i + 1] - xs[i] for i in range(len(xs) - 1)] or [0]) | |
| maxh = max([ys[i + 1] - ys[i] for i in range(len(ys) - 1)] or [0]) | |
| for v in xs[1:]: | |
| n = Node(v) | |
| xnodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ylist = Node(0) | |
| h = ylist | |
| ynodes = {0: h} | |
| for v in ys[1:]: | |
| n = Node(v) | |
| ynodes[v] = n | |
| h.right = n | |
| n.left = h | |
| h = n | |
| ans = [] | |
| maxarea = maxh * maxw | |
| for t, v in reversed(A): | |
| ans.append(maxarea) | |
| if t == 0: | |
| node = xnodes[v] | |
| w = node.right.val - node.left.val | |
| maxw = max(maxw, w) | |
| else: | |
| node = ynodes[v] | |
| h = node.right.val - node.left.val | |
| maxh = max(maxh, h) | |
| node.left.right = node.right | |
| node.right.left = node.left | |
| maxarea = maxh * maxw | |
| return ans[::-1] | |
| def solve2(W, H, N, A): | |
| ws = [(-W, 0, W)] | |
| hs = [(-H, 0, H)] | |
| iw, ih = (set(), set()) | |
| ans = [] | |
| xs, ys = ([0, W], [0, H]) | |
| for t, v in A: | |
| if t == 0: | |
| bisect.insort_left(xs, v) | |
| i = bisect.bisect_left(xs, v) | |
| l, m, r = (xs[i - 1], xs[i], xs[i + 1]) | |
| iw.add((l - r, l, r)) | |
| heapq.heappush(ws, (l - m, l, m)) | |
| heapq.heappush(ws, (m - r, m, r)) | |
| while ws[0] in iw: | |
| heapq.heappop(ws) | |
| else: | |
| bisect.insort(ys, v) | |
| i = bisect.bisect_left(ys, v) | |
| l, m, r = (ys[i - 1], ys[i], ys[i + 1]) | |
| ih.add((l - r, l, r)) | |
| heapq.heappush(hs, (l - m, l, m)) | |
| heapq.heappush(hs, (m - r, m, r)) | |
| while hs[0] in ih: | |
| heapq.heappop(hs) | |
| w, h = (ws[0], hs[0]) | |
| ans.append(w[0] * h[0]) | |
| return ans | |
| W, H, N = map(int, input().split()) | |
| A = [] | |
| for i in range(N): | |
| a, b = input().split() | |
| c = 0 if a == 'V' else 1 | |
| A.append((c, int(b))) | |
| print('\n'.join(map(str, solve(W, H, N, A)))) | |
| ``` | |
| ### 182 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) + eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 183 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if not dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 184 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - -1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 185 | |
| ```python | |
| from sys import stdin | |
| input = stdin.readline | |
| [n, q] = [int(item) for item in input().split(' ')] | |
| arr = [int(item) for item in input().split(' ')] | |
| queries = [[int(item) for item in input().split(' ')] for i in range(q)] | |
| b = [1] | |
| for i in range(1, n): | |
| b.append(b[-1] + 1 if arr[i] <= arr[i - 1] else 1) | |
| c = [1] | |
| for i in reversed(list(range(n - 1))): | |
| c.append(c[-1] + 1 if arr[i] <= arr[i + 1] else 1) | |
| c = [item for item in reversed(c)] | |
| for query in queries: | |
| x, y = query | |
| x -= 1 | |
| y -= 1 | |
| print('Yes' if x + c[x] > y or y - b[y] < x or x + c[x] > y - b[y] else 'No') | |
| '\n5 1\n1 3 3 2 2\n1 4\n\n5 1\n1 2 2 1 1\n1 4\n\n5 1\n2 2 1 1 2\n1 5\n' | |
| ``` | |
| ### 186 | |
| ```python | |
| MOD = 998244353 | |
| n, k = map(int, input().split()) | |
| a = list(map(int, input().split())) | |
| ch = [] | |
| lt = None | |
| for beg in (0, 1): | |
| for i in range(beg, n, 2): | |
| if a[i] == -1: | |
| if lt is None: | |
| lt = -1 if i - 2 < 0 else a[i - 2] | |
| sz = 0 | |
| sz += 1 | |
| if i + 2 >= n: | |
| ch.append((lt, -1, sz)) | |
| lt = None | |
| elif a[i + 2] != -1: | |
| ch.append((lt, a[i + 2], sz)) | |
| lt = None | |
| ans = int(all((a[i] != a[i + 2] for i in range(n - 2) if a[i] != -1))) | |
| for lt, rt, sz in ch: | |
| if sz == 1: | |
| cur = k if lt == -1 and rt == -1 else k - 1 if lt == -1 or rt == -1 or lt == rt else k - 2 | |
| else: | |
| eq, neq = (1 if lt == -1 else 0, 1) | |
| for _ in range(sz - 1): | |
| eq, neq = (neq * (k - 1) % MOD, (neq * (k - 2) + eq) % MOD) | |
| cur = neq * (k - 1) - eq if rt == -1 else neq * (k - 1) if lt == rt else neq * (k - 2) + eq | |
| ans = ans * cur % MOD | |
| print(ans) | |
| ``` | |
| ### 187 | |
| ```python | |
| import sys | |
| def pro(): | |
| return sys.stdin.readline().strip() | |
| def rop(): | |
| return map(int, pro().split()) | |
| a, s = rop() | |
| q = [0] * a | |
| w = [0] * s | |
| for i in range(a): | |
| n, p = rop() | |
| q[i] = (p, n - 1) | |
| w[n - 1] += 1 | |
| q.sort() | |
| t = 1e+100 | |
| for k in range(1, a + 1): | |
| p = w[:] | |
| r = [True] * a | |
| m = 0 | |
| for i in range(a): | |
| po, rty = q[i - 1] | |
| if rty != 0 and p[rty] >= k: | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| for i in range(a): | |
| po, rty = q[i] | |
| if rty != 0 and r[i] and (p[0] < k): | |
| p[0] += 1 | |
| p[rty] -= 1 | |
| m += po | |
| r[i] = False | |
| t = min(t, m) | |
| print(t) | |
| ``` | |
| ### 188 | |
| ```python | |
| n, m = list(map(int, input().split())) | |
| a = list(map(int, input().split())) | |
| gar = [] | |
| shadow = [] | |
| gar.append(a[0]) | |
| light = False | |
| a.append(m) | |
| for i in range(1, n + 1): | |
| dl = a[i] - a[i - 1] | |
| if light: | |
| gar.append(dl) | |
| light = False | |
| else: | |
| shadow.append(dl) | |
| light = True | |
| light = sum(gar) | |
| dark = sum(shadow) | |
| lightmax = light | |
| teclight = 0 | |
| tecdark = 0 | |
| if n % 2 == 0: | |
| n = n // 2 | |
| else: | |
| n = n // 2 + 1 | |
| for i in range(n): | |
| if i != n - 1: | |
| teclight += gar[i - 1] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| elif len(shadow) == n: | |
| teclight += gar[i] | |
| light -= gar[i] | |
| if gar[i] > 1: | |
| newlight = teclight - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| tecdark += shadow[i] | |
| dark -= shadow[i] | |
| if shadow[i] > 1: | |
| newlight = teclight + shadow[i] - 1 + dark | |
| lightmax = max(lightmax, newlight) | |
| print(lightmax) | |
| ``` | |
| ### 189 | |
| ```python | |
| from collections import defaultdict | |
| import bisect | |
| n = int(input()) | |
| x0, y0 = list(map(int, input().split(' '))) | |
| verticale = [] | |
| horizontale = [] | |
| diagonale1 = [] | |
| diagonale2 = [] | |
| for _ in range(n): | |
| t, x, y = input().split(' ') | |
| x, y = (int(x), int(y)) | |
| if x == x0: | |
| verticale.append((y, t)) | |
| if y == y0: | |
| horizontale.append((x, t)) | |
| if x + y == x0 + y0: | |
| diagonale1.append((x, t)) | |
| if x - y == x0 - y0: | |
| diagonale2.append((x, t)) | |
| dead = False | |
| v = sorted(verticale) | |
| if v: | |
| l = bisect.bisect(v, (y0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(horizontale) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'R'} or v[l - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'R'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'R'}: | |
| dead = True | |
| v = sorted(diagonale1) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| v = sorted(diagonale2) | |
| if v: | |
| l = bisect.bisect(v, (x0, 'K')) | |
| if 0 < l < len(v): | |
| if v[l][1] in {'Q', 'B'} or v[l - 1][1] in {'Q', 'B'}: | |
| dead = True | |
| elif l == 0: | |
| if v[0][1] in {'Q', 'B'}: | |
| dead = True | |
| elif v[len(v) - 1][1] not in {'Q', 'B'}: | |
| dead = True | |
| if dead: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 190 | |
| ```python | |
| n = int(input()) | |
| temp = input().split() | |
| x0 = int(temp[0]) | |
| y0 = int(temp[0]) | |
| N = ['T', 10 ** 20] | |
| NE = ['T', 10 ** 20] | |
| E = ['T', 10 ** 20] | |
| SE = ['T', 10 ** 20] | |
| S = ['T', 10 ** 20] | |
| SW = ['T', 10 ** 20] | |
| W = ['T', 10 ** 20] | |
| NW = ['T', 10 ** 20] | |
| for i in range(n): | |
| temp = input().split() | |
| x = int(temp[1]) | |
| y = int(temp[2]) | |
| if x - x0 == 0 and y - y0 > 0 and (y - y0 < N[1]): | |
| N = [temp[0], y - y0] | |
| elif x - x0 == y - y0 and x - x0 > 0 and (x - x0 < NE[1]): | |
| NE = [temp[0], x - x0] | |
| elif y - y0 == 0 and x - x0 > 0 and (x - x0 < E[1]): | |
| E = [temp[0], x - x0] | |
| elif x - x0 == -(y - y0) and x - x0 > 0 and (x - x0 < SE[1]): | |
| SE = [temp[0], x - x0] | |
| elif x - x0 == 0 and y - y0 < 0 and (y0 - y < S[1]): | |
| S = [temp[0], y0 - y] | |
| elif x - x0 == y - y0 and x - x0 < 0 and (x0 - x < SW[1]): | |
| SW = [temp[0], x0 - x] | |
| elif y - y0 == 0 and x - x0 < 0 and (x0 - x < W[1]): | |
| W = [temp[0], x0 - x] | |
| elif x - x0 == -(y - y0) and x - x0 < 0 and (x0 - x < NW[1]): | |
| NW = [temp[0], x0 - x] | |
| if 'R' in [N[0], E[0], S[0], W[0]] or 'B' in [NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| elif 'Q' in [N[0], E[0], S[0], W[0], NE[0], SE[0], SW[0], NW[0]]: | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 191 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(10 ** 7) | |
| N, K = map(int, input().split()) | |
| A = list(map(int, input().split())) | |
| mod = 998244353 | |
| dp1 = [1, 0] | |
| dp2 = [1, K - 1] | |
| for i in range(1 + N // 2): | |
| d1, d2 = (dp1[-1], dp2[-1]) | |
| dp1.append(d2) | |
| dp2.append(((K - 1) * d1 + (K - 2) * d2) % mod) | |
| ans = 1 | |
| dif = (K - 2) * pow(K - 1, mod - 2, mod) % mod | |
| AO = A[::-2] | |
| AE = A[1::2] | |
| if all((a == -1 for a in AO)): | |
| ans = K * pow(K - 1, len(AO) - 1, mod) % mod | |
| else: | |
| AO = [0] + AO + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AO[1:], 1): | |
| if a == -1: | |
| if AO[i - 1] != -1: | |
| l = AO[i - 1] | |
| mc += 1 | |
| else: | |
| r = a | |
| if AO[i - 1] == a: | |
| ans = 0 | |
| break | |
| elif mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| if all((a == -1 for a in AE)): | |
| ans = ans * K * pow(K - 1, len(AE) - 1, mod) % mod | |
| else: | |
| AE = [0] + AE + [0] | |
| mc = 0 | |
| l = -2 | |
| r = -2 | |
| for i, a in enumerate(AE[1:], 1): | |
| if a == -1: | |
| if AE[i - 1] != -1: | |
| l = AE[i - 1] | |
| mc += 1 | |
| else: | |
| r = AE[i] | |
| if AE[i - 1] == a: | |
| ans = 0 | |
| break | |
| if mc: | |
| if not l or not r: | |
| ans = ans * pow(K - 1, mc, mod) % mod | |
| elif l == r: | |
| ans = ans * dp2[mc] % mod | |
| else: | |
| ans = ans * (dp2[mc] * dif + dp1[mc]) % mod | |
| mc = 0 | |
| print(ans) | |
| ``` | |
| ### 192 | |
| ```python | |
| n, m = map(int, input().split(' ')) | |
| numbers = list(map(int, input().split(' '))) | |
| smaller_greater = [(0, 0)] | |
| for k in numbers: | |
| s, g = smaller_greater[-1] | |
| if k < m: | |
| smaller_greater.append((s + 1, g)) | |
| elif k > m: | |
| smaller_greater.append((s, g + 1)) | |
| else: | |
| smaller_greater.append((s, g)) | |
| i = numbers.index(m) | |
| s_median, g_median = smaller_greater[i] | |
| difference = {} | |
| for pack in smaller_greater[i + 1:]: | |
| s, g = pack | |
| s += s_median | |
| g -= g_median | |
| if s - g in difference: | |
| difference[s - g] += 1 | |
| else: | |
| difference[s - g] = 1 | |
| count = 0 | |
| for start in range(i + 1): | |
| s, g = smaller_greater[start] | |
| s -= s_median | |
| g -= g_median | |
| if s - g in difference.keys(): | |
| count += difference[s - g] | |
| if s - g - 1 in difference.keys(): | |
| count += difference[s - g - 1] | |
| print(count) | |
| ``` | |
| ### 193 | |
| ```python | |
| from copy import deepcopy | |
| import itertools | |
| from bisect import bisect_left | |
| from bisect import bisect_right | |
| import math | |
| from collections import deque | |
| def read(): | |
| return int(input()) | |
| def readmap(): | |
| return map(int, input().split()) | |
| def readlist(): | |
| return list(map(int, input().split())) | |
| N, M = readmap() | |
| A = [0] + readlist() + [M] | |
| on_time = [] | |
| off_time = [] | |
| for i in range(1, N + 2): | |
| if i % 2 == 1: | |
| on_time.append(A[i] - A[i - 1]) | |
| else: | |
| off_time.append(A[i] - A[i - 1]) | |
| sum_on_time = sum(on_time) | |
| max_on_time = sum_on_time | |
| if N % 4 == 1: | |
| i = N + 1 | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| else: | |
| i = N | |
| sum_on_time -= A[N + 1] - A[N] | |
| while i > 0: | |
| if A[i] - A[i - 1] > 1: | |
| x = A[i - 1] + 1 | |
| max_on_time = max(max_on_time, sum_on_time + A[i] - x) | |
| sum_on_time = sum_on_time + (A[i] - A[i - 1]) - (A[i - 1] - A[i - 2]) | |
| if A[i - 1] - A[i - 2] > 1: | |
| x = A[i - 1] - 1 | |
| max_on_time = max(max_on_time, sum_on_time + x - A[i - 2]) | |
| i -= 2 | |
| print(max_on_time) | |
| ``` | |
| ### 194 | |
| ```python | |
| import sys | |
| n, m = list(map(int, sys.stdin.readline().split())) | |
| M = [m] | |
| A = list(map(int, sys.stdin.readline().split())) | |
| L = [0] * n | |
| inc = False | |
| dec = False | |
| def ALLYes(): | |
| Ans = '' | |
| for i in range(M[0]): | |
| Ans += 'Yes\n' | |
| sys.stdout.write(Ans) | |
| return | |
| for i in range(1, n): | |
| if A[i] > A[i - 1]: | |
| L[i] = 1 | |
| inc = True | |
| elif A[i] == A[i - 1]: | |
| L[i] = 0 | |
| else: | |
| L[i] = -1 | |
| dec = True | |
| if inc == False or dec == False: | |
| ALLYes() | |
| else: | |
| neg = L.index(-1) | |
| pos = L.index(1) | |
| First = [-1] * n | |
| for i in range(2, n): | |
| if L[i] == 1: | |
| x = max(neg, pos) | |
| if x <= i: | |
| First[i] = x | |
| elif min(neg, pos) <= i: | |
| First[i] = min(neg, pos) | |
| else: | |
| First[i] = -1 | |
| if L[i] == 1: | |
| if neg > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = neg | |
| pos = i | |
| if L[i] == -1: | |
| if pos > i: | |
| First[i] = -1 | |
| else: | |
| First[i] = pos | |
| neg = i | |
| Ans = '' | |
| for i in range(m): | |
| l, r = list(map(int, sys.stdin.readline().split())) | |
| r -= 1 | |
| if r - l < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 0: | |
| r = First[r] | |
| if r < 1: | |
| Ans += 'Yes\n' | |
| continue | |
| if L[r] == 1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| elif L[r] == -1: | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| r = First[r] | |
| if r < l: | |
| Ans += 'Yes\n' | |
| continue | |
| else: | |
| Ans += 'No\n' | |
| continue | |
| sys.stdout.write(Ans) | |
| ``` | |
| ### 195 | |
| ```python | |
| import sys | |
| import math | |
| n, v = [int(x) for x in sys.stdin.readline().split()] | |
| k = [0] * 3002 | |
| for i in range(n): | |
| a, b = [int(x) for x in sys.stdin.readline().split()] | |
| k[a] += b | |
| i = 1 | |
| res = 0 | |
| while i <= 3001: | |
| val = 0 | |
| if k[i - 1] < v: | |
| val = k[i - 1] | |
| res += val | |
| d = v - val | |
| if k[i] < d: | |
| res += k[i] | |
| k[i] = 0 | |
| else: | |
| res += d | |
| k[i] = k[i] * d | |
| else: | |
| res += v | |
| i += 1 | |
| print(res) | |
| ``` | |
| ### 196 | |
| ```python | |
| from math import ceil, log | |
| t = 1 | |
| for test in range(t): | |
| n, m = list(map(int, input().split())) | |
| arr = list(map(int, input().split())) | |
| arr.append(m) | |
| prev = 0 | |
| on = 0 | |
| counter = 0 | |
| for i in arr: | |
| if counter % 2 == 0: | |
| on += i - prev | |
| prev = i | |
| counter += 1 | |
| off = m - on | |
| counter = 0 | |
| ans = on | |
| prev = 0 | |
| prevOn = 0 | |
| for i in arr: | |
| if counter % 6 == 0: | |
| if i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn - (i - prev)) | |
| if tmp > ans: | |
| ans = tmp | |
| prevOn += i - prev | |
| elif i - prev != 1: | |
| tmp = prevOn + i - prev - 1 + m - i - (on - prevOn) | |
| if tmp > ans: | |
| ans = tmp | |
| prev = i | |
| counter += 1 | |
| print(ans) | |
| ``` | |
| ### 197 | |
| ```python | |
| n, v = map(int, input().split()) | |
| T = 3010 | |
| a = [[] for i in range(T)] | |
| for i in range(n): | |
| x, y = map(int, input().split()) | |
| a[x].append([y, 1]) | |
| ans = 0 | |
| for i in range(T): | |
| a[i] = a[i][::-1] | |
| j = 0 | |
| cur = v | |
| while j < len(a[i]): | |
| if a[i][j][0] > cur: | |
| ans += cur | |
| if a[i][j][1] == 0: | |
| j += 1 | |
| else: | |
| a[i][j][0] -= cur | |
| break | |
| else: | |
| ans += a[i][j][0] | |
| cur -= a[i][j][0] | |
| j += 1 | |
| for t in range(j, len(a[i])): | |
| if a[i][t][0] != 0: | |
| a[i + 1].append([a[i][t][0], 0]) | |
| print(ans) | |
| ``` | |
| ### 198 | |
| ```python | |
| from sys import stdin | |
| n = int(stdin.readline()) | |
| x, y = map(int, stdin.readline().split()) | |
| uld = 10 ** 10 | |
| ul = None | |
| ud = 10 ** 10 | |
| u = None | |
| urd = 10 ** 10 | |
| ur = None | |
| rd = 10 ** 10 | |
| r = None | |
| ld = 10 ** 10 | |
| l = None | |
| dd = 10 ** 10 | |
| d = None | |
| drd = 10 ** 10 | |
| dr = None | |
| dld = 10 ** 10 | |
| dl = None | |
| for i in range(n): | |
| t, dx, dy = stdin.readline().split() | |
| dx = int(dx) | |
| dy = int(dy) | |
| if dx == x: | |
| if dy > y: | |
| if ud > dy - y: | |
| ud = dy - y | |
| u = t | |
| elif dd > y - dy: | |
| dd = y - dy | |
| d = t | |
| if dy == y: | |
| if dx > x: | |
| if rd > dx - x: | |
| rd = dx - x | |
| r = t | |
| elif ld > x - dx: | |
| ld = x - dx | |
| l = t | |
| if dx - x == dy - y: | |
| if not dy > y: | |
| if urd > dy - y: | |
| urd = dy - y | |
| ur = t | |
| elif dld > y - dy: | |
| dld = y - dy | |
| dl = t | |
| if -(dx - x) == dy - y: | |
| if dy > y: | |
| if uld > dy - y: | |
| uld = dy - y | |
| ul = t | |
| elif drd > y - dy: | |
| drd = y - dy | |
| dr = t | |
| if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): | |
| print('YES') | |
| else: | |
| print('NO') | |
| ``` | |
| ### 199 | |
| ```python | |
| def bins(sortedlist, x): | |
| n = len(sortedlist) | |
| start = 0 | |
| end = n - 1 | |
| while start <= end: | |
| mid = int((start + end) / 2) | |
| if x == sortedlist[mid][0]: | |
| return mid | |
| elif x < sortedlist[mid][0]: | |
| end = mid - 1 | |
| else: | |
| start = mid + 1 | |
| if sortedlist[mid][0] <= x: | |
| return mid | |
| else: | |
| return mid - 1 | |
| n, s = list(map(int, input().split())) | |
| hap = [] | |
| for i in range(n): | |
| hap.append(list(map(int, input().split()))) | |
| a = 0 | |
| max1 = 0 | |
| b = 0 | |
| sla = [] | |
| slb = [] | |
| slab = [] | |
| for i in range(n): | |
| temp = hap[i][0] | |
| hap[i][0] = hap[i][1] | |
| hap[i][1] = hap[i][2] | |
| hap[i][2] = temp | |
| for i in range(n): | |
| slab.append([hap[i][0] - hap[i][1], hap[i][2]]) | |
| happi = 0 | |
| for i in range(n): | |
| if hap[i][0] > hap[i][1]: | |
| a += hap[i][2] | |
| happi += hap[i][2] * hap[i][0] | |
| else: | |
| b += hap[i][2] | |
| happi += hap[i][2] * hap[i][1] | |
| sla.sort() | |
| slb.sort() | |
| slab.sort() | |
| if a % s + b % s >= s: | |
| print(happi) | |
| else: | |
| loc = bins(slab, 0) | |
| happia = happi | |
| count = 0 | |
| b = b % s | |
| a = a % s | |
| left = b % s | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happia += slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happia += slab[loc + count][0] * left | |
| break | |
| count -= 1 | |
| left = a % s | |
| count = 0 | |
| happib = happi | |
| while loc < n and slab[loc][0] <= 0: | |
| loc += 1 | |
| while left > 0: | |
| if slab[loc + count][1] < left: | |
| happib -= slab[loc + count][0] * slab[loc + count][1] | |
| left -= slab[loc + count][1] | |
| else: | |
| happib -= slab[loc + count][0] * left | |
| break | |
| count += 1 | |
| print(max(happia, happib)) | |
| ``` | |
| ### 200 | |
| ```python | |
| def main(): | |
| w, h, n = list(map(int, input().split())) | |
| res, vrt, hor = ([], [], []) | |
| vh = (vrt, hor) | |
| for i in range(n): | |
| s = input() | |
| x = int(s[2:]) | |
| flag = s[0] == 'V' | |
| vh[flag].append(i) | |
| res.append([x, flag]) | |
| dim = [] | |
| for tmp, m in zip(vh, (h, w)): | |
| tmp.sort(key=lambda e: res[e][0]) | |
| u = [None, [0]] | |
| dim.append(u) | |
| j = z = 0 | |
| for i in tmp: | |
| x = res[i][0] | |
| if not z < x - j: | |
| z = x - j | |
| j = x | |
| v = [u, res[i]] | |
| u.append(v) | |
| u = v | |
| res[i].append(u) | |
| v = [u, [m], None] | |
| u.append(v) | |
| dim.append(v) | |
| if z < m - j: | |
| z = m - j | |
| dim.append(z) | |
| l, r, wmax, u, d, hmax = dim | |
| whmax = [wmax, hmax] | |
| for i in range(n - 1, -1, -1): | |
| x, flag, link = res[i] | |
| u = whmax[flag] | |
| res[i] = u * whmax[not flag] | |
| link[0][2] = link[2] | |
| link[2][0] = link[0] | |
| v = link[2][1][0] - link[0][1][0] | |
| if u < v: | |
| whmax[flag] = v | |
| print('\n'.join(map(str, res))) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### Final Query | |
| Identify every unordered pair of earlier programs that implements the same input-output specification, including its boundary and edge-case behavior. Each cell may appear in at most one returned pair. Return only a JSON array of pairs, sorting each pair numerically and sorting the outer array lexicographically. Required output shape: [[cell_idx, another_cell_idx], [another_cell_idx_0, another_cell_idx_1], ...]. Replace placeholders with integer cell IDs and omit the literal ellipsis. | |