|
Download tasks/equivalent-program-pair-search/contexts/sample-00004.md from StringNLP/longharness: direct link, hf CLI and curl.
- Browser
- Download file 258 kB
-
https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00004.md
- Command line
-
hf download hf://datasets/StringNLP/longharness/tasks/equivalent-program-pair-search/contexts/sample-00004.md
-
curl -L -o sample-00004.md https://huggingface.co/datasets/StringNLP/longharness/resolve/main/tasks/equivalent-program-pair-search/contexts/sample-00004.md
258 kB
| ### 001 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last >= first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 0 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = 1 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 1 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 002 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & -1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 003 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 004 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[0]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 005 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[-1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 006 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N - 1): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 007 | |
| ```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') | |
| ``` | |
| ### 008 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 1): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 009 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1 - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 010 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 011 | |
| ```python | |
| import math | |
| n, m, k, q = list(map(int, input().split())) | |
| right = [-1] * n | |
| left = [-1] * n | |
| for i in range(k): | |
| row, col = list(map(int, input().split())) | |
| row -= 1 | |
| col -= 1 | |
| if right[row] == -1: | |
| right[row] = left[row] = col | |
| else: | |
| right[row] = max(right[row], col) | |
| left[row] = min(left[row], col) | |
| safe = sorted(list([int(qi) - 1 for qi in input().split()])) | |
| lup = [0] * m | |
| rup = [0] * m | |
| j = 0 | |
| while j < safe[0]: | |
| lup[j] = -1 | |
| rup[j] = safe[0] | |
| j += 1 | |
| lup[j] = rup[j] = safe[0] | |
| for i in range(1, q): | |
| j = safe[i - 1] | |
| while j < safe[i]: | |
| lup[j] = safe[i - 1] | |
| rup[j] = safe[i] | |
| j += 1 | |
| j = safe[-1] | |
| while j < m: | |
| lup[j] = safe[-1] | |
| rup[j] = -1 | |
| j += 1 | |
| if left[0] == -1: | |
| left[0] = right[0] = 0 | |
| dleft = right[0] + (right[0] - left[0]) | |
| dright = right[0] | |
| lastn = 0 | |
| for i in range(1, n): | |
| if left[i] == -1: | |
| continue | |
| left_1b = lup[left[lastn]] | |
| left_2b = rup[left[lastn]] | |
| right_1b = lup[right[lastn]] | |
| right_2b = rup[right[lastn]] | |
| ilen = right[i] - left[i] | |
| ll1: int | |
| ll2: int | |
| rl1: int | |
| rl2: int | |
| if left_1b != -1: | |
| ll1 = left[lastn] - left_1b + abs(right[i] - left_1b) | |
| else: | |
| ll1 = math.inf | |
| if left_2b != -1: | |
| ll2 = left_2b - left[lastn] + abs(right[i] - left_2b) | |
| else: | |
| ll2 = math.inf | |
| if right_1b != -1: | |
| rl1 = right[lastn] - right_1b + abs(right[i] - right_1b) | |
| else: | |
| rl1 = math.inf | |
| if right_2b != -1: | |
| rl2 = right_2b - right[lastn] + abs(right[i] - right_2b) | |
| else: | |
| rl2 = math.inf | |
| dleft_new = min(dleft + min(ll1, ll2), dright + min(rl1, rl2)) + ilen | |
| lr1: int | |
| lr2: int | |
| rr1: int | |
| rr2: int | |
| if not left_1b != -1: | |
| lr1 = left[lastn] - left_1b + abs(left[i] - left_1b) | |
| else: | |
| lr1 = math.inf | |
| if left_2b != -1: | |
| lr2 = left_2b - left[lastn] + abs(left[i] - left_2b) | |
| else: | |
| lr2 = math.inf | |
| if right_1b != -1: | |
| rr1 = right[lastn] - right_1b + abs(left[i] - right_1b) | |
| else: | |
| rr1 = math.inf | |
| if right_2b != -1: | |
| rr2 = right_2b - right[lastn] + abs(left[i] - right_2b) | |
| else: | |
| rr2 = math.inf | |
| dright_new = min(dleft + min(lr1, lr2), dright + min(rr1, rr2)) + ilen | |
| dleft, dright = (dleft_new, dright_new) | |
| lastn = i | |
| print(min(dleft, dright) + lastn) | |
| ``` | |
| ### 012 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(0): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 013 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 014 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 1: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 015 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (-1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 016 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last >= first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 1 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = 1 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 2 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 017 | |
| ```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') | |
| ``` | |
| ### 018 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 5 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 019 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = -1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 020 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 021 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i - 1][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 022 | |
| ```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') | |
| ``` | |
| ### 023 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == -2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 024 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [2, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 025 | |
| ```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() | |
| ``` | |
| ### 026 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i - 1][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 027 | |
| ```python | |
| n, m, k, q = map(int, input().split()) | |
| tr_min = [None for _ in range(n)] | |
| tr_max = [None for _ in range(n)] | |
| for _ in range(k): | |
| row, col = map(int, input().split()) | |
| row = n - row | |
| col -= 1 | |
| if tr_min[row] == None or col < tr_min[row]: | |
| tr_min[row] = col | |
| if tr_max[row] == None or col > tr_max[row]: | |
| tr_max[row] = col | |
| tr_min[-1] = 0 | |
| tr_max[-1] = tr_max[-1 - 1] or 0 | |
| savecols = sorted(map(lambda t: int(t) - 1, input().split())) | |
| def binsearch(arr, val): | |
| l, r = (0, len(arr) - 1) | |
| while l <= r: | |
| mid = l + (r - l) // 2 | |
| if arr[mid] < val: | |
| l = mid + 1 | |
| elif arr[mid] > val: | |
| r = mid - 1 | |
| else: | |
| return mid | |
| return r | |
| def find_short_descent(A, B): | |
| if A > B: | |
| return find_short_descent(B, A) | |
| idx1 = binsearch(savecols, A) | |
| idx2 = idx1 + 1 | |
| minval = m * m | |
| if idx2 < len(savecols): | |
| if savecols[idx2] < B: | |
| return B - A | |
| else: | |
| minval = min(minval, (savecols[idx2] << 1) - A - B) | |
| if idx1 >= 0: | |
| minval = min(minval, A + B - (savecols[idx1] << 1)) | |
| return minval | |
| l, r = (0, 0) | |
| found_valid = False | |
| last_valid = None | |
| for row in range(0, n): | |
| if found_valid == False: | |
| if tr_min[row] != None: | |
| found_valid = True | |
| last_valid = row | |
| l = tr_max[row] - tr_min[row] | |
| r = tr_max[row] - tr_min[row] | |
| continue | |
| continue | |
| if tr_min[row] == None: | |
| l += 1 | |
| r += 1 | |
| continue | |
| ll = find_short_descent(tr_min[last_valid], tr_min[row]) | |
| lr = find_short_descent(tr_min[last_valid], tr_max[row]) | |
| rl = find_short_descent(tr_max[last_valid], tr_min[row]) | |
| rr = find_short_descent(tr_max[last_valid], tr_max[row]) | |
| new_l = min(l + lr, r + rr) + 1 + (tr_max[row] - tr_min[row]) | |
| new_r = min(l + ll, r + rl) + 1 + (tr_max[row] - tr_min[row]) | |
| l, r = (new_l, new_r) | |
| last_valid = row | |
| answ = l + tr_min[last_valid] | |
| print(answ) | |
| ``` | |
| ### 028 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if y - 1 & d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 029 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and 3 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 030 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i - 1]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 031 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in reversed(range(sum, down - 1, -1)): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 032 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == -1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 033 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(3): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1 - 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 034 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] -= y | |
| ``` | |
| ### 035 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 >= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 036 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1 + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 037 | |
| ```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') | |
| ``` | |
| ### 038 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 039 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = -3 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 040 | |
| ```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') | |
| ``` | |
| ### 041 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 4 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 042 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 1: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 043 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + -1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 044 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 0: | |
| tt.append(rr(c0, c1, y[t - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-1][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 2) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 > r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 045 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-1) | |
| ``` | |
| ### 046 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 0: | |
| tt.append(rr(c0, c1, y[t - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-1][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 1) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 >= r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 047 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] - x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 048 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i - 1][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 049 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i -= 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 050 | |
| ```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') | |
| ``` | |
| ### 051 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-2) | |
| ``` | |
| ### 052 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y < r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 053 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 3 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 054 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i - 1] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 055 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) and y > max(ya, yb) and (y < min(yc, yd)) and (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 056 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum <= 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 057 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 5 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 058 | |
| ```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') | |
| ``` | |
| ### 059 | |
| ```python | |
| n, m, k, q = map(int, input().split()) | |
| tr_min = [None for _ in range(n)] | |
| tr_max = [None for _ in range(n)] | |
| for _ in range(k): | |
| row, col = map(int, input().split()) | |
| row = n - row | |
| col -= 1 | |
| if tr_min[row] == None or col < tr_min[row]: | |
| tr_min[row] = col | |
| if tr_max[row] == None or col > tr_max[row]: | |
| tr_max[row] = col | |
| tr_min[-1] = 0 | |
| tr_max[-1] = tr_max[-1] or 0 | |
| savecols = sorted(map(lambda t: int(t) - 1, input().split())) | |
| def binsearch(arr, val): | |
| l, r = (0, len(arr) - 1) | |
| while l <= r: | |
| mid = l + (r - l) // 2 | |
| if arr[mid] > val: | |
| l = mid + 1 | |
| elif arr[mid] > val: | |
| r = mid - 1 | |
| else: | |
| return mid | |
| return r | |
| def find_short_descent(A, B): | |
| if A > B: | |
| return find_short_descent(B, A) | |
| idx1 = binsearch(savecols, A) | |
| idx2 = idx1 + 1 | |
| minval = m * m | |
| if idx2 < len(savecols): | |
| if savecols[idx2] < B: | |
| return B - A | |
| else: | |
| minval = min(minval, (savecols[idx2] << 1) - A - B) | |
| if idx1 >= 0: | |
| minval = min(minval, A + B - (savecols[idx1] << 1)) | |
| return minval | |
| l, r = (0, 0) | |
| found_valid = False | |
| last_valid = None | |
| for row in range(0, n): | |
| if found_valid == False: | |
| if tr_min[row] != None: | |
| found_valid = True | |
| last_valid = row | |
| l = tr_max[row] - tr_min[row] | |
| r = tr_max[row] - tr_min[row] | |
| continue | |
| continue | |
| if tr_min[row] == None: | |
| l += 1 | |
| r += 1 | |
| continue | |
| ll = find_short_descent(tr_min[last_valid], tr_min[row]) | |
| lr = find_short_descent(tr_min[last_valid], tr_max[row]) | |
| rl = find_short_descent(tr_max[last_valid], tr_min[row]) | |
| rr = find_short_descent(tr_max[last_valid], tr_max[row]) | |
| new_l = min(l + lr, r + rr) + 1 + (tr_max[row] - tr_min[row]) | |
| new_r = min(l + ll, r + rl) + 1 + (tr_max[row] - tr_min[row]) | |
| l, r = (new_l, new_r) | |
| last_valid = row | |
| answ = l + tr_min[last_valid] | |
| print(answ) | |
| ``` | |
| ### 060 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 1: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 061 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 062 | |
| ```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') | |
| ``` | |
| ### 063 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +-1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 064 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == -2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 065 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m <= w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 066 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] != l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 067 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = +1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 068 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 3]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 069 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= -1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 070 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == -1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 071 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 072 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 3 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 073 | |
| ```python | |
| n, m, k, q = map(int, input().split()) | |
| tr_min = [None for _ in range(n)] | |
| tr_max = [None for _ in range(n)] | |
| for _ in range(k): | |
| row, col = map(int, input().split()) | |
| row = n - row | |
| col -= 1 | |
| if tr_min[row] == None or col < tr_min[row]: | |
| tr_min[row] = col | |
| if tr_max[row] == None or col > tr_max[row]: | |
| tr_max[row] = col | |
| tr_min[-1] = 0 | |
| tr_max[-1] = tr_max[-1] or 0 | |
| savecols = sorted(map(lambda t: int(t) - 1, input().split())) | |
| def binsearch(arr, val): | |
| l, r = (0, len(arr) - 1) | |
| while l <= r: | |
| mid = l + (r - l) // 2 | |
| if arr[mid] < val: | |
| l = mid + 1 | |
| elif arr[mid] > val: | |
| r = mid - 1 | |
| else: | |
| return mid | |
| return r | |
| def find_short_descent(A, B): | |
| if A > B: | |
| return find_short_descent(B, A) | |
| idx1 = binsearch(savecols, A) | |
| idx2 = idx1 + 0 | |
| minval = m * m | |
| if idx2 < len(savecols): | |
| if savecols[idx2] < B: | |
| return B - A | |
| else: | |
| minval = min(minval, (savecols[idx2] << 1) - A - B) | |
| if idx1 >= 0: | |
| minval = min(minval, A + B - (savecols[idx1] << 1)) | |
| return minval | |
| l, r = (0, 0) | |
| found_valid = False | |
| last_valid = None | |
| for row in range(0, n): | |
| if found_valid == False: | |
| if tr_min[row] != None: | |
| found_valid = True | |
| last_valid = row | |
| l = tr_max[row] - tr_min[row] | |
| r = tr_max[row] - tr_min[row] | |
| continue | |
| continue | |
| if tr_min[row] == None: | |
| l += 1 | |
| r += 1 | |
| continue | |
| ll = find_short_descent(tr_min[last_valid], tr_min[row]) | |
| lr = find_short_descent(tr_min[last_valid], tr_max[row]) | |
| rl = find_short_descent(tr_max[last_valid], tr_min[row]) | |
| rr = find_short_descent(tr_max[last_valid], tr_max[row]) | |
| new_l = min(l + lr, r + rr) + 1 + (tr_max[row] - tr_min[row]) | |
| new_r = min(l + ll, r + rl) + 1 + (tr_max[row] - tr_min[row]) | |
| l, r = (new_l, new_r) | |
| last_valid = row | |
| answ = l + tr_min[last_valid] | |
| print(answ) | |
| ``` | |
| ### 074 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 075 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 0] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 1 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 076 | |
| ```python | |
| import sys | |
| from itertools import accumulate | |
| def solve(): | |
| n, m = map(int, input().split()) | |
| w = [[] for i in range(3)] | |
| for i in range(n): | |
| wi, ci = map(int, sys.stdin.readline().split()) | |
| wi -= 1 | |
| w[wi].append(ci) | |
| for i in range(2): | |
| w[i].sort(reverse=True) | |
| dp = [0] * (m + 1) | |
| used = [[0] * 2 for i in range(m + 1)] | |
| s0 = len(w[0]) | |
| s1 = len(w[1]) | |
| if s0 > 0: | |
| dp[1] = w[0][0] | |
| used[1] = [1, 0] | |
| for i in range(2, m + 1): | |
| if used[i - 1][0] < s0: | |
| dp[i] = dp[i - 1] + w[0][used[i - 1][0]] | |
| used[i] = used[i - 1][:] | |
| used[i][0] += 1 | |
| else: | |
| dp[i] = dp[i - 1] | |
| used[i] = used[i - 1][:] | |
| if used[i - 2][1] < s1 and dp[i] < dp[i - 2] + w[1][used[i - 2][1]]: | |
| dp[i] = dp[i - 2] + w[1][used[i - 2][1]] | |
| used[i] = used[i - 2][:] | |
| used[i][1] += 1 | |
| pf = [0] + list(accumulate(w[2])) | |
| ans = max((pf[k] + dp[m - 3 * k] for k in range(min(len(pf), m // 3 + 1)))) | |
| print(ans) | |
| def __starting_point(): | |
| solve() | |
| __starting_point() | |
| ``` | |
| ### 077 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1 + 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 078 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 2 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 079 | |
| ```python | |
| import math | |
| n, m, k, q = list(map(int, input().split())) | |
| right = [-1] * n | |
| left = [-1] * n | |
| for i in range(k): | |
| row, col = list(map(int, input().split())) | |
| row -= 1 | |
| col -= 1 | |
| if right[row] == -1: | |
| right[row] = left[row] = col | |
| else: | |
| right[row] = max(right[row], col) | |
| left[row] = min(left[row], col) | |
| safe = sorted(list([int(qi) - 1 for qi in input().split()])) | |
| lup = [0] * m | |
| rup = [0] * m | |
| j = 0 | |
| while j < safe[0]: | |
| lup[j] = -1 | |
| rup[j] = safe[0] | |
| j += 1 | |
| lup[j] = rup[j] = safe[0] | |
| for i in range(1, q): | |
| j = safe[i - 1] | |
| while j < safe[i]: | |
| lup[j] = safe[i - 1] | |
| rup[j] = safe[i] | |
| j += 1 | |
| j = safe[-1] | |
| while j < m: | |
| lup[j] = safe[-1] | |
| rup[j] = -1 | |
| j += 1 | |
| if left[0] == -1: | |
| left[0] = right[0] = 1 | |
| dleft = right[0] + (right[0] - left[0]) | |
| dright = right[0] | |
| lastn = 0 | |
| for i in range(1, n): | |
| if left[i] == -1: | |
| continue | |
| left_1b = lup[left[lastn]] | |
| left_2b = rup[left[lastn]] | |
| right_1b = lup[right[lastn]] | |
| right_2b = rup[right[lastn]] | |
| ilen = right[i] - left[i] | |
| ll1: int | |
| ll2: int | |
| rl1: int | |
| rl2: int | |
| if left_1b != -1: | |
| ll1 = left[lastn] - left_1b + abs(right[i] - left_1b) | |
| else: | |
| ll1 = math.inf | |
| if left_2b != -1: | |
| ll2 = left_2b - left[lastn] + abs(right[i] - left_2b) | |
| else: | |
| ll2 = math.inf | |
| if right_1b != -1: | |
| rl1 = right[lastn] - right_1b + abs(right[i] - right_1b) | |
| else: | |
| rl1 = math.inf | |
| if right_2b != -1: | |
| rl2 = right_2b - right[lastn] + abs(right[i] - right_2b) | |
| else: | |
| rl2 = math.inf | |
| dleft_new = min(dleft + min(ll1, ll2), dright + min(rl1, rl2)) + ilen | |
| lr1: int | |
| lr2: int | |
| rr1: int | |
| rr2: int | |
| if left_1b != -1: | |
| lr1 = left[lastn] - left_1b + abs(left[i] - left_1b) | |
| else: | |
| lr1 = math.inf | |
| if left_2b != -1: | |
| lr2 = left_2b - left[lastn] + abs(left[i] - left_2b) | |
| else: | |
| lr2 = math.inf | |
| if right_1b != -1: | |
| rr1 = right[lastn] - right_1b + abs(left[i] - right_1b) | |
| else: | |
| rr1 = math.inf | |
| if right_2b != -1: | |
| rr2 = right_2b - right[lastn] + abs(left[i] - right_2b) | |
| else: | |
| rr2 = math.inf | |
| dright_new = min(dleft + min(lr1, lr2), dright + min(rr1, rr2)) + ilen | |
| dleft, dright = (dleft_new, dright_new) | |
| lastn = i | |
| print(min(dleft, dright) + lastn) | |
| ``` | |
| ### 080 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += -1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 081 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last >= first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 0 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = -1 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 2 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 082 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1 - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 083 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-0) | |
| ``` | |
| ### 084 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][-1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 085 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += -1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 086 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = -4 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 087 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 1 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 088 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j - 1]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 089 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const - 1): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 090 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 4] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 091 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) + pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 092 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y // m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 093 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == -1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 094 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + (a2 + c1) | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 095 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res -= 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 096 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == -1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 1 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 097 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return -2 <= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 098 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 1]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] != l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 099 | |
| ```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() | |
| ``` | |
| ### 100 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 1) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 101 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a > l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res += 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 102 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 2 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 103 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif not ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 104 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last >= first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 0 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = 0 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 2 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 105 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 0: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v * d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 106 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 2) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 107 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 4 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 108 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) * pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) + pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 109 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil(n * y / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 110 | |
| ```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') | |
| ``` | |
| ### 111 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == -1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 112 | |
| ```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') | |
| ``` | |
| ### 113 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, -1, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 114 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n - 1): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 115 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 2 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 1 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 116 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --2)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 117 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-3) | |
| ``` | |
| ### 118 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 2 <= i < n and 0 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 119 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 1 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 120 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - -1 | |
| print(ans - sum) | |
| ``` | |
| ### 121 | |
| ```python | |
| N = int(input()) | |
| A = [] | |
| for _ in range(N): | |
| a = int(input()) | |
| b = [] | |
| for _ in range(a): | |
| b.append(list(map(int, input().split()))) | |
| A.append(b) | |
| def F(i): | |
| cnt = 0 | |
| B = [-1] * N | |
| r = 0 | |
| for j in range(N): | |
| if i >> j & 1: | |
| r += 1 | |
| if B[j] == 0: | |
| return 0 | |
| B[j] = 0 | |
| for p, q in A[j]: | |
| if q == 0: | |
| if B[p - 1] == 1: | |
| return 0 | |
| B[p - 1] = 0 | |
| else: | |
| if B[p - 1] == 0: | |
| return 0 | |
| B[p - 1] = 1 | |
| else: | |
| cnt += 1 | |
| else: | |
| if B[j] == 1: | |
| return 0 | |
| B[j] = 0 | |
| if cnt == r: | |
| return cnt | |
| ans = 0 | |
| for i in range(1 << N): | |
| ans = max(ans, F(i)) | |
| print(ans) | |
| ``` | |
| ### 122 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i - 1] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = ax * day - ay * dax - bx * day + by * dax | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 123 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if xa == xb and xc == xd or (ya == yb and yc == yd): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if not (y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd))): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 124 | |
| ```python | |
| df = int(input()) | |
| for _ in range(df): | |
| n, x, y, d = list(map(int, input().split())) | |
| d2 = abs(x - y) | |
| ans = 3000000000.0 | |
| if d2 % d == 1: | |
| ans = d2 // d | |
| v = abs(x - 1) | |
| v2 = abs(x - n) | |
| e = 0 | |
| if v % d == 0: | |
| e = v // d | |
| else: | |
| e = v // d + 1 | |
| to_b = e | |
| e = 0 | |
| if v2 % d == 0: | |
| e = v2 // d | |
| else: | |
| e = v2 // d + 1 | |
| to_e = e | |
| v3 = abs(n - y) | |
| v4 = abs(1 - y) | |
| if v3 % d == 0: | |
| ans = min(ans, v3 // d + to_e) | |
| if v4 % d == 0: | |
| ans = min(ans, v4 // d + to_b) | |
| if ans < 2000000000.0: | |
| print(ans) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 125 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -5): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 126 | |
| ```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') | |
| ``` | |
| ### 127 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += -1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 128 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 0: | |
| tt.append(rr(c0, c1, y[t - 1 - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-1][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 1) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 > r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 129 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[1]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = 1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 130 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 1: | |
| tt.append(rr(c0, c1, y[t - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-1][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 1) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 > r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 131 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 132 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last >= first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 0 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = 1 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 2 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 133 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] -= 1 | |
| arr[a] += 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 134 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 0: | |
| tt.append(rr(c0, c1, y[t - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-0][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 1) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 > r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 135 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l >= 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 136 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(-1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 137 | |
| ```python | |
| n, m, l = list(map(int, input().split())) | |
| l += 1 | |
| hair = [1] * (n + 1) | |
| other_end = [1] * (n + 1) | |
| i = 1 | |
| j = 1 | |
| big = 1 | |
| res = 1 | |
| for a in map(int, input().split()): | |
| hair[i] = a | |
| if a >= l: | |
| if big == 1: | |
| j = i | |
| big = 2 | |
| elif big == 2: | |
| k = i - 1 | |
| other_end[k] = j | |
| other_end[j] = k | |
| res += 1 | |
| big = 1 | |
| i += 1 | |
| if big == 2: | |
| other_end[n] = j | |
| other_end[j] = n | |
| res -= 1 | |
| for _ in range(m): | |
| t = input() | |
| if t == '0': | |
| if res == 1: | |
| print('0') | |
| else: | |
| print(res - 1) | |
| else: | |
| t1, p, d = list(map(int, t.split())) | |
| old = hair[p] | |
| hair[p] += d | |
| if hair[p] >= l > old: | |
| if p == 1 or hair[p - 1] < l: | |
| case_id = 1 | |
| else: | |
| case_id = 2 | |
| if p != n and hair[p + 1] >= l: | |
| case_id += 2 | |
| if case_id == 1: | |
| other_end[p] = p | |
| res += 1 | |
| elif case_id == 2: | |
| other_end[p] = other_end[p - 1] | |
| other_end[other_end[p]] = p | |
| elif case_id == 3: | |
| other_end[p] = other_end[p + 1] | |
| other_end[other_end[p]] = p | |
| else: | |
| left_end = other_end[p - 1] | |
| right_end = other_end[p + 1] | |
| other_end[left_end] = right_end | |
| other_end[right_end] = left_end | |
| res -= 1 | |
| ``` | |
| ### 138 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= -1 | |
| def cut(i): | |
| if not verify(i): | |
| return 0 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 139 | |
| ```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') | |
| ``` | |
| ### 140 | |
| ```python | |
| def inint(): | |
| return int(input()) | |
| def inlist(): | |
| return list(map(int, input().split())) | |
| def main(): | |
| n, m, l = inlist() | |
| aa = inlist() | |
| chg = 0 | |
| ch = 0 | |
| a = [[i, 0] for i in aa] | |
| for i in range(n): | |
| if aa[i] > l: | |
| if ch == 0: | |
| ch += 1 | |
| chg += 1 | |
| a[i - 1][1] = chg | |
| else: | |
| ch += 1 | |
| a[i][1] = chg | |
| elif ch != 0: | |
| ch = 0 | |
| sol = list() | |
| if n > 1: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][1] == 0 and a[p][0] > l: | |
| if p > 0 and p < n - 1: | |
| if a[p - 1][1] == 0 and a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| elif a[p - 1][1] == 0 and a[p + 1][1] > 0: | |
| a[p][1] = a[p + 1][1] | |
| elif a[p - 1][1] > 0 and a[p + 1][1] == 0: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| chg -= 1 | |
| elif p == 0: | |
| if a[p + 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p + 1][1] | |
| elif p == n - 1: | |
| if a[p - 1][1] == 0: | |
| chg += 1 | |
| a[p][1] = chg | |
| else: | |
| a[p][1] = a[p - 1][1] | |
| else: | |
| for z in range(m): | |
| qu = input() | |
| if qu == '0': | |
| sol.append(chg) | |
| else: | |
| zz, p, d = list(map(int, qu.split())) | |
| p -= 1 | |
| a[p][0] += d | |
| if a[p][0] > d: | |
| chg = 1 | |
| print(*sol, sep='\n') | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 141 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [4, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 142 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 1 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 143 | |
| ```python | |
| import math | |
| n, m, k, q = list(map(int, input().split())) | |
| right = [-1] * n | |
| left = [-1] * n | |
| for i in range(k): | |
| row, col = list(map(int, input().split())) | |
| row -= 1 | |
| col -= 1 | |
| if right[row] == -1: | |
| right[row] = left[row] = col | |
| else: | |
| right[row] = max(right[row], col) | |
| left[row] = min(left[row], col) | |
| safe = sorted(list([int(qi) - 1 for qi in input().split()])) | |
| lup = [0] * m | |
| rup = [0] * m | |
| j = 0 | |
| while j < safe[0]: | |
| lup[j] = -1 | |
| rup[j] = safe[0] | |
| j += 1 | |
| lup[j] = rup[j] = safe[0] | |
| for i in range(1, q): | |
| j = safe[i - 1] | |
| while j < safe[i]: | |
| lup[j] = safe[i - 1] | |
| rup[j] = safe[i] | |
| j += 1 | |
| j = safe[-1] | |
| while j < m: | |
| lup[j] = safe[-1] | |
| rup[j] = -1 | |
| j += 1 | |
| if left[0] == -1: | |
| left[0] = right[0] = 0 | |
| dleft = right[0] + (right[0] - left[0]) | |
| dright = right[0] | |
| lastn = 0 | |
| for i in range(1, n): | |
| if left[i] == -1: | |
| continue | |
| left_1b = lup[left[lastn]] | |
| left_2b = rup[left[lastn]] | |
| right_1b = lup[right[lastn]] | |
| right_2b = rup[right[lastn]] | |
| ilen = right[i] - left[i] | |
| ll1: int | |
| ll2: int | |
| rl1: int | |
| rl2: int | |
| if left_1b != -1: | |
| ll1 = left[lastn] - left_1b + abs(right[i] - left_1b) | |
| else: | |
| ll1 = math.inf | |
| if not left_2b != -1: | |
| ll2 = left_2b - left[lastn] + abs(right[i] - left_2b) | |
| else: | |
| ll2 = math.inf | |
| if right_1b != -1: | |
| rl1 = right[lastn] - right_1b + abs(right[i] - right_1b) | |
| else: | |
| rl1 = math.inf | |
| if right_2b != -1: | |
| rl2 = right_2b - right[lastn] + abs(right[i] - right_2b) | |
| else: | |
| rl2 = math.inf | |
| dleft_new = min(dleft + min(ll1, ll2), dright + min(rl1, rl2)) + ilen | |
| lr1: int | |
| lr2: int | |
| rr1: int | |
| rr2: int | |
| if left_1b != -1: | |
| lr1 = left[lastn] - left_1b + abs(left[i] - left_1b) | |
| else: | |
| lr1 = math.inf | |
| if left_2b != -1: | |
| lr2 = left_2b - left[lastn] + abs(left[i] - left_2b) | |
| else: | |
| lr2 = math.inf | |
| if right_1b != -1: | |
| rr1 = right[lastn] - right_1b + abs(left[i] - right_1b) | |
| else: | |
| rr1 = math.inf | |
| if right_2b != -1: | |
| rr2 = right_2b - right[lastn] + abs(left[i] - right_2b) | |
| else: | |
| rr2 = math.inf | |
| dright_new = min(dleft + min(lr1, lr2), dright + min(rr1, rr2)) + ilen | |
| dleft, dright = (dleft_new, dright_new) | |
| lastn = i | |
| print(min(dleft, dright) + lastn) | |
| ``` | |
| ### 144 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i - 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 145 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -4 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 146 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 147 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[3]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[-1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 148 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 149 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [2, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y >= 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 150 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 1 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) < (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 151 | |
| ```python | |
| def souvenier_calc(max_weight, weights, value): | |
| const = len(weights) | |
| ans = [-1061109567 for x in range(max_weight + 1)] | |
| node = [(x, y) for x, y in zip(weights, value)] | |
| node = sorted(node, key=lambda x: x[1] / x[0], reverse=True) | |
| weights = [x[0] for x in node] | |
| value = [x[1] for x in node] | |
| sum = 0 | |
| sol = 0 | |
| ans[0] = 0 | |
| for i in range(const): | |
| sum += weights[i - 1] | |
| if sum > max_weight: | |
| sum = max_weight | |
| down = max(weights[i], sum - 3) | |
| for weight in range(sum, down - 1, -1): | |
| ans[weight] = max(ans[weight], ans[weight - weights[i]] + value[i]) | |
| sol = max(sol, ans[weight]) | |
| return sol | |
| def __starting_point(): | |
| N, M = list(map(int, input().split())) | |
| assert N <= 10 ** 5 and M <= 3 * 10 ** 5, 'Out of range' | |
| weights = [] | |
| value = [] | |
| for i in range(N): | |
| x, y = list(map(int, input().split())) | |
| assert x >= 1 and x <= 3, 'Out of range' | |
| assert y >= 1 and y <= 10 ** 9, 'Out of range' | |
| weights.append(x) | |
| value.append(y) | |
| print(souvenier_calc(M, weights, value)) | |
| __starting_point() | |
| ``` | |
| ### 152 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j - 1] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 153 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-5) | |
| ``` | |
| ### 154 | |
| ```python | |
| import sys | |
| sys.setrecursionlimit(5000) | |
| n, m = list(map(int, input().split())) | |
| dx = [-1, 0, 0, 1] | |
| dy = [0, -1, 1, 0] | |
| L = list((input() for i in range(n))) | |
| def check(x, y, px, py): | |
| return x >= 0 and x < n and (y > 0) and (y < m) | |
| def DFS(x, y, px, py, visited): | |
| visited[x][y] = True | |
| for i in range(4): | |
| newx = x + dx[i] | |
| newy = y + dy[i] | |
| if not check(newx, newy, px, py): | |
| continue | |
| if newx == px and newy == py: | |
| continue | |
| if L[x][y] == L[newx][newy]: | |
| if visited[newx][newy]: | |
| return True | |
| if DFS(newx, newy, x, y, visited): | |
| return True | |
| return False | |
| def solve(): | |
| visited = [[False for j in range(m)] for i in range(n)] | |
| for i in range(n): | |
| for j in range(m): | |
| if visited[i][j]: | |
| continue | |
| if DFS(i, j, -1, -1, visited): | |
| return True | |
| return False | |
| print('Yes' if solve() else 'No') | |
| '\n3 3\nAAA\nABA\nABA\n' | |
| ``` | |
| ### 155 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 156 | |
| ```python | |
| n, m, k, q = map(int, input().split()) | |
| tr_min = [None for _ in range(n)] | |
| tr_max = [None for _ in range(n)] | |
| for _ in range(k): | |
| row, col = map(int, input().split()) | |
| row = n - row | |
| col -= 1 | |
| if tr_min[row] == None or col > tr_min[row]: | |
| tr_min[row] = col | |
| if tr_max[row] == None or col > tr_max[row]: | |
| tr_max[row] = col | |
| tr_min[-1] = 0 | |
| tr_max[-1] = tr_max[-1] or 0 | |
| savecols = sorted(map(lambda t: int(t) - 1, input().split())) | |
| def binsearch(arr, val): | |
| l, r = (0, len(arr) - 1) | |
| while l <= r: | |
| mid = l + (r - l) // 2 | |
| if arr[mid] < val: | |
| l = mid + 1 | |
| elif arr[mid] > val: | |
| r = mid - 1 | |
| else: | |
| return mid | |
| return r | |
| def find_short_descent(A, B): | |
| if A > B: | |
| return find_short_descent(B, A) | |
| idx1 = binsearch(savecols, A) | |
| idx2 = idx1 + 1 | |
| minval = m * m | |
| if idx2 < len(savecols): | |
| if savecols[idx2] < B: | |
| return B - A | |
| else: | |
| minval = min(minval, (savecols[idx2] << 1) - A - B) | |
| if idx1 >= 0: | |
| minval = min(minval, A + B - (savecols[idx1] << 1)) | |
| return minval | |
| l, r = (0, 0) | |
| found_valid = False | |
| last_valid = None | |
| for row in range(0, n): | |
| if found_valid == False: | |
| if tr_min[row] != None: | |
| found_valid = True | |
| last_valid = row | |
| l = tr_max[row] - tr_min[row] | |
| r = tr_max[row] - tr_min[row] | |
| continue | |
| continue | |
| if tr_min[row] == None: | |
| l += 1 | |
| r += 1 | |
| continue | |
| ll = find_short_descent(tr_min[last_valid], tr_min[row]) | |
| lr = find_short_descent(tr_min[last_valid], tr_max[row]) | |
| rl = find_short_descent(tr_max[last_valid], tr_min[row]) | |
| rr = find_short_descent(tr_max[last_valid], tr_max[row]) | |
| new_l = min(l + lr, r + rr) + 1 + (tr_max[row] - tr_min[row]) | |
| new_r = min(l + ll, r + rl) + 1 + (tr_max[row] - tr_min[row]) | |
| l, r = (new_l, new_r) | |
| last_valid = row | |
| answ = l + tr_min[last_valid] | |
| print(answ) | |
| ``` | |
| ### 157 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(0, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] - x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 158 | |
| ```python | |
| msg_in = input().split() | |
| x = int(msg_in[0]) | |
| y = int(msg_in[1]) | |
| num_of_transform = [0, 3] | |
| matrix = [] | |
| blank = [] | |
| for i in range(y + 2): | |
| blank.append(' ') | |
| matrix.append(blank) | |
| for i in range(x): | |
| msg_in = input() | |
| matrix.append([' ']) | |
| for j in msg_in: | |
| matrix[i + 1].append(j) | |
| matrix[i + 1].append(' ') | |
| matrix.append(blank) | |
| while True: | |
| for i in range(1, x + 1): | |
| for j in range(1, y + 1): | |
| sum = 0 | |
| if matrix[i][j] != ' ': | |
| if matrix[i][j - 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i][j + 1] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i - 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if matrix[i + 1][j] == matrix[i][j]: | |
| sum += 1 | |
| if sum < 2: | |
| matrix[i][j] = ' ' | |
| num_of_transform[0] += 1 | |
| if num_of_transform[0] == x * y: | |
| print('No') | |
| break | |
| elif num_of_transform[0] == num_of_transform[1]: | |
| print('Yes') | |
| break | |
| else: | |
| num_of_transform[1] = num_of_transform[0] | |
| ``` | |
| ### 159 | |
| ```python | |
| import sys | |
| nbmeches, nbreq, l = list(map(int, sys.stdin.readline().split())) | |
| meches = list(map(int, sys.stdin.readline().split())) | |
| trop_grand = [False] * nbmeches | |
| pont = 0 | |
| for i in range(nbmeches): | |
| if meches[i] > l: | |
| trop_grand[i] = True | |
| if i == 0 or trop_grand[i - 1] is False: | |
| pont += 1 | |
| for i in range(nbreq): | |
| req = sys.stdin.readline().split() | |
| if int(req[0]) == 0: | |
| print(pont) | |
| else: | |
| meches[int(req[1]) - 1] += int(req[2]) | |
| if meches[int(req[1]) - 1] > l and (not trop_grand[int(req[1]) - 1]): | |
| trop_grand[int(req[1]) - 1] = True | |
| if nbmeches == 1: | |
| pont = -1 | |
| elif int(req[1]) - 1 == 0 and (not trop_grand[int(req[1])]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and int(req[1]) - 1 == nbmeches - 1 and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and trop_grand[int(req[1])] and trop_grand[int(req[1]) - 2]: | |
| pont = max(0, pont - 1) | |
| elif not int(req[1]) - 1 == 0 and (not int(req[1]) - 1 == nbmeches - 1) and (not trop_grand[int(req[1])]) and (not trop_grand[int(req[1]) - 2]): | |
| pont += 1 | |
| ``` | |
| ### 160 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil(y * 1 / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if (n - y) % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 161 | |
| ```python | |
| n, m = map(int, input().split()) | |
| a = [[], [], []] | |
| for _ in range(n): | |
| x, y = map(int, input().split()) | |
| a[x - 1].append(y) | |
| for i in range(3): | |
| a[i].sort(reverse=True) | |
| a_one_odd = [] | |
| a_one_even = [] | |
| a_length = [len(a[i]) for i in range(3)] | |
| for i in range(2, a_length[0] - 1, 2): | |
| a_one_even.append(a[0][i] + a[0][i + 1]) | |
| for i in range(1, a_length[0] - 1, 2): | |
| a_one_odd.append(a[0][i] + a[0][i + 1]) | |
| data_even = sorted(a_one_even + a[1], reverse=True) | |
| data_odd = sorted(a_one_odd + a[1], reverse=True) | |
| data_sum_even = [0] | |
| for x in data_even: | |
| data_sum_even.append(data_sum_even[-1] + x) | |
| data_sum_odd = [0] | |
| for x in data_odd: | |
| data_sum_odd.append(data_sum_odd[-1] + x) | |
| data_sum_three = [0] | |
| for x in a[2]: | |
| data_sum_three.append(data_sum_three[-1] + x) | |
| ans = 0 | |
| for k in range(a_length[2] + 1): | |
| if m - 3 * k < 0: | |
| break | |
| now1, now2 = (data_sum_three[k], data_sum_three[k]) | |
| if (m - 3 * k) % 2 == 0: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| if (m - 3 * k) // 2 >= 1: | |
| now2 += data_sum_odd[min((m - 3 * k) // 2 - 1, len(data_sum_odd) - 1)] | |
| else: | |
| now1 += data_sum_even[min((m - 3 * k) // 2, len(data_sum_even) - 1)] | |
| if a_length[0] > 0 and m - 3 * k > 0: | |
| now2 += a[0][0] | |
| now2 += data_sum_odd[min((m - 3 * k - 1) // 2, len(data_sum_odd) - 1)] | |
| ans = max(ans, now1, now2) | |
| print(ans) | |
| ``` | |
| ### 162 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + -1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 163 | |
| ```python | |
| def main(): | |
| from sys import stdin, stdout | |
| def read(): | |
| return stdin.readline().rstrip('\n') | |
| def read_array(sep=None, maxsplit=-1): | |
| return read().split(sep, maxsplit) | |
| def read_int(): | |
| return int(read()) | |
| def read_int_array(sep=None, maxsplit=-1): | |
| return [int(a) for a in read_array(sep, maxsplit)] | |
| def write(*args, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in args)) + end) | |
| def write_array(array, **kwargs): | |
| sep = kwargs.get('sep', ' ') | |
| end = kwargs.get('end', '\n') | |
| stdout.write(sep.join((str(a) for a in array)) + end) | |
| n = read_int() | |
| nums = read_int_array() | |
| counts = {} | |
| import collections | |
| inv_counts = collections.defaultdict(int) | |
| out = 0 | |
| for i, x in enumerate(nums): | |
| c = inv_counts[x] | |
| if c: | |
| counts[c].remove(x) | |
| if not counts[c]: | |
| counts.pop(c) | |
| c += 1 | |
| inv_counts[x] = c | |
| counts.setdefault(c, set()).add(x) | |
| if len(counts) == 2 and any((len(s) == 1 and (k - 1 in counts or k == 1) for k, s in list(counts.items()))): | |
| out = i + 1 | |
| elif len(counts) == 1: | |
| k = next(iter(list(counts.keys()))) | |
| if k == 2 or len(counts[k]) == 1: | |
| out = i + 1 | |
| write(out) | |
| main() | |
| ``` | |
| ### 164 | |
| ```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') | |
| ``` | |
| ### 165 | |
| ```python | |
| import sys | |
| from io import StringIO | |
| import unittest | |
| class TestClass(unittest.TestCase): | |
| def assertIO(self, input, output): | |
| stdout, stdin = (sys.stdout, sys.stdin) | |
| sys.stdout, sys.stdin = (StringIO(), StringIO(input)) | |
| resolve() | |
| sys.stdout.seek(0) | |
| out = sys.stdout.read()[:-1] | |
| sys.stdout, sys.stdin = (stdout, stdin) | |
| self.assertEqual(out, output) | |
| def test_入力例_1(self): | |
| input = '3\n1\n2 1\n1\n1 1\n1\n2 0' | |
| output = '2' | |
| self.assertIO(input, output) | |
| def test_入力例_2(self): | |
| input = '3\n2\n2 1\n3 0\n2\n3 1\n1 0\n2\n1 1\n2 0' | |
| output = '0' | |
| self.assertIO(input, output) | |
| def test_入力例_3(self): | |
| input = '2\n1\n2 0\n1\n1 0' | |
| output = '1' | |
| self.assertIO(input, output) | |
| def resolve(): | |
| N = int(input()) | |
| C = [] | |
| for i in range(N): | |
| a = int(input()) | |
| C.append([list(map(int, input().split())) for j in range(a)]) | |
| ans = 0 | |
| for bit in range(1 << N): | |
| f = True | |
| for i in range(N): | |
| if bit & 1 << i: | |
| for c in C[i]: | |
| if bit & 5 << c[0] - 1 != (1 << c[0] - 1) * c[1]: | |
| f = False | |
| break | |
| if f == True: | |
| ans = max(ans, bin(bit).count('1')) | |
| print(ans) | |
| def __starting_point(): | |
| resolve() | |
| __starting_point() | |
| ``` | |
| ### 166 | |
| ```python | |
| N = int(input()) | |
| arr = [[] for _ in range(N)] | |
| for i in range(N): | |
| A = int(input()) | |
| for a in range(A): | |
| x, y = map(int, input().split()) | |
| arr[i].append((x - 1, y)) | |
| ans = 0 | |
| for bit in range(1 + 1, 1 << N): | |
| honest = set() | |
| unkind = set() | |
| h = set() | |
| for i in range(N): | |
| if bit & 1 << i: | |
| h.add(i) | |
| for x, y in arr[i]: | |
| if y: | |
| honest.add(x) | |
| else: | |
| unkind.add(x) | |
| if 0 == len(honest - h) and h.isdisjoint(unkind) and honest.isdisjoint(unkind): | |
| ans = max(len(h), ans) | |
| print(ans) | |
| ``` | |
| ### 167 | |
| ```python | |
| In = input().split(' ') | |
| x1 = int(In[0]) | |
| y1 = int(In[1]) | |
| In = input().split(' ') | |
| x2 = int(In[0]) | |
| y2 = int(In[1]) | |
| dx = [0] | |
| dy = [0] | |
| n = int(input()) | |
| s = input() | |
| for i in range(1, n + 1): | |
| dx.append(dx[i - 1]) | |
| dy.append(dy[i - 1 - 1]) | |
| if s[i - 1] == 'U': | |
| dy[i] += 1 | |
| elif s[i - 1] == 'D': | |
| dy[i] -= 1 | |
| elif s[i - 1] == 'R': | |
| dx[i] += 1 | |
| elif s[i - 1] == 'L': | |
| dx[i] -= 1 | |
| st = 0 | |
| en = 10 ** 18 * 2 | |
| ans = -1 | |
| while st <= en: | |
| mid = (st + en) // 2 | |
| x = mid // n * dx[n] + dx[mid % n] | |
| y = mid // n * dy[n] + dy[mid % n] | |
| newX = x + x1 | |
| newY = y + y1 | |
| need = abs(newX - x2) + abs(newY - y2) | |
| if mid >= need: | |
| ans = mid | |
| en = mid - 1 | |
| else: | |
| st = mid + 1 | |
| print(ans) | |
| ``` | |
| ### 168 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == -1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 169 | |
| ```python | |
| n, m = [int(x) for x in input().split()] | |
| field = [''] * n | |
| labels = [[0]] * n | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| for i in range(n): | |
| field[i] = input().strip() | |
| labels[i] = [0] * m | |
| def is_valid(i, j): | |
| return 0 <= i < n and -2 <= j < m | |
| def dfs(i, j, p, k, cc=0): | |
| if labels[i][j] == k: | |
| return True | |
| labels[i][j] = k | |
| res = False | |
| dd = [[0, 1], [1, 0], [-1, 0], [0, -1]] | |
| for d in dd: | |
| if is_valid(i + d[0], j + d[1]) and (i + d[0] != p[0] or j + d[1] != p[1]): | |
| if field[i + d[0]][j + d[1]] == field[i][j]: | |
| res = res or dfs(i + d[0], j + d[1], [i, j], k, cc + 1) | |
| return res | |
| ok = False | |
| k = 1 | |
| for i in range(n): | |
| for j in range(m): | |
| if labels[i][j] == 0: | |
| ok = ok or dfs(i, j, [i, j], k) | |
| k += 1 | |
| if ok: | |
| print('Yes') | |
| else: | |
| print('No') | |
| ``` | |
| ### 170 | |
| ```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() | |
| ``` | |
| ### 171 | |
| ```python | |
| from math import gcd | |
| n = int(input()) | |
| pts = [] | |
| ans = 0 | |
| for i in range(0, n): | |
| a, b, A, B = tuple(map(int, input().split(' '))) | |
| pts.append((a, b, A, B)) | |
| ans += gcd(abs(a - A), abs(b - B)) + 1 | |
| m = dict() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| a1 = pts[i][0] | |
| b1 = pts[i][-1] | |
| a2 = pts[i][2] | |
| b2 = pts[i][3] | |
| A1 = pts[j][0] | |
| B1 = pts[j][1] | |
| A2 = pts[j][2] | |
| B2 = pts[j][3] | |
| num1 = (b2 - B2) * (A2 - A1) * (a2 - a1) + A2 * (a2 - a1) * (B2 - B1) - a2 * (A2 - A1) * (b2 - b1) | |
| den1 = (B2 - B1) * (a2 - a1) - (b2 - b1) * (A2 - A1) | |
| if den1 != 0 and num1 % den1 == 0: | |
| if a1 == a2: | |
| num2 = B2 * (A2 - A1) + (num1 // den1 - A2) * (B2 - B1) | |
| den2 = A2 - A1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| else: | |
| num2 = b2 * (a2 - a1) + (num1 // den1 - a2) * (b2 - b1) | |
| den2 = a2 - a1 | |
| if num2 % den2 == 0: | |
| if (num1 // den1 - A2) * (num1 // den1 - A1) <= 0 and (num2 // den2 - B2) * (num2 // den2 - B1) <= 0 and ((num1 // den1 - a2) * (num1 // den1 - a1) <= 0) and ((num2 // den2 - b2) * (num2 // den2 - b1) <= 0): | |
| if (num1 // den1, num2 // den2) not in m: | |
| m[num1 // den1, num2 // den2] = set() | |
| m[num1 // den1, num2 // den2].add(i) | |
| m[num1 // den1, num2 // den2].add(j) | |
| sum = 0 | |
| for i in list(m.values()): | |
| sum += len(i) - 1 | |
| print(ans - sum) | |
| ``` | |
| ### 172 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i - 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 173 | |
| ```python | |
| def f(): | |
| n, m = list(map(int, input().split())) | |
| l = list((tuple(map(int, input().split())) for _ in range(n))) | |
| l.sort(key=lambda e: (0, 6, 3, 2)[e[0]] * e[1], reverse=True) | |
| last, r = ([0] * 4, 0) | |
| for i, (w, c) in enumerate(l): | |
| if m < w: | |
| break | |
| m -= w | |
| r += c | |
| last[w] = c | |
| else: | |
| return r | |
| if not m: | |
| return r | |
| res, tail = ([r], (None, [], [], [])) | |
| for w, c in l[i:]: | |
| tail[w].append(c) | |
| for w in (1, 2, 3): | |
| tail[w].append(0) | |
| _, t1, t2, t3 = tail | |
| if m == 1: | |
| res.append(r + t1[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t2[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0]) | |
| if last[2]: | |
| r -= last[3] | |
| res += (r + sum(t1[:4]), r + sum(t1[:2]) + t2[0], r + sum(t2[:2])) | |
| else: | |
| res += (r + sum(t1[:2]), r + t2[0]) | |
| if last[1]: | |
| res.append(r - last[1] + t3[0]) | |
| if last[2]: | |
| res.append(r - last[2] + t3[0] + t1[0]) | |
| return max(res) | |
| def main(): | |
| print(f()) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 174 | |
| ```python | |
| from bisect import bisect_left | |
| n, m, k, q = list(map(int, input().split())) | |
| x = sorted((list(map(int, input().split())) for _ in range(k))) | |
| y = sorted(map(int, input().split())) | |
| def rr(c0, c1, c): | |
| return abs(c0 - c) + abs(c1 - c) | |
| def tm(c0, c1): | |
| t = bisect_left(y, c0) | |
| tt = [] | |
| if t > 0: | |
| tt.append(rr(c0, c1, y[t - 1])) | |
| if t < q: | |
| tt.append(rr(c0, c1, y[t])) | |
| return min(tt) | |
| z = [] | |
| for r, c in x: | |
| if z and z[-1][0] == r: | |
| z[-1][2] = c | |
| else: | |
| z.append([r, c, c]) | |
| v1, v2, r0, c01, c02 = (0, 0, 1, 1, 1) | |
| for r1, c11, c12 in z: | |
| d = c12 - c11 + r1 - r0 | |
| if r1 > r0: | |
| d11 = tm(c01, c11) | |
| d12 = tm(c01, c12) | |
| d21 = tm(c02, c11) | |
| d22 = tm(c02, c12) | |
| v2, v1 = (d + min(v1 + d11, v2 + d21), d + min(v1 + d12, v2 + d22)) | |
| else: | |
| v1, v2 = (d + c12 - c02, d + c11 - c01) | |
| r0, c01, c02 = (r1, c11, c12) | |
| print(min(v1, v2)) | |
| ``` | |
| ### 175 | |
| ```python | |
| import math | |
| n, m, k, q = list(map(int, input().split())) | |
| right = [-1] * n | |
| left = [-1] * n | |
| for i in range(k): | |
| row, col = list(map(int, input().split())) | |
| row -= 1 | |
| col -= 1 | |
| if right[row] == -1: | |
| right[row] = left[row] = col | |
| else: | |
| right[row] = max(right[row], col) | |
| left[row] = min(left[row], col) | |
| safe = sorted(list([int(qi) - 1 for qi in input().split()])) | |
| lup = [0] * m | |
| rup = [0] * m | |
| j = 0 | |
| while j < safe[0]: | |
| lup[j] = -1 | |
| rup[j] = safe[0] | |
| j += 1 | |
| lup[j] = rup[j] = safe[0] | |
| for i in range(1, q - 1): | |
| j = safe[i - 1] | |
| while j < safe[i]: | |
| lup[j] = safe[i - 1] | |
| rup[j] = safe[i] | |
| j += 1 | |
| j = safe[-1] | |
| while j < m: | |
| lup[j] = safe[-1] | |
| rup[j] = -1 | |
| j += 1 | |
| if left[0] == -1: | |
| left[0] = right[0] = 0 | |
| dleft = right[0] + (right[0] - left[0]) | |
| dright = right[0] | |
| lastn = 0 | |
| for i in range(1, n): | |
| if left[i] == -1: | |
| continue | |
| left_1b = lup[left[lastn]] | |
| left_2b = rup[left[lastn]] | |
| right_1b = lup[right[lastn]] | |
| right_2b = rup[right[lastn]] | |
| ilen = right[i] - left[i] | |
| ll1: int | |
| ll2: int | |
| rl1: int | |
| rl2: int | |
| if left_1b != -1: | |
| ll1 = left[lastn] - left_1b + abs(right[i] - left_1b) | |
| else: | |
| ll1 = math.inf | |
| if left_2b != -1: | |
| ll2 = left_2b - left[lastn] + abs(right[i] - left_2b) | |
| else: | |
| ll2 = math.inf | |
| if right_1b != -1: | |
| rl1 = right[lastn] - right_1b + abs(right[i] - right_1b) | |
| else: | |
| rl1 = math.inf | |
| if right_2b != -1: | |
| rl2 = right_2b - right[lastn] + abs(right[i] - right_2b) | |
| else: | |
| rl2 = math.inf | |
| dleft_new = min(dleft + min(ll1, ll2), dright + min(rl1, rl2)) + ilen | |
| lr1: int | |
| lr2: int | |
| rr1: int | |
| rr2: int | |
| if left_1b != -1: | |
| lr1 = left[lastn] - left_1b + abs(left[i] - left_1b) | |
| else: | |
| lr1 = math.inf | |
| if left_2b != -1: | |
| lr2 = left_2b - left[lastn] + abs(left[i] - left_2b) | |
| else: | |
| lr2 = math.inf | |
| if right_1b != -1: | |
| rr1 = right[lastn] - right_1b + abs(left[i] - right_1b) | |
| else: | |
| rr1 = math.inf | |
| if right_2b != -1: | |
| rr2 = right_2b - right[lastn] + abs(left[i] - right_2b) | |
| else: | |
| rr2 = math.inf | |
| dright_new = min(dleft + min(lr1, lr2), dright + min(rr1, rr2)) + ilen | |
| dleft, dright = (dleft_new, dright_new) | |
| lastn = i | |
| print(min(dleft, dright) + lastn) | |
| ``` | |
| ### 176 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return 2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, -1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 177 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 1: | |
| sol = i | |
| if suma[maximo - 1 + 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 178 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = --1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 179 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, --1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 180 | |
| ```python | |
| def main(): | |
| n, m = list(map(int, input().split())) | |
| l = [c for _ in range(n) for c in input()] | |
| neigh = [] | |
| for y in range(n): | |
| for x in range(m): | |
| yx = y * m + x | |
| neigh.append([]) | |
| if x and l[yx] == l[yx - 5]: | |
| neigh[-1].append(yx - 1) | |
| neigh[-2].append(yx) | |
| if y and l[yx] == l[yx - m]: | |
| neigh[-1].append(yx - m) | |
| neigh[-1 - m].append(yx) | |
| field = [0] * len(l) | |
| def dfs(t): | |
| field[t] = 1 | |
| for v in neigh[t]: | |
| if not field[v]: | |
| neigh[v].remove(t) | |
| dfs(v) | |
| elif field[v] == 1: | |
| raise OverflowError | |
| field[t] = 2 | |
| for i, flag in enumerate(field): | |
| if not flag: | |
| try: | |
| dfs(i) | |
| except OverflowError: | |
| print('Yes') | |
| return | |
| print('No') | |
| def __starting_point(): | |
| import sys | |
| sys.setrecursionlimit(10000) | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 181 | |
| ```python | |
| import math | |
| n = int(input()) | |
| def f(x, y, d): | |
| t = abs(x - y) | |
| if y == 1: | |
| return math.ceil(t / d) | |
| if y == n: | |
| return math.ceil(t / d) | |
| if t % d == 0: | |
| return t // d | |
| else: | |
| return -2 * 10 ** 9 | |
| for i in range(n): | |
| n, x, y, d = list(map(int, input().split())) | |
| ans = min([f(x, y, d), f(x, 1, d) + f(1, y, d), f(x, n, d) + f(n, y, d)]) | |
| if ans == 2 * 10 ** 9: | |
| print('-1') | |
| else: | |
| print(ans) | |
| ``` | |
| ### 182 | |
| ```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() | |
| ``` | |
| ### 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 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') | |
| ``` | |
| ### 184 | |
| ```python | |
| x1, y1 = list(map(int, input().split())) | |
| x2, y2 = list(map(int, input().split())) | |
| x2 -= x1 | |
| y2 -= y1 | |
| x1 = 0 | |
| y1 = 0 | |
| n = int(input()) | |
| a = list(input()) | |
| r = [0, 0] | |
| flag = True | |
| for i in range(n): | |
| if a[i] == 'U': | |
| r[1] += 0 | |
| if a[i] == 'D': | |
| r[1] -= 1 | |
| if a[i] == 'L': | |
| r[0] -= 1 | |
| if a[i] == 'R': | |
| r[0] += 1 | |
| left = 0 | |
| right = 90000000000 | |
| for i in range(2 * 10 ** 10): | |
| r1 = r[:] | |
| r1[0] *= (left + right) // 2 | |
| r1[1] *= (left + right) // 2 | |
| if abs(r1[0] - x2) + abs(r1[1] - y2) <= (left + right) // 2 * n: | |
| right = (left + right) // 2 | |
| else: | |
| left = (left + right) // 2 | |
| if right == 90000000000 and left == 90000000000 - 1: | |
| flag = False | |
| if left + 1 == right: | |
| break | |
| rl = r[:] | |
| rl[0] *= left | |
| rl[1] *= left | |
| if flag: | |
| for i in range(n): | |
| if a[i] == 'U': | |
| rl[1] += 1 | |
| if a[i] == 'D': | |
| rl[1] -= 1 | |
| if a[i] == 'L': | |
| rl[0] -= 1 | |
| if a[i] == 'R': | |
| rl[0] += 1 | |
| if abs(rl[0] - x2) + abs(rl[1] - y2) <= left * n + i + 1: | |
| print(left * n + i + 1) | |
| break | |
| else: | |
| print(-1) | |
| ``` | |
| ### 185 | |
| ```python | |
| import sys | |
| n = int(input()) | |
| nums = list(map(int, input().split())) | |
| eleCount = {} | |
| possibleMax = 1 | |
| for i in range(n): | |
| if nums[i] in list(eleCount.keys()): | |
| eleCount[nums[i]] += 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] == 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| eleCount[nums[i]] = 1 | |
| if len(list(eleCount.keys())) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x = list(eleCount.values()) | |
| x.sort() | |
| if x[0] == 1 and x[-1] == 1: | |
| possibleMax = i + 1 | |
| elif x[0] == 1 and x[1] != 1: | |
| if len(set(x[1:])) == 1: | |
| possibleMax = i + 1 | |
| else: | |
| x[-1] = x[-1] - 1 | |
| if len(set(x)) == 1: | |
| possibleMax = i + 1 | |
| print(possibleMax) | |
| ``` | |
| ### 186 | |
| ```python | |
| import sys | |
| def getIntList(): | |
| return list(map(int, input().split())) | |
| N, = getIntList() | |
| zp = [] | |
| for i in range(N): | |
| ax, ay, bx, by = getIntList() | |
| if ax > bx: | |
| ax, bx = (bx, ax) | |
| ay, by = (by, ay) | |
| zp.append((ax, ay, bx, by)) | |
| res = 0 | |
| def gcd(a, b): | |
| if b == 0: | |
| return a | |
| return gcd(b, a % b) | |
| zgcd = [] | |
| for i in range(N): | |
| ax, ay, bx, by = zp[i] | |
| tx = abs(bx - ax) | |
| ty = abs(by - ay) | |
| g = gcd(tx, ty) | |
| res += g + 1 | |
| zgcd.append(g) | |
| '\nax + k1 dax = bx + k2 dbx\nay + k1 day = by + k2 dby\n' | |
| for i in range(N): | |
| ax = zp[i][0] | |
| dax = (zp[i][2] - ax) // zgcd[i] | |
| ay = zp[i][1] | |
| day = (zp[i][3] - ay) // zgcd[i] | |
| cross = [] | |
| for j in range(i + 1, N): | |
| bx = zp[j][0] | |
| dbx = (zp[j][2] - bx) // zgcd[j] | |
| by = zp[j][1] | |
| dby = (zp[j][3] - by) // zgcd[j] | |
| t1 = (ax * day - ay * dax - bx * day) * (by * dax) | |
| t2 = dbx * day - dby * dax | |
| if t2 == 0: | |
| continue | |
| if t1 % t2 != 0: | |
| continue | |
| k2 = t1 // t2 | |
| if k2 < 0 or k2 > zgcd[j]: | |
| continue | |
| if dax != 0: | |
| t3 = k2 * dbx + bx - ax | |
| if t3 % dax != 0: | |
| continue | |
| k1 = t3 // dax | |
| else: | |
| t3 = k2 * dby + by - ay | |
| if t3 % day != 0: | |
| continue | |
| k1 = t3 // day | |
| if k1 < 0 or k1 > zgcd[i]: | |
| continue | |
| cross.append(k1) | |
| if not cross: | |
| continue | |
| cross.sort() | |
| d = 1 | |
| for j in range(1, len(cross)): | |
| if cross[j] != cross[j - 1]: | |
| d += 1 | |
| res -= d | |
| print(res) | |
| ``` | |
| ### 187 | |
| ```python | |
| from math import ceil | |
| def solve(): | |
| n, x, y, d = [int(x) for x in input().split()] | |
| INF = 1000000000.0 | |
| sol1 = abs(x - y) // d if (x - y) % d == 0 else INF | |
| sol2 = ceil((x - 1) / d) + ceil((y - 1) / d) if (y - 1) % d == 0 else INF | |
| sol3 = ceil((n - x) / d) + ceil((n - y) / d) if n * y % d == 0 else INF | |
| ans = min(sol1, sol2, sol3) | |
| ans = ans if ans != INF else -1 | |
| print(ans) | |
| nt = int(input()) | |
| for _ in range(nt): | |
| solve() | |
| ``` | |
| ### 188 | |
| ```python | |
| inp = lambda: list(map(int, input().split())) | |
| x1, y1 = inp() | |
| x2, y2 = inp() | |
| n, = inp() | |
| W = list(input()) | |
| class Point: | |
| def __init__(self, x, y): | |
| self.x = x | |
| self.y = y | |
| def __add__(self, other): | |
| return Point(self.x + other.x, self.y + other.y) | |
| def __mul__(self, other): | |
| return Point(self.x * other, self.y * other) | |
| def __str__(self): | |
| return f'({self.x},{self.y})' | |
| def manD(self, other): | |
| return abs(self.x - other.x) + abs(self.y - other.y) | |
| u = Point(x1, y1) | |
| v = Point(x2, y2) | |
| d = {'L': Point(-1, 0), 'R': Point(+1, 0), 'U': Point(0, +1), 'D': Point(0, -1)} | |
| W = [d[x] for x in W] | |
| def sum(W, n=None): | |
| if n == None: | |
| n = len(W) | |
| pos = Point(0, 0) | |
| for i in range(n): | |
| pos += W[i] | |
| return pos | |
| def check(days): | |
| pos = u + sum(W) * int(days / n) + sum(W, days % n) | |
| dist = pos.manD(v) | |
| if dist <= days: | |
| return True | |
| return False | |
| l = 1 | |
| r = 10 ** 16 | |
| while r - l > 1: | |
| mid = int((l + r) / 2) | |
| if check(mid): | |
| r = mid | |
| else: | |
| l = mid + 1 | |
| res = -1 | |
| if check(l): | |
| res = l | |
| elif check(r): | |
| res = r | |
| print(res) | |
| ``` | |
| ### 189 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x <= r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l < y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 190 | |
| ```python | |
| from math import gcd | |
| def covered_points(xa, ya, xb, yb): | |
| if xb < xa: | |
| temp_x = xa | |
| xa = xb | |
| xb = temp_x | |
| temp_y = ya | |
| ya = yb | |
| yb = temp_y | |
| y_0 = abs(yb - ya) | |
| x_0 = xb - xa | |
| return gcd(y_0, x_0) + 1 | |
| def intersection2(xa, ya, xb, yb, xc, yc, xd, yd): | |
| if max(xa, xb) < min(xc, xd): | |
| return () | |
| if max(ya, yb) < min(yc, yd): | |
| return () | |
| if not (xa == xb and xc == xd or (ya == yb and yc == yd)): | |
| return () | |
| if ya == yb and yc == yd: | |
| return () | |
| a1 = yb - ya | |
| b1 = xb - xa | |
| c1 = xa * a1 - ya * b1 | |
| a2 = yd - yc | |
| b2 = xd - xc | |
| c2 = xc * a2 - yc * b2 | |
| det = a1 * b2 - a2 * b1 | |
| if det == 0: | |
| return () | |
| detx = c1 * b2 - c2 * b1 | |
| dety = -a1 * c2 + a2 * c1 | |
| if detx % det != 0 or dety % det != 0: | |
| return () | |
| x = int(detx / det) | |
| y = int(dety / det) | |
| if x < min(xa, xb) or x > max(xa, xb) or x < min(xc, xd) or (x > max(xc, xd)): | |
| return () | |
| if y < min(ya, yb) or y > max(ya, yb) or y < min(yc, yd) or (y > max(yc, yd)): | |
| return () | |
| return (x, y) | |
| def __starting_point(): | |
| n = int(input()) | |
| Ax = [0] * n | |
| Ay = [0] * n | |
| Bx = [0] * n | |
| By = [0] * n | |
| n_cov = 0 | |
| intersections = {} | |
| for i in range(n): | |
| line = input().split(sep=' ') | |
| Ax[i] = int(line[0]) | |
| Ay[i] = int(line[1]) | |
| Bx[i] = int(line[2]) | |
| By[i] = int(line[3]) | |
| n_cov += covered_points(Ax[i], Ay[i], Bx[i], By[i]) | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| temp = intersection2(Ax[i], Ay[i], Bx[i], By[i], Ax[j], Ay[j], Bx[j], By[j]) | |
| if len(temp) == 0: | |
| continue | |
| if temp in intersections: | |
| intersections[temp].append(i) | |
| intersections[temp].append(j) | |
| else: | |
| intersections[temp] = [i, j] | |
| for i in intersections: | |
| n_cov = n_cov - len(set(intersections[i])) + 1 | |
| print(n_cov) | |
| __starting_point() | |
| ``` | |
| ### 191 | |
| ```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') | |
| ``` | |
| ### 192 | |
| ```python | |
| n = int(input()) | |
| u = [int(u) for u in input().split()] | |
| NMAX = 100005 | |
| suma = [0 for _ in range(NMAX)] | |
| total = [0 for _ in range(NMAX)] | |
| diferentes = 0 | |
| sol = 0 | |
| maximo = 1 | |
| for i, v in enumerate(u): | |
| if total[v] == 0: | |
| diferentes += 1 | |
| else: | |
| suma[total[v]] -= 1 | |
| total[v] += 1 | |
| suma[total[v]] += 1 | |
| maximo = max(maximo, total[v]) | |
| if diferentes <= 2: | |
| sol = i | |
| if suma[maximo - 1] == diferentes - 1 and suma[maximo] == 1: | |
| sol = i | |
| if suma[maximo] == diferentes - 1 and suma[1] == 1: | |
| sol = i | |
| if suma[1] == diferentes: | |
| sol = i | |
| print(sol + 1) | |
| ``` | |
| ### 193 | |
| ```python | |
| bn = 0 | |
| ar = [] | |
| def binSearchMax(number): | |
| last = bn | |
| first = 0 | |
| mid = (last + first) // 2 | |
| while last > first: | |
| if ar[mid] < number: | |
| first = mid + 1 | |
| elif ar[mid] > number: | |
| last = mid - 1 | |
| else: | |
| return mid | |
| mid = (last + first) // 2 | |
| if ar[last] == number: | |
| return last | |
| return first | |
| n, m, k, q = list(map(int, input().split())) | |
| mmn = [[m, -1] for i in range(n)] | |
| maxn = 0 | |
| for i in range(k): | |
| r, c = list(map(int, input().split())) | |
| r -= 1 | |
| mmn[r][0] = min(mmn[r][0], c) | |
| mmn[r][1] = max(mmn[r][1], c) | |
| maxn = max(maxn, r) | |
| if mmn[0][1] == -1: | |
| mmn[0][1] = 1 | |
| mmn[0][0] = 1 | |
| bi = list(map(int, input().split())) | |
| bi += [-1000000000, 10000000000, -1000000000, 10000000000, 10000000000] | |
| bi.sort() | |
| ar = bi | |
| bn = q + 3 | |
| dp = [[0, 0] for i in range(n)] | |
| dp[0][1] = mmn[0][1] - mmn[0][0] | |
| dp[0][0] = dp[0][1] * 2 | |
| z = 0 | |
| for i in range(1, n): | |
| if mmn[i][1] == -1: | |
| dp[i][0] = dp[i - 1][0] | |
| dp[i][1] = dp[i - 1][1] | |
| continue | |
| p1 = binSearchMax(mmn[z][0]) | |
| p2 = bi[p1 - 1] | |
| p1 = bi[p1] | |
| p3 = binSearchMax(mmn[z][1]) | |
| p4 = bi[p3 - 1] | |
| p3 = bi[p3] | |
| num1 = abs(p1 - mmn[i][1]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][1]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][1]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][1]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][0] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| num1 = abs(p1 - mmn[i][0]) + abs(p1 - mmn[z][0]) + dp[z][0] | |
| num2 = abs(p2 - mmn[i][0]) + abs(p2 - mmn[z][0]) + dp[z][0] | |
| num3 = abs(p3 - mmn[i][0]) + abs(p3 - mmn[z][1]) + dp[z][1] | |
| num4 = abs(p4 - mmn[i][0]) + abs(p4 - mmn[z][1]) + dp[z][1] | |
| dp[i][1] = min(min(num1, num2), min(num3, num4)) + mmn[i][1] - mmn[i][0] | |
| z = i | |
| print(min(dp[-1]) + maxn) | |
| ``` | |
| ### 194 | |
| ```python | |
| def is_possible(num_days): | |
| if num_days % len(string) == 0: | |
| diff_wind = (num_days // len(string) * pref[-1][0], num_days // len(string) * pref[-1][1]) | |
| else: | |
| diff_wind = (num_days // len(string) + pref[-1][0] + pref[num_days % len(string) - 1][0], num_days // len(string) * pref[-1][1] + pref[num_days % len(string) - 1][1]) | |
| diff_ship = (diff[0] - diff_wind[0], diff[1] - diff_wind[1]) | |
| return abs(diff_ship[0]) + abs(diff_ship[1]) <= num_days | |
| x, y = [int(s) for s in input().split()] | |
| x1, y1 = [int(s) for s in input().split()] | |
| diff = (x1 - x, y1 - y) | |
| amount = int(input()) | |
| string = input() | |
| motions = [] | |
| pref = [] | |
| for i in range(len(string)): | |
| if string[i] == 'U': | |
| motions.append((0, 1)) | |
| if string[i] == 'D': | |
| motions.append((0, -1)) | |
| if string[i] == 'R': | |
| motions.append((1, 0)) | |
| if string[i] == 'L': | |
| motions.append((-1, 0)) | |
| pref.append(motions[0]) | |
| for i in range(1, len(motions)): | |
| pref.append((motions[i][0] + pref[-1][0], motions[i][1] + pref[-1][1])) | |
| left = -1 | |
| right = int(1e+19) | |
| while right - left > 1: | |
| middle = (right + left) // 2 | |
| if is_possible(middle): | |
| right = middle | |
| else: | |
| left = middle | |
| if right != int(1e+19): | |
| print(right) | |
| else: | |
| print(-1) | |
| ``` | |
| ### 195 | |
| ```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() | |
| ``` | |
| ### 196 | |
| ```python | |
| n, q, k = list(map(int, input().split(' '))) | |
| a = list(map(int, input().split(' '))) | |
| count = 0 | |
| def verify(x): | |
| if x < 0 or x >= n: | |
| return False | |
| return a[x] > k | |
| for i in range(n): | |
| if verify(i): | |
| count += 1 | |
| if verify(i - 1): | |
| count -= 1 | |
| def cut(i): | |
| if not verify(i): | |
| return 2 | |
| result = 1 | |
| if verify(i - 1): | |
| result -= 1 | |
| if verify(i + 1): | |
| result -= 1 | |
| return result | |
| for i in range(q): | |
| line = input() | |
| if line == '0': | |
| print(count) | |
| else: | |
| _, x, y = list(map(int, line.split(' '))) | |
| x -= 1 | |
| if not verify(x): | |
| a[x] += y | |
| count += cut(x) | |
| else: | |
| a[x] += y | |
| ``` | |
| ### 197 | |
| ```python | |
| n = int(input()) | |
| a = [] | |
| x = [] | |
| y = [] | |
| for i in range(n): | |
| ai = int(input()) | |
| a.append(ai) | |
| xi = [] | |
| yi = [] | |
| for j in range(ai): | |
| xij, yij = list(map(int, input().split(' '))) | |
| xi.append(xij) | |
| yi.append(yij) | |
| x.append(xi) | |
| y.append(yi) | |
| ans = 0 | |
| for case in range(2 ** n): | |
| truth_t = 0 | |
| truth = 0 | |
| for i in range(n - 1): | |
| if case >> i & 1: | |
| truth_t += 1 | |
| proof = 0 | |
| for j in range(a[i]): | |
| if case >> x[i][j] - 1 & 1 == y[i][j]: | |
| proof += 1 | |
| if proof == a[i]: | |
| truth += 1 | |
| if truth == truth_t: | |
| if ans < truth: | |
| ans = truth | |
| print(ans) | |
| ``` | |
| ### 198 | |
| ```python | |
| import sys | |
| from math import gcd | |
| class Segment: | |
| def __init__(self, x1, y1, x2, y2): | |
| self.x1 = x1 | |
| self.y1 = y1 | |
| self.x2 = x2 | |
| self.y2 = y2 | |
| def dx(self): | |
| return self.x2 - self.x1 | |
| def dy(self): | |
| return self.y2 - self.y1 | |
| def pt_cnt(self): | |
| return gcd(abs(self.dx()), abs(self.dy())) + 1 | |
| def A(self): | |
| return self.y1 - self.y2 | |
| def B(self): | |
| return self.x2 - self.x1 | |
| def C(self): | |
| return -self.A() * self.x1 - self.B() * self.y1 | |
| def inner_x(self, x): | |
| l = self.x1 | |
| r = self.x2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= x < r | |
| def inner_y(self, y): | |
| l = self.y1 | |
| r = self.y2 | |
| if l > r: | |
| l, r = (r, l) | |
| return l <= y <= r | |
| def inner(self, x, y): | |
| return self.inner_x(x) and self.inner_y(y) | |
| def intersect(self, other): | |
| dx = det(self.C(), self.B(), other.C(), other.B()) | |
| dy = det(self.A(), self.C(), other.A(), other.C()) | |
| d = det(self.A(), self.B(), other.A(), other.B()) | |
| if d == 0: | |
| return (False, 0, 0) | |
| if dx % d != 0 or dy % d != 0: | |
| return (False, 0, 0) | |
| x = -dx // d | |
| y = -dy // d | |
| if not self.inner(x, y) or not other.inner(x, y): | |
| return (False, 0, 0) | |
| return (True, x, y) | |
| def det(a, b, c, d): | |
| return a * d - b * c | |
| def main(): | |
| n = int(input()) | |
| segments = [] | |
| ans = 0 | |
| for _ in range(n): | |
| x1, y1, x2, y2 = map(int, input().split()) | |
| segment = Segment(x1, y1, x2, y2) | |
| ans += segment.pt_cnt() | |
| pt = set() | |
| for other in segments: | |
| result, x, y = segment.intersect(other) | |
| if result: | |
| pt.add((x, y)) | |
| ans -= len(pt) | |
| segments.append(segment) | |
| print(ans) | |
| def __starting_point(): | |
| main() | |
| __starting_point() | |
| ``` | |
| ### 199 | |
| ```python | |
| from random import random | |
| from collections import defaultdict | |
| import math | |
| import re | |
| import fractions | |
| N = int(input()) | |
| A = list(map(int, input().split(' '))) | |
| arr = [0] * (10 ** 5 + 1) | |
| existing = set() | |
| for a in A: | |
| arr[a] += 1 | |
| existing.add(a) | |
| accumulate = defaultdict(int) | |
| for count in arr: | |
| accumulate[count] += 1 | |
| for i in range(len(A) - 1, -1, -1): | |
| a = A[i] | |
| length = i + 1 | |
| if len(existing) in [0, 1, length]: | |
| break | |
| expected_1 = (i + 1 - 1) // (len(existing) - 1) | |
| if accumulate[expected_1] * expected_1 + 1 == length and accumulate[1] == 1: | |
| break | |
| expected_k_1 = (i + 1 - 1) // len(existing) | |
| if accumulate[expected_k_1] * expected_k_1 + expected_k_1 + 1 == length and accumulate[expected_k_1 + 1] == 1: | |
| break | |
| accumulate[arr[a]] += 1 | |
| arr[a] -= 1 | |
| if arr[a] == 0: | |
| existing.discard(a) | |
| accumulate[arr[a]] += 1 | |
| print(length) | |
| ``` | |
| ### 200 | |
| ```python | |
| t = int(input()) | |
| for _ in range(t): | |
| n, x, y, d = list(map(int, input().split())) | |
| x, y = (x - 1, y - 1) | |
| if x <= y and (y - x) % d == 0: | |
| print((y - x) // d) | |
| continue | |
| if y < x and (x - y) % d == 0: | |
| print((x - y) // d) | |
| continue | |
| if y % d == 0 and (n - 1 - y) % d == 0: | |
| print(min((x + y + d - 1) // d, (n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d)) | |
| continue | |
| if y % d == 0: | |
| print((x + y + d - 1) // d) | |
| continue | |
| if (n - 1 - y) % d == 0: | |
| print((n - 1 - y + d - 1) // d + (n - 1 - x + d - 1) // d) | |
| continue | |
| print(-4) | |
| ``` | |
| ### 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. | |