description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) l1, r1 = list(map(int, input().split())) l2, r2 = list(map(int, input().split())) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 if l2 <= r1: per_interval = abs(l1 - l2) + abs(r1 - r2) already_overlap = min(r1, r2) - max(l1, l2) k -= already_overlap * n k = max(k, 0) if per_interval * n >= k: print(k) continue cost = per_interval * n cost += 2 * (k - cost) print(cost) continue diff = l2 - r1 per_interval = r2 - l1 if per_interval >= k: print(diff + k) continue cost = diff + per_interval rem_n = n - 1 rem_k = k - per_interval while rem_n and diff < per_interval and rem_k >= per_interval: rem_n -= 1 rem_k -= per_interval cost += diff + per_interval if rem_n and diff < per_interval: cost_from_new = diff + rem_k cost_from_cur = 2 * rem_k cost += min(cost_from_new, cost_from_cur) else: cost += 2 * rem_k print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline T = int(input()) for testcase in range(1, T + 1): n, k = list(map(int, input().split())) a, b = list(map(int, input().split())) s, t = list(map(int, input().split())) if a > s: a, s = s, a b, t = t, b if s <= b and n * (min(b, t) - s) >= k: print(0) elif s <= b: now = n * (min(b, t) - s) w = n * (s - a + abs(t - b)) if now + w >= k: print(k - now) else: now += w print(w + (k - now) * 2) elif t - a >= k: print(s - b + k) else: res = 10**20 for i in range(1, n + 1): tmp = i * (s - b) if i * (t - a) >= k: tmp += k else: tmp += i * (t - a) + (k - i * (t - a)) * 2 res = min(res, tmp) print(res)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] l1, r1 = [int(x) for x in input().split()] l2, r2 = [int(x) for x in input().split()] its = min(r1, r2) - max(l1, l2) if its >= 0: k -= n * its if k <= 0: ans = 0 else: aoc = max(r1, r2) - min(l1, l2) if n * (aoc - its) >= k: ans = k else: ans = n * (aoc - its) k -= n * (aoc - its) ans += k * 2 print(ans) else: gap = -its aoc = max(r1, r2) - min(l1, l2) minn = float("inf") for i in range(1, n + 1): total = gap * i if aoc * i >= k: total += k else: total += aoc * i + (k - aoc * i) * 2 minn = min(minn, total) print(minn)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) over = min(r1, r2) - max(l1, l2) moves = 0 if (l1, r1) == (l2, r2): if (r1 - l1) * n >= k: print(0) else: k -= (r1 - l1) * n print(2 * k) elif over > 0: over *= n if over >= k: print(0) else: o = max(r1, r2) - min(l1, l2) - over // n if over + o * n >= k: print(k - over) else: moves += o * n over += o * n print(moves + (k - over) * 2) else: d = -1 * over o = max(r1, r2) - min(l1, l2) x = k // o k = k % o if n > x: moves += x * (d + o) if d < k or x == 0: moves += d + k else: moves += 2 * k else: moves += n * (d + o) + ((x - n) * o + k) * 2 print(moves)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline aaaa = list() for kek in range(int(input())): n, k = map(int, input().split()) al, ar = map(int, input().split()) bl, br = map(int, input().split()) L = n * max(0, min(ar, br) - max(al, bl)) if L >= k: aaaa.append(0) continue if ( al <= bl and bl <= ar or al <= br and br <= ar or bl <= ar and ar <= br or bl <= al and al <= br ): x = n * (abs(al - bl) + abs(ar - br)) if x + L >= k: aaaa.append(k - L) else: aaaa.append(2 * (k - L - x) + x) continue if al > bl: al, bl = bl, al ar, br = br, ar zero = bl - ar one = br - al ans = 0 if n <= k // one: ans += n * (one + zero) k -= n * one ans += 2 * k else: ans += k // one * (one + zero) if k // one > 0: k -= k // one * one ans += min(zero + k, 2 * k) else: ans += zero + k aaaa.append(ans) print(*aaaa, sep="\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys q = int(input()) for i in range(q): n, k = [int(j) for j in sys.stdin.readline().split()] l1, r1 = [int(j) for j in sys.stdin.readline().split()] l2, r2 = [int(j) for j in sys.stdin.readline().split()] if l2 < l1: l1, r1, l2, r2 = l2, r2, l1, r1 if r1 >= r2: kol = k - (r2 - l2) * n if kol <= 0: print(0) elif kol <= (r1 - l1 - (r2 - l2)) * n: print(kol) else: print((r1 - l1 - (r2 - l2)) * n + (kol - (r1 - l1 - (r2 - l2)) * n) * 2) elif r1 <= l2: kol = k sep = min(kol // (r2 - l1), n) rez = sep * (r2 - l1 + l2 - r1) if kol // (r2 - l1) == 0: rez += kol % (r2 - l1) + l2 - r1 elif kol - sep * (r2 - l1) <= l2 - r1: rez += (kol - sep * (r2 - l1)) * 2 elif sep == n: rez += (kol - sep * (r2 - l1)) * 2 else: rez += min(kol % (r2 - l1) * 2, kol % (r2 - l1) + l2 - r1) print(rez) else: kol = k - (r1 - l2) * n if kol <= 0: print(0) elif kol <= (r2 - l1 - (r1 - l2)) * n: print(kol) else: print((r2 - l1 - (r1 - l2)) * n + (kol - (r2 - l1 - (r1 - l2)) * n) * 2)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
T = int(input()) for _ in range(0, T): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) L1 = 0 R1 = 0 L2 = 0 R2 = 0 if l1 <= l2: L1 = l1 R1 = r1 L2 = l2 R2 = r2 else: L1 = l2 R1 = r2 L2 = l1 R2 = r1 ic = 0 curr = 0 if L2 > R1: ic = L2 - R1 curr = 0 else: ic = 0 curr = min(R1, R2) - max(L1, L2) one = max(R2, R1) - min(L1, L2) - curr rem = k - curr * n if rem <= 0: print(0) else: ans = ic + min(rem, one) rem -= one if rem <= 0: print(ans) elif rem < one: if ic + rem < 2 * rem and n - 1 > 0: ans += ic + rem else: ans += 2 * rem print(ans) elif ic + one < 2 * one and n - 1 > 0: kk1 = min(n - 1, rem // one) rem = rem - kk1 * one ans += kk1 * (ic + one) if kk1 < n - 1 and ic + rem < 2 * rem: ans += ic + rem else: ans += 2 * rem print(ans) else: ans += 2 * rem print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if r2 < r1: l1, r1, l2, r2 = l2, r2, l1, r1 if l1 <= l2 <= r1 <= r2: start = (r1 - l2) * n plusone = (r2 - l1 - (r1 - l2)) * n if k <= start: print(0) else: k -= start if k <= plusone: print(k) else: print(plusone + (k - plusone) * 2) elif l2 <= l1 <= r1 <= r2: start = (r1 - l1) * n plusone = (r2 - l2 - (r1 - l1)) * n if k <= start: print(0) else: k -= start if k <= plusone: print(k) else: print(plusone + (k - plusone) * 2) else: zero = l2 - r1 plusone = r2 - l1 ANS = 1 << 60 for use in range(1, n + 1): if k <= plusone * use: q, r = divmod(k, plusone) ANS1 = use * zero ANS1 += q * plusone if r > 0: ANS1 += r ANS = min(ANS, ANS1) else: ANS1 = (zero + plusone) * use k2 = k - plusone * use ANS1 += k2 * 2 ANS = min(ANS, ANS1) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def profit(l1, r1, l2, r2): if r1 > r2: r1, r2 = r2, r1 l1, l2 = l2, l1 if l2 > r1: return r2 - l1 elif l2 > l1: return r2 - l1 - r1 + l2 else: return r2 - r1 + l1 - l2 def tax(l1, r1, l2, r2): if r1 > r2: r1, r2 = r2, r1 l1, l2 = l2, l1 if l2 > r1: return l2 - r1 else: return 0 def overlap(l1, r1, l2, r2): if r1 > r2: r1, r2 = r2, r1 l1, l2 = l2, l1 if l2 > r1: return 0 elif l2 > l1: return r1 - l2 else: return r1 - l1 tt = int(input()) while tt: tt -= 1 n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) s = 0 p = profit(l1, r1, l2, r2) t = tax(l1, r1, l2, r2) o = overlap(l1, r1, l2, r2) k -= n * o if k <= 0: print(s) continue s += p + t k -= p n -= 1 if k <= 0: print(s + k) continue if k - p * n > 0: s += (p + t) * n s += 2 * (k - p * n) print(s) continue n = k // p if k - n * p > t: s += (p + t) * n s += k - n * p + t print(s) continue s += (p + t) * n s += 2 * (k - n * p) print(s)
FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = list(map(int, input().split())) l, r = list(map(int, input().split())) u, v = list(map(int, input().split())) if l > u: l, u = u, l r, v = v, r if l == u and v == r: overlap = r - l overlap = overlap * n if overlap >= k: print(0) else: rem = k - overlap print(2 * rem) elif u > r: dis = u - r overlap = v - l fans = 0 ans = 10**20 for i in range(n): fans += dis if overlap >= k: fans += k ans = min(ans, fans) break fans += overlap k -= overlap ans = min(ans, fans + 2 * k) print(ans) else: if u >= l and u <= r and v >= r: overlap = r - u e = u - l + v - r elif l >= u and v >= l and r >= v: overlap = v - l e = l - u + r - v elif u >= l and u <= r and v >= l and v <= r: overlap = v - u e = u - l + r - v overlap *= n e *= n if k <= overlap: print(0) else: k -= overlap if k <= e: print(k) else: ans = e k -= e ans += 2 * k print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
def inter(l1, r1, l2, r2): l = max(l1, l2) r = min(r1, r2) return max(r - l, 0) def solve(): n, k = map(int, input().split()) al, ar = map(int, input().split()) bl, br = map(int, input().split()) res = inter(al, ar, bl, br) * n goodt = max(ar, br) - min(al, bl) - inter(al, ar, bl, br) minhodi = 1000000000000 hodi = 0 to_soed = max(0, max(al, bl) - min(ar, br)) if res >= k: print(0) return 0 for i in range(n): hodi += to_soed ineed = k - res if goodt >= ineed: hodi += ineed minhodi = min(minhodi, hodi) break hodi += goodt res += goodt minhodi = min(hodi + (k - res) * 2, minhodi) print(minhodi) for i in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for i in range(t): nk = input().split() n = int(nk[0]) k = int(nk[1]) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 if l2 >= r1: reach = l2 - r1 span = r2 - l1 if k <= span: print(reach + k) continue else: steps = 0 while k >= span and n > 0: k -= span n -= 1 steps += reach + span if n == 0: steps += k * 2 print(steps) continue else: print(min(steps + reach + k, steps + k * 2)) continue else: if r2 >= r1: covered = r1 - l2 else: covered = r2 - l2 inhand = covered * n if inhand >= k: print(0) continue k -= inhand if r2 >= r1: addspan = (r2 - r1 + l2 - l1) * n else: addspan = (r1 - r2 + l2 - l1) * n if addspan >= k: print(k) continue else: k -= addspan steps = addspan + k * 2 print(steps) continue
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
from sys import stdin, stdout def segment_intersections(n, k, l1, r1, l2, r2): lmx = max(l1, l2) rmn = min(r1, r2) I = max(rmn - lmx, 0) * n if I >= k: return 0 k -= I lmn = min(l1, l2) rmx = max(r1, r2) cov = rmx - lmn - max(rmn - lmx, 0) if cov == 0: return k * 2 steps = lmx - lmn + (rmx - rmn) if steps / cov >= 2: if k > cov: return steps + (k - cov) * 2 else: return rmx - rmn + k d = min(k // cov, n) ans = steps * d if k // cov < n: subans1 = k % cov if rmn - lmx < 0: subans1 += abs(rmn - lmx) subans2 = 10**10 if k // cov > 0: subans2 = k % cov * 2 ans += min(subans1, subans2) else: ans += (k - cov * d) * 2 return ans t = int(stdin.readline()) for _ in range(t): n, k = map(int, stdin.readline().split()) l1, r1 = map(int, stdin.readline().split()) l2, r2 = map(int, stdin.readline().split()) ans = segment_intersections(n, k, l1, r1, l2, r2) stdout.write(str(ans) + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline (T,) = map(int, input().split()) for _ in range(T): n, k = map(int, input().split()) l, r = map(int, input().split()) x, y = map(int, input().split()) cost = 0 if r < x or y < l: cost = max(x - r, l - y) if cost: ff = 0 capacity = max(r, y) - min(l, x) - ff else: ff = min(r, y) - max(l, x) capacity = max(r, y) - min(l, x) - ff e = k - ff * n if e <= 0: print(0) continue if cost: R = 10**18 for i in range(1, n + 1): r = cost * i if e <= capacity * i: R = min(R, r + e) else: xx = capacity * i R = min(R, r + xx + (e - xx) * 2) print(R) elif e <= capacity * n: print(e) else: r = capacity * n print(r + (e - r) * 2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) def solve3(n, k, l1, r1, l2, r2): A, B = l2 - r1, r2 - l1 g = min(n, k // B) ans = g * (A + B) k -= g * B if g == n: ans = ans + 2 * k else: ans = min(ans + A + k, ans + 2 * k) print(ans) def solve1(n, k, l1, r1, l2, r2): A, B = l2 - r1, r2 - l1 ans = 100000000000000 for i in range(1, n + 1): ans_ = i * A if k <= i * B: ans = min(ans, ans_ + k) else: ans = min(ans, ans_ + i * B + (k - i * B) * 2) print(ans) def solve2(n, k, l1, r1, l2, r2): X = abs(l1 - l2) + abs(r1 - r2) if r2 < r1: k -= (r2 - l2) * n else: k -= (r1 - l2) * n if k <= X * n: print(max(k, 0)) else: ans = X * n k -= X * n print(ans + 2 * k) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, l2 = l2, l1 r1, r2 = r2, r1 if l2 >= r1: solve1(n, k, l1, r1, l2, r2) else: solve2(n, k, l1, r1, l2, r2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) x = max(0, min(r1, r2) - max(l1, l2)) a = max(r1, r2) - min(l1, l2) cnt = x * n if cnt >= k: print(0) continue if a == x: print(2 * (k - cnt)) continue i = min(n, (k - cnt) // (a - x)) d = k - cnt - i * (a - x) p = max(0, max(l1, l2) - min(r1, r2)) if k - cnt - (a - x) <= 0: print(p + k - cnt) continue if p < d and i < n: print(i * (p + a - x) + p + d) else: print(i * (p + a - x) + 2 * d)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] l1, r1 = [int(x) for x in input().split()] l2, r2 = [int(x) for x in input().split()] if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 if l2 < r1: start = (min(r1, r2) - max(l1, l2)) * n if start >= k: print(0) continue cheap = n * (max(r1, r2) - min(l1, l2)) - start if start + cheap >= k: print(k - start) continue else: print(cheap + (k - start - cheap) * 2) continue best = 10**100 cost_sf = 0 intersection_sf = 0 for j in range(n): cost_sf += l2 - r1 cheap = r2 - l1 if intersection_sf + cheap >= k: best = min(best, cost_sf + max(k - intersection_sf, 0)) intersection_sf += cheap cost_sf += cheap best = min(best, cost_sf + max((k - intersection_sf) * 2, 0)) print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 ans = 10**18 if r1 < l2: shared = 0 initial_cost = l2 - r1 max_len = r2 - l1 else: shared = min(r1, r2) - l2 initial_cost = 0 max_len = max(r1, r2) - l1 - shared k = max(0, k - shared * n) if k == 0: ans = 0 for i in range(n + 1): if i == 0 and initial_cost > 0: continue cost = i * initial_cost rest = max(0, k - i * max_len) ans = min(ans, cost + (k - rest) + rest * 2) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) ans = float("inf") if min(r1, r2) < max(l1, l2): i = max(l1, l2) - min(r1, r2) for j in range(1, n + 1): c = i * j m = (max(r1, r2) - min(l1, l2)) * j c += min(k, m) + max(0, k - m) * 2 ans = min(ans, c) else: k = max(0, k - n * (min(r1, r2) - max(l1, l2))) m = n * (abs(l1 - l2) + abs(r1 - r2)) ans = min(k, m) + 2 * max(0, k - m) print(int(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) if l1 > l2: l1, r1, l2, r2 = l2, r2, l1, r1 if l2 > r1: ans = float("inf") for i in range(1, n + 1): temp = i * (l2 - r1) limit = (r2 - l1) * i if k <= limit: temp += k ans = min(ans, temp) else: temp += limit K = k - limit temp += 2 * K ans = min(ans, temp) print(ans) else: count = n * (min(r1, r2) - l2) res = 0 if count > k: print(res) continue b = max(r2, r1) - min(l2, l1) - (min(r1, r2) - l2) for i in range(n): if k - count <= b: res += k - count break else: res += b count += b else: res += (k - count) * 2 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = map(int, input().split()) s = sorted([[int(i) for i in input().split()] for i in range(2)]) g = s[1][0] - s[0][1] ans = int(1e20) if s[1][0] < s[0][1] and s[0][1] > s[1][1]: k -= (s[1][1] - s[1][0]) * n mX = s[0][1] - s[0][0] - (s[1][1] - s[1][0]) ans = min(k, mX * n) + max(0, 2 * (k - mX * n)) print(max(0, ans)) continue if -g * n >= k: print(0) continue mX = s[1][0] - s[0][0] + abs(s[1][1] - s[1][0]) for i in range(1, n + 1): ans = min(ans, g * i + min(mX * i, k) + max(0, 2 * (k - mX * i))) assert ans > 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") T = ni() for _ in range(T): n, k = nm() a, b = nm() c, d = nm() if d < b: a, b, c, d = c, d, a, b if a <= c <= b: cur = (b - c) * n if (d - a) * n > k: print(max(k - cur, 0)) else: ans = (d - a) * n - cur print(ans + (k - (d - a) * n) * 2) elif c < a: cur = (b - a) * n if (d - c) * n > k: print(max(k - cur, 0)) else: ans = (d - c) * n - cur print(ans + (k - (d - c) * n) * 2) else: ans = 10**18 cur = 0 for i in range(n): cur += c - b if k > d - a: k -= d - a cur += d - a else: cur += k k -= k ans = min(ans, cur + k * 2) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
import sys input = sys.stdin.readline for f in range(int(input())): n, k = map(int, input().split()) l1, r1 = map(int, input().split()) l2, r2 = map(int, input().split()) intersect = False if l1 >= l2 and l1 <= r2: intersect = True if r1 >= l2 and r1 <= r2: intersect = True if l2 >= l1 and l2 <= r1: intersect = True if r2 >= l1 and r2 <= r1: intersect = True if intersect: l = min(l1, l2) r = max(r1, r2) totlen = r - l intlen = r2 - l2 + (r1 - l1) - totlen if n * intlen >= k: print(0) elif n * totlen >= k: print(k - n * intlen) else: s = n * (totlen - intlen) k -= n * totlen print(s + 2 * k) else: l = min(l1, l2) r = max(r1, r2) totlen = r - l dist = totlen - (r2 - l2) - (r1 - l1) s = 0 i = 0 while k > 0 and i < n: i += 1 s += dist if k <= totlen: s += k k = 0 else: k -= totlen s += totlen if k < dist: s += 2 * k k = 0 if k > 0: s += 2 * k print(s)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
from sys import gettrace, stdin if gettrace(): inputi = input else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n, k = map(int, inputi().split()) l1, r1 = map(int, inputi().split()) l2, r2 = map(int, inputi().split()) overlap = min(r1, r2) - max(l1, l2) if overlap * n >= k: print(0) return maxoverlap = max(r1, r2) - min(l1, l2) if overlap >= 0: if maxoverlap * n >= k: k -= overlap * n print(k) else: k -= maxoverlap * n print((maxoverlap - overlap) * n + k * 2) else: cost = 0 for i in range(n): noverlap = min(maxoverlap, k) cost += -overlap + noverlap k -= noverlap if k <= -overlap: cost += k * 2 break else: cost += k * 2 print(cost) def main(): t = int(inputi()) for _ in range(t): solve() main()
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given two lists of segments $[al_1, ar_1], [al_2, ar_2], \dots, [al_n, ar_n]$ and $[bl_1, br_1], [bl_2, br_2], \dots, [bl_n, br_n]$. Initially, all segments $[al_i, ar_i]$ are equal to $[l_1, r_1]$ and all segments $[bl_i, br_i]$ are equal to $[l_2, r_2]$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $1$. In other words, suppose you've chosen segment $[x, y]$ then you can transform it either into $[x - 1, y]$ or into $[x, y + 1]$. Let's define a total intersection $I$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $\sum\limits_{i=1}^{n}{\text{intersection_length}([al_i, ar_i], [bl_i, br_i])}$. Empty intersection has length $0$ and length of a segment $[x, y]$ is equal to $y - x$. What is the minimum number of steps you need to make $I$ greater or equal to $k$? -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) — the length of lists and the minimum required total intersection. The second line of each test case contains two integers $l_1$ and $r_1$ ($1 \le l_1 \le r_1 \le 10^9$) — the segment all $[al_i, ar_i]$ are equal to initially. The third line of each test case contains two integers $l_2$ and $r_2$ ($1 \le l_2 \le r_2 \le 10^9$) — the segment all $[bl_i, br_i]$ are equal to initially. It's guaranteed that the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- Print $t$ integers — one per test case. For each test case, print the minimum number of step you need to make $I$ greater or equal to $k$. -----Example----- Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Output 7 2000000000 0 -----Note----- In the first test case, we can achieve total intersection $5$, for example, using next strategy: make $[al_1, ar_1]$ from $[1, 2]$ to $[1, 4]$ in $2$ steps; make $[al_2, ar_2]$ from $[1, 2]$ to $[1, 3]$ in $1$ step; make $[bl_1, br_1]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps; make $[bl_2, br_2]$ from $[3, 4]$ to $[1, 4]$ in $2$ steps. In result, $I = \text{intersection_length}([al_1, ar_1], [bl_1, br_1]) + \text{intersection_length}([al_2, ar_2], [bl_2, br_2]) + \\ + \text{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$ In the second test case, we can make $[al_1, ar_1] = [0, 1000000000]$ in $1000000000$ steps and $[bl_1, br_1] = [0, 1000000000]$ in $1000000000$ steps. In the third test case, the total intersection $I$ is already equal to $10 > 3$, so we don't need to do any steps.
for _ in range(int(input())): n, k = list(map(int, input().split())) l_1, r1 = list(map(int, input().split())) l_2, r2 = list(map(int, input().split())) if r1 < l_2: pre_steps = l_2 - r1 elif l_1 > r2: pre_steps = l_1 - r2 else: pre_steps = 0 if pre_steps: easy_steps = r1 + r2 - l_1 - l_2 + pre_steps else: easy_steps = abs(l_1 - l_2) + abs(r1 - r2) k -= (min(r1, r2) - max(l_1, l_2)) * n ans = pre_steps * n + k * 2 for n1 in range(1, n + 1): cur_ans = pre_steps * n1 if easy_steps * n1 < k: cur_ans += k * 2 - easy_steps * n1 else: cur_ans += k ans = min(ans, cur_ans) if k <= 0: ans = 0 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
import sys spies, turns, note, final = map(int, input().split()) if note < final: next = 1 else: next = -1 answer = [] def pass_note(current_turn, next_watch, left, right, note): if current_turn == next_watch: if note in range(left, right + 1) or note + next in range(left, right + 1): return note, "X" if note < final: note += next return note, "R" else: note += next return note, "L" current_turn = 0 for turn in range(turns): next_watch, left, right = map(int, input().split()) while current_turn < next_watch: current_turn += 1 note, ans = pass_note(current_turn, next_watch, left, right, note) answer.append(ans) if note == final: print("".join(answer)) sys.exit() while note != final: current_turn += 1 note, ans = pass_note(current_turn, 0, -1, -1, note) answer.append(ans) print("".join(answer))
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR STRING IF VAR VAR VAR VAR RETURN VAR STRING VAR VAR RETURN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def checkKey(dict, key): if key in dict: return True return False n, m, s, f = map(int, input().split()) d = {} for i__ in range(m): t, l, r = map(int, input().split()) d[t] = l, r if f > s: ans = "" cp = s time = 1 while cp != f: if time in d: start = d[time][0] end = d[time][1] if cp >= start and cp <= end or cp + 1 >= start and cp + 1 <= end: ans += "X" time += 1 else: ans += "R" time += 1 cp += 1 else: ans += "R" time += 1 cp += 1 print(ans) elif f < s: ans = "" cp = s time = 1 while cp != f: if time in d: start = d[time][0] end = d[time][1] if cp >= start and cp <= end or cp - 1 >= start and cp - 1 <= end: ans += "X" time += +1 else: ans += "L" time += 1 cp -= 1 else: ans += "L" time += 1 cp -= 1 print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def checkKey(dict, key): if key in dict: return True return False n, m, s, f = map(int, input().split()) p = s d = -1 c = "L" if s < f: d = 1 c = "R" t = 1 ts = {} ans = "" for _ in range(m): x, y, z = map(int, input().split()) ts[x] = y, z while p != f: if t in ts: l, r = ts[t] if l <= p <= r or l <= p + d <= r: ans += "X" else: p += d ans += c else: p += d ans += c t += 1 print(ans)
FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) data = {} for _ in range(m): t, l, r = map(int, input().split()) data[t] = l, r k = 1 curr = s while curr != f: if k in data: if data[k][0] <= curr <= data[k][1]: print("X", end="") elif 1 < curr < n: if curr > f: if data[k][0] <= curr - 1 <= data[k][1]: print("X", end="") else: print("L", end="") curr -= 1 elif data[k][0] <= curr + 1 <= data[k][1]: print("X", end="") else: print("R", end="") curr += 1 elif curr == 1: if data[k][0] <= 2 <= data[k][1]: print("X", end="") else: print("R", end="") curr = 2 elif curr == n: if data[k][0] <= curr - 1 <= data[k][1]: print("X", end="") else: print("L", end="") curr -= 1 elif curr > f: print("L", end="") curr -= 1 else: print("R", end="") curr += 1 k += 1
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING IF NUMBER VAR VAR IF VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER VAR NUMBER
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) ans, p = "", 1 if s < f: for i in range(m): t, l, r = map(int, input().split()) if t > p: if t - p < f - s: ans += "R" * (t - p) s += t - p else: ans += "R" * (f - s) s = f break p = t + 1 if s + 1 < l or s > r: ans += "R" s += 1 if s == f: break else: ans += "X" else: for i in range(m): t, l, r = map(int, input().split()) if t > p: if t - p < s - f: ans += "L" * (t - p) s -= t - p else: ans += "L" * (s - f) s = f break p = t + 1 if s < l or s - 1 > r: ans += "L" s -= 1 if s == f: break else: ans += "X" print(ans + ("R" * (f - s) if f > s else "L" * (s - f)))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
R = lambda: list(map(int, input().split())) n, m, s, f = R() if s < f: d = 1 c = "R" else: d = -1 c = "L" res = "" i = 1 j = s t, l, r = R() k = 1 while j != f: if i > t and k < m: t, l, r = R() k += 1 if i == t and (l <= j <= r or l <= j + d <= r): res += "X" else: res += c j += d i += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) l = [] for i in range(m): t, c, r = map(int, input().split()) l.append([t, c, r]) if s > f: st = "" t = 1 i = 0 while i < m: if t == l[i][0]: if (s < l[i][1] or s > l[i][2]) and (s - 1 < l[i][1] or s - 1 > l[i][2]): s -= 1 st += "L" else: st += "X" i += 1 if s == f: break else: st += "L" s -= 1 if s == f: break t += 1 while s > f: s -= 1 st += "L" print(st) if s < f: st = "" t = 1 i = 0 while i < m: if t == l[i][0]: if (s < l[i][1] or s > l[i][2]) and (s + 1 < l[i][1] or s + 1 > l[i][2]): s += 1 st += "R" else: st += "X" i += 1 if s == f: break else: st += "R" s += 1 if s == f: break t += 1 while s < f: s += 1 st += "R" print(st)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def __starting_point(): inp = input() arr = inp.split(" ") n = int(arr[0]) m = int(arr[1]) s = int(arr[2]) f = int(arr[3]) ans = "" ch = "L" inc = -1 if s < f: ch = "R" inc = 1 tm = 1 done = False for i in range(m): inp = input() if done: continue arr = inp.split(" ") ct = int(arr[0]) l = int(arr[1]) r = int(arr[2]) while ct != tm: ans += ch tm += 1 s += inc if s == f: break if s != f and (s + inc >= l and s + inc <= r or s >= l and s <= r): ans += "X" elif s != f: ans += ch s += inc tm += 1 if s == f: done = True while s != f: s += inc ans += ch print(ans) __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) watch = {} for i in range(m): t, l, r = map(int, input().split()) watch[t] = l, r cur = s move, symbol = (1, "R") if s < f else (-1, "L") step = 1 while cur != f: if step in watch: l, r = watch[step] if l <= cur <= r or l <= cur + move <= r: print("X", end="") else: cur += move print(symbol, end="") else: cur += move print(symbol, end="") step += 1
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER STRING NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
import sys n, m, s, f = list(map(int, sys.stdin.readline().split())) L = [] R = [] T = [] for i in range(m): t, l, r = list(map(int, sys.stdin.readline().split())) T.append(t) L.append(l) R.append(r) if f > s: i = s step = 1 ind = 0 Ans = "" while i != f: if ind >= m or T[ind] != step: Ans += "R" i += 1 else: if i >= L[ind] and i <= R[ind] or i + 1 >= L[ind] and i + 1 <= R[ind]: Ans += "X" else: Ans += "R" i += 1 ind += 1 step += 1 else: i = s step = 1 ind = 0 Ans = "" while i != f: if ind >= m or T[ind] != step: Ans += "L" i -= 1 else: if i >= L[ind] and i <= R[ind] or i - 1 >= L[ind] and i - 1 <= R[ind]: Ans += "X" else: Ans += "L" i -= 1 ind += 1 step += 1 sys.stdout.write(Ans + "\n")
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
while True: try: def soln(n, m, s, t, stp): stp.sort() flg = True if s > t: flg = False i = 1 j = 0 ans = [] while True: fund = False while j < m: if stp[j][0] == i: j += 1 fund = True break if fund and not flg: a = max(stp[j - 1][1], stp[j - 1][2]) b = min(stp[j - 1][1], stp[j - 1][2]) if s <= a and s >= b: ans.append("X") elif s - 1 <= a and s - 1 >= b: ans.append("X") else: ans.append("L") s -= 1 elif fund and flg: a = min(stp[j - 1][1], stp[j - 1][2]) b = max(stp[j - 1][1], stp[j - 1][2]) if s >= a and s <= b: ans.append("X") elif s + 1 >= a and s + 1 <= b: ans.append("X") else: ans.append("R") s += 1 elif flg: ans.append("R") s += 1 else: ans.append("L") s -= 1 if s == t: break i += 1 print("".join(ans)) def read(): n, m, s, t = map(int, input().split()) stp = [] for i in range(m): a, b, c = map(int, input().split()) stp.append([a, b, c]) soln(n, m, s, t, stp) if __name__ == "__main__": read() except EOFError: break
WHILE NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) if s < f: step = "R" step_i = 1 else: step = "L" step_i = -1 ans = [] tp = 0 for i in range(m): t, l, r = map(int, input().split()) k = min(t - tp - 1, abs(f - s)) for j in range(k): ans.append(step) s += step_i * k if s == f: print("".join(ans)) break if not l <= s + step_i <= r and not l <= s <= r: s += step_i ans.append(step) else: ans.append("X") tp = t else: if s != f: ans.extend([step] * abs(f - s)) print("".join(ans))
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = list(map(int, input().split())) if s > f: a = "L" c = -1 elif s == f: a = "X" c = 0 else: a = "R" c = 1 l = [] for i in range(m): l.append(list(map(int, input().split()))) ans = "" if l[0][0] != 1: d = l[0][0] - 1 while s != f and d > 0: s = s + c ans = ans + a d = d - 1 for i in range(m): if s == f: break if i == 0: if s >= l[i][1] and s <= l[i][2] or s + c >= l[i][1] and s + c <= l[i][2]: ans = ans + "X" else: ans = ans + a s = s + c elif l[i][0] - l[i - 1][0] == 1: if s >= l[i][1] and s <= l[i][2] or s + c >= l[i][1] and s + c <= l[i][2]: ans = ans + "X" else: ans = ans + a s = s + c else: d = l[i][0] - l[i - 1][0] - 1 while s != f and d > 0: s = s + c ans = ans + a d = d - 1 if s == f: break if s >= l[i][1] and s <= l[i][2] or s + c >= l[i][1] and s + c <= l[i][2]: ans = ans + "X" else: ans = ans + a s = s + c if s == f: break if s != f: while s != f: ans = ans + a s = s + c print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) l = [] for i in range(m): k = list(map(int, input().split())) l.append(k) count = 1 ans = "" i = 0 while i < m: if count < l[i][0]: if f > s: ans += "R" * min(l[i][0] - count, f - s) s += min(l[i][0] - count, f - s) count = l[i][0] if f < s: ans += "L" * min(l[i][0] - count, s - f) s -= min(l[i][0] - count, s - f) count = l[i][0] else: if l[i][1] <= s <= l[i][2]: ans += "X" else: if f > s: if l[i][1] <= s + 1 <= l[i][2]: ans += "X" else: ans += "R" s += 1 if f < s: if l[i][1] <= s - 1 <= l[i][2]: ans += "X" else: ans += "L" s -= 1 i += 1 count += 1 if s == f: break if s != f: if s < f: ans += "R" * (f - s) else: ans += "L" * (s - f) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR STRING IF VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR STRING VAR STRING VAR NUMBER IF VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) t = dict() for i in range(m): t1, l1, r1 = map(int, input().split()) t[t1] = l1, r1 pos = s i = 1 while 1: if pos == f: break if i in t: if t[i][0] <= pos <= t[i][1]: print("X", end="") i += 1 continue elif f - pos > 0 and t[i][0] <= pos + 1 <= t[i][1]: print("X", end="") i += 1 continue elif f - pos < 0 and t[i][0] <= pos - 1 <= t[i][1]: print("X", end="") i += 1 continue if f - pos > 0: print("R", end="") pos += 1 elif pos - f > 0: print("L", end="") pos -= 1 i += 1
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING STRING VAR NUMBER VAR NUMBER
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = list(map(int, input().split())) d = {} for i in range(m): t, l, r = list(map(int, input().split())) d[t] = [l, r] ans = "" for i in range(1, n + m): if s == f: print(ans) return t = -1 if f < s: t = s - 1 else: t = s + 1 if i in d: if d[i][0] <= s and s <= d[i][1] or d[i][0] <= t and t <= d[i][1]: t = -1 if t == -1: ans += "X" else: if f < s: ans += "L" else: ans += "R" s = t print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR STRING IF VAR VAR VAR STRING VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def solve(s, f, t): res = "" step = 1 if s < f else -1 i = current = 0 while s != f: current += 1 ti, l, r = t[i] if i < len(t) else (-1, -1, -1) if ti == current: i += 1 if l <= s <= r or l <= s + step <= r: res += "X" continue s += step res += "R" if step > 0 else "L" return res def __starting_point(): parse = lambda: list(map(int, input().split())) n, m, s, f = parse() l = [parse() for _ in range(m)] print(solve(s, f, l)) __starting_point()
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR VAR VAR NUMBER STRING STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) d = {} final = 0 for i in range(m): a = list(map(int, input().split())) d[a[0]] = [a[1], a[2]] final = a[0] z = "" if s < f: i = 0 while s != f: i += 1 if i not in d: s += 1 z += "R" elif s not in range(d[i][0], d[i][1] + 1) and s + 1 not in range( d[i][0], d[i][1] + 1 ): s += 1 z += "R" else: z += "X" if s == f: break else: i = 0 while s != f: i += 1 if i not in d: s -= 1 z += "L" elif s not in range(d[i][0], d[i][1] + 1) and s - 1 not in range( d[i][0], d[i][1] + 1 ): s -= 1 z += "L" else: z += "X" if s == f: break print(z)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR STRING IF VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR STRING VAR STRING IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR STRING IF VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR STRING VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = [int(x) for x in input().split()] now = 1 d = 1 if f > s else -1 step = "R" if d == 1 else "L" ret = "" for _ in range(m): t, l, r = [int(x) for x in input().split()] if t > now: flag = False while t > now: s += d ret += step now += 1 if s == f: flag = True break if flag: break if l <= s <= r or l <= s + d <= r: ret += "X" else: ret += step s += d if s == f: break now += 1 while s != f: s += d ret += step print(ret)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR STRING VAR VAR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = map(int, input().split()) c, t1 = 1, 0 ans = "" t = 1 while s != f and t1 < m: t, l, r = map(int, input().split()) while t != c: if s < f: ans += "R" s += 1 else: ans += "L" s -= 1 if s == f: break c += 1 if s < f: if s >= l - 1 and s <= r: ans += "X" else: ans += "R" s += 1 elif s > f: if s <= r + 1 and s >= l: ans += "X" else: ans += "L" s -= 1 c += 1 t1 += 1 if s < f: ans += "R" * (f - s) elif s > f: ans += "L" * (s - f) print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR STRING VAR NUMBER VAR STRING VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR STRING VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR STRING VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR IF VAR VAR VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def geto(a, b): return max(0, min(a[1], b[1]) - max(a[0], b[0]) + 1) n, m, s, f = map(int, input().split()) di = {} for _ in range(m): t, l, r = map(int, input().split()) di[t] = [l, r] t = 1 ans = [] while s != f: if f > s: inte = [s, s + 1] if t in di and geto(inte, di[t]): ans += ["X"] else: ans += ["R"] s += 1 else: inte = [s - 1, s] if t in di and geto(inte, di[t]): ans += ["X"] else: ans += ["L"] s -= 1 t += 1 print("".join(ans))
FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR LIST STRING VAR LIST STRING VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR LIST STRING VAR LIST STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
def mi(): return map(int, input().split()) n, m, s, f = mi() t = [0] * m l = [0] * m r = [0] * m for i in range(m): t[i], l[i], r[i] = mi() curp = s curt = 1 i = 0 a = "" if f > s: a = (f - s) * "R" else: a = (s - f) * "L" while i < m and curp != f: if t[i] == curt: if a[0] == "R" and curp + 1 >= l[i] and curp + 1 <= r[i]: print("X", end="") elif a[0] == "L" and curp - 1 >= l[i] and curp - 1 <= r[i]: print("X", end="") elif curp >= l[i] and curp <= r[i]: print("X", end="") elif a[0] == "R": curp += 1 print(a[0], end="") else: curp -= 1 print(a[0], end="") i += 1 elif a[0] == "R": curp += 1 print(a[0], end="") else: curp -= 1 print(a[0], end="") curt += 1 if f > curp: print((f - curp) * "R") elif curp > f: print((curp - f) * "L")
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR STRING WHILE VAR VAR VAR VAR IF VAR VAR VAR IF VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR NUMBER STRING BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING IF VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER IF VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR STRING
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): n, m, s, f = map(int, input().split()) curr = s step = 1 steps = {} output = [] for i in range(m): t, l, r = map(int, input().split()) steps[t] = [l, r] if f > s: while curr != f: if step not in steps: output.append("R") step += 1 curr += 1 elif curr + 1 < steps[step][0] or curr > steps[step][1]: output.append("R") step += 1 curr += 1 else: output.append("X") step += 1 else: while curr != f: if step not in steps: output.append("L") step += 1 curr -= 1 elif curr - 1 > steps[step][1] or curr < steps[step][0]: output.append("L") step += 1 curr -= 1 else: output.append("X") step += 1 print("".join(output)) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR IF VAR VAR WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER WHILE VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR
n, m, s, f = list(map(int, input().split())) t = {} step = 1 ans = "" if s < f: sig = "R" else: sig = "L" for i in range(m): t0, l0, r0 = list(map(int, input().split())) t[t0] = [l0, r0] for i in range(1, n + m + 1): if s < f: u = s + 1 else: u = s - 1 if i in t: if t[i][0] <= s <= t[i][1] or t[i][0] <= u <= t[i][1]: ans += "X" else: ans += sig s = u else: ans += sig s = u if s == f: break print(ans)
ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR STRING VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of $n$ stations, enumerated from $1$ through $n$. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station $i$ is station $i+1$ if $1 \leq i < n$ or station $1$ if $i = n$. It takes the train $1$ second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver $m$ candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from $1$ through $m$. Candy $i$ ($1 \leq i \leq m$), now at station $a_i$, should be delivered to station $b_i$ ($a_i \neq b_i$). [Image] The blue numbers on the candies correspond to $b_i$ values. The image corresponds to the $1$-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. -----Input----- The first line contains two space-separated integers $n$ and $m$ ($2 \leq n \leq 5\,000$; $1 \leq m \leq 20\,000$) — the number of stations and the number of candies, respectively. The $i$-th of the following $m$ lines contains two space-separated integers $a_i$ and $b_i$ ($1 \leq a_i, b_i \leq n$; $a_i \neq b_i$) — the station that initially contains candy $i$ and the destination station of the candy, respectively. -----Output----- In the first and only line, print $n$ space-separated integers, the $i$-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station $i$. -----Examples----- Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 -----Note----- Consider the second sample. If the train started at station $1$, the optimal strategy is as follows. Load the first candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the first candy. Proceed to station $1$. This step takes $1$ second. Load the second candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the second candy. Proceed to station $1$. This step takes $1$ second. Load the third candy onto the train. Proceed to station $2$. This step takes $1$ second. Deliver the third candy. Hence, the train needs $5$ seconds to complete the tasks. If the train were to start at station $2$, however, it would need to move to station $1$ before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is $5+1 = 6$ seconds.
import sys input = sys.stdin.readline mii = lambda: map(int, input().split()) n, m = mii() a = [(0) for _ in range(n)] c = [(123456) for _ in range(n)] for _ in range(m): u, v = mii() u %= n v %= n if v < u: v += n a[u] += 1 if c[u] > v: c[u] = v ans = [] for i in list(range(1, n)) + [0]: out = 0 for j in range(i, n): if not a[j]: continue tmp = j - i + (a[j] - 1) * n + (c[j] - j) out = max(out, tmp) for j in range(i): if not a[j]: continue tmp = j + n - i + (a[j] - 1) * n + (c[j] - j) out = max(out, tmp) ans.append(out) print(" ".join(map(str, ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) OK = [[] for i in range(n + 1)] NG = [[] for i in range(n + 1)] for i in range(n): if A[i] == B[i]: OK[B[i]].append(i) else: NG[B[i]].append(i) ANS = [] OKflag = -1 for i in range(m - 1, -1, -1): if len(NG[C[i]]) > 0: x = NG[C[i]].pop() ANS.append(x) OK[C[i]].append(x) OKflag = x elif len(OK[C[i]]) > 0: x = OK[C[i]][0] ANS.append(x) OKflag = x elif OKflag != -1: ANS.append(OKflag) else: print("NO") break else: for i in range(n + 1): if len(NG[i]) > 0: print("NO") break else: print("YES") print(*[(a + 1) for a in ANS][::-1])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = lambda: sys.stdin.buffer.readline() for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) last = c[-1] if last in b: idx = -1 for i in range(n): if b[i] == last: idx = i break ch = [[] for i in range(n + 1)] ans = [(-1) for i in range(m)] for i in range(n): if a[i] != b[i]: ch[b[i]].append(i + 1) if ch[last]: ans[-1] = ch[last].pop() idx = ans[-1] - 1 else: ans[-1] = idx + 1 for i in range(m - 1): if ch[c[i]]: ans[i] = ch[c[i]].pop() else: ans[i] = idx + 1 if all(not ch[i] for i in range(n + 1)): print("YES") print(*ans) else: print("NO") else: print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
t = int(input()) for test in range(t): n, m = map(int, input().split()) need = [0] * n check = [0] * n a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = dict() for i in range(n): if a[i] != b[i]: if b[i] not in d: d[b[i]] = [i + 1] else: d[b[i]].append(i + 1) need[b[i] - 1] += 1 check[b[i] - 1] += 1 have = [0] * n for i in range(m): have[c[i] - 1] += 1 flag = True for i in range(n): flag = flag and need[i] <= have[i] if not flag: print("NO") continue if not check[c[-1] - 1]: print("NO") else: print("YES") trash = c[-1] if trash not in d: trash = b.index(trash) + 1 else: trash = d[trash][0] for i in range(m): if i == m - 1: print(trash) break if need[c[i] - 1]: need[c[i] - 1] -= 1 print(d[c[i]][-1], end=" ") d[c[i]].pop() else: print(trash, end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def solve(n, m, a, b, c): weNeedDict = {} for i in range(n): if a[i] != b[i]: if b[i] not in weNeedDict: weNeedDict[b[i]] = [i] else: weNeedDict[b[i]].append(i) avail = {} for i in c: if i not in avail: avail[i] = 1 else: avail[i] += 1 for i in weNeedDict: if i not in avail: return False if avail[i] < len(weNeedDict[i]): return False if c[m - 1] not in set(b): return False if c[m - 1] not in weNeedDict: lastPos = b.index(c[m - 1]) + 1 else: lastPos = weNeedDict[c[m - 1]][0] + 1 answer = [] for i in c: if i in weNeedDict: pos = weNeedDict[i].pop() answer.append(pos + 1) if not weNeedDict[i]: weNeedDict.__delitem__(i) else: answer.append(lastPos) return answer for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) answer = solve(n, m, a, b, c) if answer: print("YES") for i in answer: print(i, end=" ") print() else: print("NO")
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
t = int(input()) for _ in range(t): def main(): n, m = map(int, input().split()) a = [0] + list(map(int, input().split())) b = [0] + list(map(int, input().split())) c = list(map(int, input().split())) need = [[] for _ in range(n + 1)] for i in range(1, n + 1): if a[i] != b[i]: need[b[i]].append(i) if not need[c[-1]]: for i in range(1, n + 1): if b[i] == c[-1]: need[b[i]].append(i) break else: return print("NO") ans = [need[c[-1]][-1]] * m for j in range(m - 1, -1, -1): if need[c[j]]: ans[j] = need[c[j]].pop() for i in need: if i: return print("NO") print("YES") print(" ".join(map(str, ans))) main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) now = list(map(int, input().split())) goal = list(map(int, input().split())) color = list(map(int, input().split())) same = {} dif = {} for i in range(n): if now[i] != goal[i]: if goal[i] not in dif: dif[goal[i]] = [i] else: dif[goal[i]].append(i) elif now[i] not in same: same[now[i]] = i ans = [] count = 0 for paint in color: if paint in dif: k = dif[paint].pop() if len(dif[paint]) == 0: dif.pop(paint) now[k] = paint if paint not in same: same[paint] = k for _ in range(count + 1): ans.append(k + 1) count = 0 elif paint in same: for _ in range(count + 1): ans.append(same[paint] + 1) count = 0 else: count += 1 count += len(dif) if count: print("NO") else: print("YES") ans = map(str, ans) print(" ".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d1 = {} for i in range(n): if a[i] in d1: d1[a[i]].append(i) else: d1[a[i]] = [i] if a == b: ans = [] for i in range(m): if c[i] in d1: val = d1[c[i]][0] + 1 ans.append(val) else: ans.append(-1) if set(ans) == {-1} or ans[-1] == -1: print("NO") else: print("YES") for i in range(m): if ans[i] == -1: print(val, end=" ") else: print(ans[i], end=" ") print() else: p, q = [], [] for i in range(n): if a[i] != b[i]: p.append(a[i]) q.append(b[i]) else: p.append(-1) q.append(-1) d2 = {} for i in range(n): if q[i] in d2: d2[q[i]].append(i) else: d2[q[i]] = [i] ans = [] for i in range(m): if c[i] in d2: val = d2[c[i]][0] + 1 ans.append(val) if len(d2[c[i]]) == 1: del d2[c[i]] else: d2[c[i]].pop(0) if len(d1[a[val - 1]]) == 1: del d1[a[val - 1]] else: d1[a[val - 1]].remove(val - 1) if c[i] in d1: d1[c[i]].append(val - 1) else: d1[c[i]] = [val - 1] a[val - 1] = c[i] elif c[i] in d1: val = d1[c[i]][0] + 1 ans.append(val) else: ans.append(-1) if set(ans) == {-1} or a != b or ans[-1] == -1: print("NO") else: print("YES") for i in range(m): if ans[i] == -1: print(val, end=" ") else: print(ans[i], end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def solveTestCase(): nm = [int(i) for i in input().split()] A = [int(i) for i in input().split()] B = [int(i) for i in input().split()] C = [int(i) for i in input().split()] to_change = {} last_couleur = C[-1] for_last = 0 last_mod = 0 to_mod = 0 for i in range(len(A)): if A[i] != B[i]: to_mod += 1 if B[i] not in to_change: to_change[B[i]] = [] to_change[B[i]].append(i + 1) if B[i] == last_couleur: last_mod = i + 1 if B[i] == last_couleur: for_last = i + 1 if last_mod != 0: for_last = last_mod if for_last == 0: print("NO") return if to_mod > nm[1]: print("NO") return res = [] for i in range(len(C)): if C[i] not in to_change or len(to_change[C[i]]) == 0: res.append(for_last) else: res.append(to_change[C[i]].pop(0)) for k, v in to_change.items(): if len(v) != 0: print("NO") return print("YES") for i in res: print(i, end=" ") print() t = int(input().split()[0]) while t > 0: t -= 1 solveTestCase()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def solve(n, m, a, b, c): d = {} good = {} cnt = {} for i in range(n): if a[i] == b[i]: good[b[i]] = i elif b[i] not in d: d[b[i]] = [i] cnt[b[i]] = 1 else: d[b[i]].append(i) cnt[b[i]] += 1 fallback = -1 p = [] for i in range(m - 1, -1, -1): if c[i] in cnt: if cnt[c[i]]: cnt[c[i]] -= 1 p.append(d[c[i]][cnt[c[i]]] + 1) fallback = p[-1] elif c[i] in good: p.append(good[c[i]] + 1) fallback = p[-1] elif fallback == -1: print("NO") return else: p.append(fallback) for k in cnt: if cnt[k]: print("NO") return print("YES") print(*p[::-1]) def main(): t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) solve(n, m, a, b, c) main()
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n, m = map(int, input().split()) aa = [int(a) for a in input().split()] bb = [int(a) for a in input().split()] cc = [int(a) for a in input().split()] if cc[-1] not in bb: print("NO") return rcm = {} nrcm = {} for i, (a, b) in enumerate(zip(aa, bb)): if b != a: rcm[b] = rcm.get(b, []) + [i] else: nrcm[b] = i res = [] for c in cc[::-1]: if c in rcm and rcm[c] != []: x = rcm[c].pop() + 1 elif c in nrcm: x = nrcm[c] + 1 else: x = res[0] res.append(x) if any(rcm[c] != [] for c in rcm): print("NO") return print("YES") print(" ".join(map(str, res[::-1]))) def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR VAR VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = lambda: sys.stdin.readline().rstrip() T = int(input()) for _ in range(T): N, M = map(int, input().split()) A = [(int(a) - 1) for a in input().split()] B = [(int(a) - 1) for a in input().split()] C = [(int(a) - 1) for a in input().split()][::-1] X = [[] for _ in range(N)] ANS = [] for i in range(N): if A[i] != B[i]: X[B[i]].append(i) if C[0] not in B: print("NO") continue z = X[C[0]][-1] if X[C[0]] else B.index(C[0]) for c in C: if len(X[c]) == 0: ANS.append(z + 1) else: ANS.append(X[c].pop() + 1) for x in X: if x: print("NO") break else: print("YES") print(*ANS[::-1])
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys t = int(input()) while t > 0: n, m = map(int, input().split()) a1 = [[] for i in range(0, n + 1)] a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) for i in range(0, n): if b[i] != a[i]: a1[b[i]].append(i) special_color = c[m - 1] if len(a1[special_color]) == 0: for i in range(0, n): if b[i] == special_color: a1[special_color].append(i) break if len(a1[special_color]) == 0: print("NO") else: ans = list() for i in range(0, m): if len(a1[c[i]]) != 0: ans.append(a1[c[i]][0] + 1) if c[i] != special_color or len(a1[special_color]) > 1 or i == m - 1: a1[c[i]].remove(a1[c[i]][0]) else: ans.append(a1[special_color][0] + 1) check = 0 for i in range(0, n + 1): if len(a1[i]) != 0: check = 1 break if check == 1: print("NO") else: print("YES") for i in ans: print(i, end=" ") print("") t -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
for _ in range(int(input())): n, m = map(int, input().split()) initial = list(map(int, input().split())) final = list(map(int, input().split())) painters = list(map(int, input().split())) last = painters[-1] if last in final: x = 0 to_be_painted = {} for i in range(n): if initial[i] != final[i]: if final[i] in to_be_painted: to_be_painted[final[i]].append(i + 1) else: to_be_painted[final[i]] = [i + 1] if final[i] == last: x = i + 1 if x == 0: x = final.index(last) + 1 ans = [] for p in painters: if p in to_be_painted: ans.append(str(to_be_painted[p].pop(0))) if not to_be_painted[p]: del to_be_painted[p] else: ans.append(str(x)) if not to_be_painted.keys(): print("YES") print(" ".join(ans)) else: print("NO") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def find(): n, m = map(int, input().split()) old = list(map(int, input().split())) new = list(map(int, input().split())) worker = list(map(int, input().split())) task = [[] for _ in range(n + 1)] for i in range(len(old)): if old[i] != new[i]: task[new[i]].append(i) last = -1 if len(task[worker[-1]]) > 0: last = task[worker[-1]].pop() else: for i in range(len(new)): if new[i] == worker[-1]: last = i break if last == -1: return "No" ans = [0] * m ans[-1] = last for i in range(len(worker) - 1): if len(task[worker[i]]) > 0: ans[i] = task[worker[i]].pop() else: ans[i] = last for i in task: if i: return "No" return "Yes\n" + " ".join([str(i + 1) for i in ans]) for i in range(int(input())): print(find())
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR RETURN STRING RETURN BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def main(): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) q = [[] for _ in range(n + 1)] idx = [-1] * (n + 1) for i in range(n): idx[b[i]] = i + 1 if a[i] != b[i]: q[b[i]].append(i + 1) x = [0] * m ok = -1 for j in range(m - 1, -1, -1): if q[c[j]]: x[j] = q[c[j]].pop() ok = x[j] elif ok != -1: x[j] = ok elif idx[c[j]] != -1: x[j] = idx[c[j]] ok = x[j] else: print("NO") return for i in range(n + 1): if q[i]: print("NO") return print("YES") print(*x) return t = int(input()) for i in range(t): main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) finded = {} d = {} z = {} for i in range(n): if b[i] != a[i]: if not b[i] in d: d[b[i]] = 1 z[b[i]] = [i + 1] else: d[b[i]] += 1 z[b[i]].append(i + 1) else: finded[b[i]] = i + 1 m = list(map(int, input().split())) c = [(0) for i in range(len(m))] is_finished = False is_can = True for ind, el in enumerate(reversed(m)): if el in d: d[el] -= 1 c[len(m) - 1 - ind] = z[el][0] if not is_finished: is_finished = True finish_ind = z[el][0] if len(z[el]) > 1: z[el].pop(0) else: if el in finded: c[len(m) - 1 - ind] = finded[el] if not is_finished: is_finished = True finish_ind = finded[el] continue if not is_finished: is_can = False break c[len(m) - 1 - ind] = finish_ind for el in d: if d[el] > 0: is_can = False break if is_can: print("YES") print(" ".join(list(map(str, c)))) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
for t in range(int(input())): n, m = map(int, input().split()) colors = [[] for i in range(n + 1)] pos = 0 a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(n): if a[i] == b[i]: colors[b[i]].append(i) for i in range(n): if a[i] != b[i]: colors[b[i]].append(i) mm = list(map(int, input().split())) if len(colors[mm[-1]]) == 0: print("NO") continue lastpos = colors[mm[-1]][-1] ans = [0] * m for i in range(m - 1, -1, -1): c = mm[i] if len(colors[c]) == 0: ans[i] = lastpos elif len(colors[c]) == 1: ans[i] = colors[c][0] else: ans[i] = colors[c].pop() for i in range(m): a[ans[i]] = mm[i] if a == b: print("YES\n" + " ".join([str(i + 1) for i in ans])) else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
from sys import stdin t = int(stdin.readline()) for _ in range(t): n, m = map(int, stdin.readline().split()) arra = list(map(int, stdin.readline().split())) arrb = list(map(int, stdin.readline().split())) arrc = list(map(int, stdin.readline().split())) dif = {} so = 0 for i in range(n): if arra[i] != arrb[i]: so += 1 if arrb[i] not in dif: dif[arrb[i]] = [i] else: dif[arrb[i]].append(i) ans = [] if arrc[-1] not in arrb: print("no") continue if arrc[-1] in dif: cuoi = dif[arrc[-1]].pop() + 1 so -= 1 else: cuoi = arrb.index(arrc[-1]) + 1 for i in arrc[:-1]: if i not in dif: ans.append(cuoi) elif len(dif[i]) > 0: ans.append(dif[i].pop(0) + 1) so -= 1 else: ans.append(cuoi) ans.append(cuoi) if so > 0: print("no") else: print("yes") print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def solve(): d = dict() pd = dict() o = b.index(c[-1]) for i in range(m): if c[i] in pd: pd[c[i]] += 1 else: pd[c[i]] = 1 for i in range(n): if a[i] != b[i]: if b[i] in d: d[b[i]].append(i) else: d[b[i]] = [i] else: b[i] = 0 for k, v in d.items(): if k in pd: if pd[k] < len(v): return "NO", [] else: return "NO", [] if c[-1] in b: o = b.index(c[-1]) order = [] for i in range(m): if c[i] in d: if len(d[c[i]]) != 0: order.append(d[c[i]][-1] + 1) d[c[i]].pop() else: order.append(o + 1) else: order.append(o + 1) return "YES", order t = int(input()) for z in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) if c[-1] not in b: print("NO") else: p, q = solve() if p == "NO": print("NO") else: print("YES") print(*q)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR RETURN STRING LIST RETURN STRING LIST IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] c = [int(i) for i in input().split()] d = {} d1 = {} for i in range(n): if a[i] != b[i]: if b[i] not in d: d[b[i]] = [] d[b[i]].append(i + 1) d1[b[i]] = i + 1 ans = [] for i in range(k): ans.append(0) for i in range(k): if c[i] in d: ans[i] = d[c[i]][0] d[c[i]].pop(0) if len(d[c[i]]) == 0: d.pop(c[i]) elif c[i] in d1: ans[i] = d1[c[i]] if len(d) != 0: print("NO") else: last = -1 for i in range(k - 1, -1, -1): if ans[i] == 0: if last == -1 and c[i] not in d1: print("NO") break elif last != -1: ans[i] = last else: ans[i] = d1[c[i]] else: last = ans[i] else: print("YES") for i in ans: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) d = {} r = {} for i in range(len(b)): if a[i] == b[i]: r[b[i]] = i elif b[i] in d: d[b[i]].append(i) else: d[b[i]] = [i] e = 0 if c[-1] not in d and c[-1] not in r: sys.stdout.write("NO\n") continue else: if c[-1] in r: e = r[c[-1]] if c[-1] in d: e = d[c[-1]][0] ans = [0] * k for i in range(len(c)): x = c[i] if x not in d: ans[i] = e + 1 elif len(d[x]) == 0: ans[i] = e + 1 else: ans[i] = d[x].pop() + 1 f = 1 for i in d: if len(d[i]) != 0: f = 0 break if f: sys.stdout.write("YES\n") print(*ans) else: sys.stdout.write("NO\n")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
p = lambda: list(map(int, input().split())) r = print g = range l = len for _ in "." * p()[0]: N, M = p() L = N + 1 A = p() B = p() C = p()[::-1] X = [[] for i in g(L)] Y = [[] for i in g(L)] for i in g(N): if A[i] == B[i]: X[A[i]].append(i + 1) else: Y[B[i]].append(i + 1) P = [] for i in g(M): c = C[i] if l(Y[c]): P.append(Y[c].pop()) elif i: P.append(P[0]) elif l(X[c]): P.append(X[c][0]) else: Y[0].append(-1) P.append(0) if sum([l(Y[i]) for i in g(L)]) < 1: r("YES") r(*P[::-1]) else: r("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR BIN_OP STRING FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] x = c[-1] g = -1 d = {} for i in range(n): if b[i] == x: g = i if a[i] != b[i]: if b[i] in d: d[b[i]].append(i) else: d[b[i]] = [i] if g == -1: print("NO") continue if x in d: g = d[x].pop() if d[x] == []: del d[x] ans = [] for i in range(m - 1): if c[i] in d: ans.append(d[c[i]].pop() + 1) if d[c[i]] == []: del d[c[i]] else: ans.append(g + 1) ans.append(g + 1) if d == {}: print("YES") print(*ans) else: print("NO")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR DICT EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input().strip())): n, m = map(int, input().strip().split(" ")) a = list(map(int, input().strip().split(" "))) b = list(map(int, input().strip().split(" "))) c = list(map(int, input().strip().split(" "))) t = {} tp = 0 bd = {} bdc = 0 kmata = [] for i in range(n): if a[i] != b[i]: if b[i] not in bd: bd[b[i]] = [] bd[b[i]].append(i + 1) kmata.append(b[i]) bdc += 1 if b[i] not in t: t[b[i]] = [] t[b[i]].append(i + 1) i = 0 j = 0 out = [] while bdc != 0 and i < m: if c[i] in bd: out.append(bd[c[i]].pop()) if bd[c[i]] == []: del bd[c[i]] bdc -= 1 else: while kmata[j] not in bd: j += 1 out.append(bd[kmata[j]][0]) i += 1 kpos = [] while i < m: if c[i] not in t: tp = 1 kpos.append(len(out)) out.append(0) elif tp > 0: tp -= 1 out.append(t[c[i]][0]) while kpos: out[kpos.pop()] = t[c[i]][0] else: out.append(t[c[i]][0]) i += 1 if tp == 0 and bdc == 0: print("YES") sys.stdout.write(" ".join(map(str, out)) + "\n") else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR LIST VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [int(item) for item in input().split()] b = [int(item) for item in input().split()] c = [int(item) for item in input().split()] color_place = [-1] * n to_recolor = [[] for _ in range(n)] for i, (aa, bb) in enumerate(zip(a, b)): if aa != bb: to_recolor[bb - 1].append(i) color_place[bb - 1] = i ans = [] for i, cc in enumerate(c): if len(to_recolor[cc - 1]) > 0: ans.append(to_recolor[cc - 1].pop() + 1) else: ans.append(None) dump_place = -1 ok = True for i in range(n): if len(to_recolor[i]) > 0: ok = False if not ok: print("NO") continue if ans[-1] == None: if color_place[c[-1] - 1] == -1: print("NO") continue else: dump_place = color_place[c[-1] - 1] + 1 else: dump_place = ans[-1] for i in range(len(c)): if ans[i] == None: ans[i] = dump_place print("YES") print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER NONE IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
def solve(): n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) req = [(0) for i in range(n + 1)] maxreq = [(0) for i in range(n + 1)] available = [(0) for i in range(n + 1)] last = [(0) for i in range(n + 1)] indexes = [[] for i in range(n + 1)] ans = [(0) for i in range(m)] for i in range(n): if a[i] != b[i]: req[b[i]] += 1 indexes[b[i]].append(i + 1) last[b[i]] = i + 1 maxreq[b[i]] += 1 for color in c: available[color] += 1 for i in range(n + 1): if req[i] > available[i]: print("NO") return if maxreq[c[m - 1]] == 0: print("NO") return print("YES") wrong = None if indexes[c[m - 1]]: wrong = indexes[c[m - 1]][0] else: wrong = last[c[m - 1]] for i in range(m): if indexes[c[i]]: ans[i] = indexes[c[i]].pop() else: ans[i] = wrong print(*ans) t = int(input()) while t: solve() t -= 1
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING ASSIGN VAR NONE IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
q = int(input()) while q: q -= 1 f = 1 ri = -1 n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) c.reverse() cntt = 0 cnt = [[] for i in range(n)] res = [] cnt2 = [[] for i in range(n)] for i in range(n): if a[i] != b[i]: cnt[b[i] - 1].append(i) ri = i cntt += 1 cnt2[b[i] - 1].append(i) if ri == -1: if len(cnt2[c[0] - 1]) == 0: print("NO") continue else: print("YES") for i in range(m): print(cnt2[c[0] - 1][-1] + 1, end=" ") print() continue k = 0 for i in range(m): j = c[i] if len(cnt[j - 1]): res.append(cnt[j - 1].pop() + 1) cntt -= 1 k = 1 ri = res[-1] - 1 elif len(cnt2[j - 1]): res.append(cnt2[j - 1][-1] + 1) k = 1 ri = res[-1] - 1 elif k == 0: f = 0 break else: res.append(ri + 1) if f and cntt == 0 and len(res) == m: print("YES") res.reverse() for i in res: print(i, end=" ") print() else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): N, M = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) t = [[] for _ in range(N + 1)] for i in range(N): if a[i] != b[i]: t[b[i]].append(i + 1) res = [0] * M same = [0] * (N + 1) for i in range(M): x = c[i] if len(t[x]): res[i] = t[x].pop() if len(t[x]) == 0: same[x] = res[i] for i in range(N): if a[i] == b[i]: same[a[i]] = i + 1 for i in range(M): if res[i]: continue x = c[i] if same[x]: res[i] = same[x] for i in range(M): if res[i]: continue res[i] = res[-1] if 0 in res: print("NO") continue for i in range(M): x = c[i] j = res[i] - 1 a[j] = x if a == b: print("YES") print(*res) else: print("NO")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def maa(): return map(int, input().split()) t = inp() while t: t -= 1 n, m = maa() a = lis() b = lis() c = lis() ma = {} uma = {} for i in range(n): if a[i] == b[i]: try: ma[a[i]].append(i) except: ma[a[i]] = [i] else: try: uma[b[i]].append(i) except: uma[b[i]] = [i] fl = 0 mo = [] rush = 0 for i in range(m): if c[i] in uma: for k in range(rush + 1): mo.append(uma[c[i]][-1] + 1) rush = 0 fuk = uma[c[i]].pop() try: ma[c[i]].append(fuk) except: ma[c[i]] = [fuk] if len(uma[c[i]]) == 0: del uma[c[i]] continue elif c[i] not in ma: rush += 1 continue if c[i] in ma: if rush: for k in range(rush + 1): mo.append(ma[c[i]][-1] + 1) rush = 0 else: mo.append(ma[c[i]][-1] + 1) if len(uma) or rush: print("NO") else: print("YES") print(*mo)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(map(int, input().split())) dic = {} for i in range(n): if a[i] != b[i]: if b[i] in dic: dic[b[i]].append(i) else: dic[b[i]] = [i] m_dic = {} for i in c: if i in m_dic: m_dic[i] += 1 else: m_dic[i] = 1 flag = 0 for i in dic: if i not in m_dic: flag = 1 print("NO") break if len(dic[i]) > m_dic[i]: flag = 1 print("NO") break if flag == 1: continue if c[-1] not in b: print("NO") continue ans = [] if c[-1] in dic: last_ind = dic[c[-1]][0] else: last_ind = b.index(c[-1]) for i in range(m): if c[i] not in dic: ans.append(last_ind + 1) else: ans.append(dic[c[i]][-1] + 1) if len(dic[c[i]]) > 1: dic[c[i]].pop() print("YES") print(*ans)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
from sys import stdin t = int(stdin.readline()) for _ in range(t): flag = True n, m = map(int, stdin.readline().split()) prev = list(map(int, stdin.readline().split())) aft = list(map(int, stdin.readline().split())) paints = list(map(int, stdin.readline().split())) diff = [] for i in range(n): if prev[i] != aft[i]: diff.append(aft[i]) dic_diff = {} for num in diff: if num not in dic_diff: dic_diff[num] = 1 else: dic_diff[num] += 1 dic_paints = {} for num in paints: if num not in dic_paints: dic_paints[num] = 1 else: dic_paints[num] += 1 for key in dic_diff: if key not in dic_paints or dic_paints[key] < dic_diff[key]: print("NO") flag = False break if not flag: continue elif paints[-1] not in aft: print("NO") else: trash = aft.index(paints[-1]) positions = {} for i in range(n): if aft[i] == prev[i]: continue if aft[i] not in positions: positions[aft[i]] = [i] else: positions[aft[i]].append(i) if paints[-1] in positions: trash = positions[paints[-1]][0] ans = [] for color in paints: if color in dic_diff and positions[color]: ans.append(1 + positions[color][-1]) positions[color].pop(-1) else: ans.append(1 + trash) print("YES") print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
T = int(input()) for _ in range(T): n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] needed = [[] for _ in range(n)] cols_at_end = [(-1) for _ in range(n)] total_needed = 0 for i in range(n): if a[i] != b[i]: needed[b[i] - 1] += [i] total_needed += 1 cols_at_end[b[i] - 1] = i plan = [] fail = False for col in reversed(c): if len(needed[col - 1]) > 0: where = needed[col - 1].pop() plan += [where] total_needed -= 1 elif cols_at_end[col - 1] != -1: plan += [cols_at_end[col - 1]] elif len(plan) == 0: fail = True break else: plan += [plan[-1]] if fail or total_needed > 0: print("NO") else: print("YES") print(" ".join([str(x + 1) for x in reversed(plan)]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER LIST VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR LIST VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR LIST VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR LIST VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
t = int(input()) for _ in range(t): a, b = map(int, input().split()) f = list(map(int, input().split())) s = list(map(int, input().split())) an = list(map(int, input().split())) l = [[] for i in range(a + 1)] sa = 0 bsa = 0 amo = 0 for i in range(a): if s[i] == an[-1] and (sa == 0 or f[i] != s[i]): sa = i + 1 if f[i] != s[i]: l[s[i]].append(i) amo += 1 if sa == 0: print("NO") continue ans = [] for i in range(1, b + 1): if len(l[an[-i]]) > 0: ans.append(l[an[-i]][-1] + 1) l[an[-i]].pop() amo -= 1 else: ans.append(sa) if amo != 0: print("NO") else: ans.reverse() print("YES") for i in ans: print(i, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence — so plain and boring, that you'd like to repaint it. You have a fence consisting of $n$ planks, where the $i$-th plank has the color $a_i$. You want to repaint the fence in such a way that the $i$-th plank has the color $b_i$. You've invited $m$ painters for this purpose. The $j$-th painter will arrive at the moment $j$ and will recolor exactly one plank to color $c_j$. For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank. Can you get the coloring $b$ you want? If it's possible, print for each painter which plank he must paint. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of planks in the fence and the number of painters. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the initial colors of the fence. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le n$) — the desired colors of the fence. The fourth line of each test case contains $m$ integers $c_1, c_2, \dots, c_m$ ($1 \le c_j \le n$) — the colors painters have. It's guaranteed that the sum of $n$ doesn't exceed $10^5$ and the sum of $m$ doesn't exceed $10^5$ over all test cases. -----Output----- For each test case, output "NO" if it is impossible to achieve the coloring $b$. Otherwise, print "YES" and $m$ integers $x_1, x_2, \dots, x_m$, where $x_j$ is the index of plank the $j$-th painter should paint. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" are all recognized as positive answer). -----Examples----- Input 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 Output YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO -----Note----- None
import sys from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n, m = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) c = list(map(int, stdin.readline().split())) dic = [[] for i in range(n + 1)] for i in range(n): if a[i] != b[i]: dic[b[i]].append(i) end = [None] * n ans = [0] * m if c[-1] not in b: print("NO") continue for i in range(m - 1, -1, -1): if i == m - 1: if len(dic[c[i]]) > 0: ableind = dic[c[i]][-1] ans[i] = dic[c[i]][-1] del dic[c[i]][-1] else: for j in range(n): if b[j] == c[i]: ableind = j ans[i] = j elif len(dic[c[i]]) > 0: ans[i] = dic[c[i]][-1] del dic[c[i]][-1] else: ans[i] = ableind fl = True for i in range(n + 1): if len(dic[i]) > 0: fl = False if fl: print("YES") for i in range(m): ans[i] += 1 print(*ans) else: print("NO")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9). -----Output----- In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. -----Examples----- Input 5 1 2 3 4 5 Output 2 4 5 Input 3 1 2 4 Output 1 4
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n = nmbr() a = lst() bits = [[(0) for _ in range(34)] for _ in range(n)] for i in range(n): for j in range(34): bits[i][33 - j] = a[i] >> j & 1 bit = -1 x = (1 << 32) - 1 for bp in range(34): nd = x for i in range(n): if bits[i][33 - bp]: nd &= a[i] if nd % (1 << bp) == 0: bit = bp if bit == -1: print(-1) else: ans = [] for i in range(n): if bits[i][33 - bit]: ans += [a[i]] print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9). -----Output----- In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. -----Examples----- Input 5 1 2 3 4 5 Output 2 4 5 Input 3 1 2 4 Output 1 4
n = int(input()) t = list(map(int, input().split())) p = [bin(i) for i in t] p = [("0" * (32 - len(i)) + i[2:]) for i in p] p = ["".join(i) for i in zip(*p)] x = 0 for i in range(30): x = p[i] if "1" in x and not any( all(x[k] == y[k] for k in range(n) if x[k] == "1") for y in p[i + 1 :] ): break t = [str(t[k]) for k in range(n) if x[k] == "1"] print(len(t)) print(" ".join(t))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR IF STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9). -----Output----- In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. -----Examples----- Input 5 1 2 3 4 5 Output 2 4 5 Input 3 1 2 4 Output 1 4
x = int(input()) s = list(map(int, input().split())) ans = [] for u in range(0, 30): cur = 1 << u v = (1 << u + 1) - 1 tem = [] for n in s: if n & cur: tem.append(n) for n in tem: v &= n if v % (1 << u) == 0: ans = tem print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9). -----Output----- In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. -----Examples----- Input 5 1 2 3 4 5 Output 2 4 5 Input 3 1 2 4 Output 1 4
n = int(input()) a = list(map(int, input().split())) mxa = max(a) v = 1 << 30 while v > mxa: v >>= 1 while True: d = -1 for i in range(n): if a[i] & v: d &= a[i] if d % v == 0: break v >>= 1 b = [i for i in a if i & v] print(len(b)) print(" ".join(map(str, b)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules. Input The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format. Output Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them. Examples Input 4 1 2 3 4 4 1 3 2 4 3 1 2 3 4 2 1 3 1 2 4 Output 2 1 1 4 Note In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
def f(): t = [0] * n for i, j in enumerate(input().split()): t[int(j) - 1] = i return t n = int(input()) p = [f() for i in range(n)] s = f() for x, t in enumerate(p): i = j = x < 1 for y in range(n): if x != y and s[y] < s[i]: i = y if t[i] < t[j]: j = i print(j + 1)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules. Input The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format. Output Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them. Examples Input 4 1 2 3 4 4 1 3 2 4 3 1 2 3 4 2 1 3 1 2 4 Output 2 1 1 4 Note In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
n = int(input()) arr = [list(map(int, input().split())) for _ in range(n + 1)] res = [0] * n for i in range(n): p = [0] * (n + 1) for j in range(n): p[arr[i][j]] = j u, t, b = 0, int(100000.0), int(100000.0) for x in arr[n]: if x != i + 1 and x < b: if p[x] < t: u, t = x, p[x] b = x res[i] = u print(*res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules. Input The first line contains an integer n (2 ≤ n ≤ 300) — the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format. Output Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them. Examples Input 4 1 2 3 4 4 1 3 2 4 3 1 2 3 4 2 1 3 1 2 4 Output 2 1 1 4 Note In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
n = int(input()) frnd = [list(map(int, input().split())) for _ in range(n)] ara = list(map(int, input().split())) pref = [0] * (n + 1) ans = [0] * n for i in range(n): pref[ara[i]] = i def ok(val, row): for x in ara: if x - 1 == row: continue if x < val: return 0 if x == val: return 1 for ro in range(n): for x in frnd[ro]: if x - 1 == ro: continue if ok(x, ro): ans[ro] = x break for i in range(n): print(ans[i], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF FOR VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = map(int, input().split()) p = list(map(int, input().split())) p.sort() t = [[i] for i in p] for i in range(1, n): t += [(t[-1] + i) for i in t[: n - i]] print("\n".join(str(len(i)) + " " + " ".join(map(str, i)) for i in t[:k]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort(reverse=True) cur_len, res = 1, [] while k > 0 and cur_len <= n: for i in range(cur_len, n + 1): print(cur_len, *res, a[i - 1]) k -= 1 if k == 0: break res.append(a[cur_len - 1]) cur_len += 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER LIST WHILE VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = list(map(int, input().split())) A = list(map(int, input().split())) C = [] U = len(A) c = 0 g = 0 for i in range(k): print(len(C + [A[g]]), " ".join(map(str, C + [A[g]]))) g += 1 if g == len(A): g = 0 C.append(max(A)) A.remove(max(A))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = map(int, input().split()) l = [int(i) for i in input().split()] l.sort(reverse=True) elems = [] j = -1 cnt = 0 for i in range(n): if cnt == k: break j += 1 fixed = l[0:j] for z in range(j, n): if cnt == k: continue print(j + 1, *(fixed + [l[z]])) cnt += 1 if cnt == k: break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR NUMBER IF VAR VAR
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) j = 0 c = 0 for i in range(k): b = [a[i - c]] for l in range(n - j, n): b.append(a[l]) print(j + 1, *b) if i - c == n - j - 1: c += n - j j += 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = map(int, input().split()) soldiers = list(map(int, input().split())) count = 0 seen = {(0): 0} beauties = [0] while count != k: for beauty in beauties: bits = seen[beauty] for i, x in enumerate(soldiers): if bits & 1 << i != 0 or beauty + x in seen: continue new_bits = bits | 1 << i seen[beauty + x] = new_bits beauties.append(beauty + x) group = [] for j, y in enumerate(soldiers): if new_bits & 1 << j != 0: group.append(y) print(" ".join(map(str, [len(group)] + group))) count += 1 if count == k: break if count == k: break
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP LIST FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() cnt = n while k: for i in range(cnt): print(n - cnt + 1, a[i]) for j in range(cnt, n): print(a[j]) k -= 1 if k == 0: break cnt -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
from sys import stdin def arr_inp(n): if n == 1: return [int(x) for x in stdin.readline().split()] elif n == 2: return [float(x) for x in stdin.readline().split()] else: return list(stdin.readline()[:-1]) n, k = arr_inp(1) c = sorted(arr_inp(1))[::-1] for i in range(min(n, k)): print(1, c[i]) k -= 1 tem = [] for i in range(min(n - 1, k)): tem.append(c[i]) for j in range(n - 1, i, -1): print(i + 2, end=" ") print(*(tem + [c[j]])) k -= 1 if not k: return
FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR VAR NUMBER IF VAR RETURN
General Payne has a battalion of n soldiers. The soldiers' beauty contest is coming up, it will last for k days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value a_{i} represents the beauty of the i-th soldier. On each of k days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of k days the beauty of the sent detachment should be unique. In other words, all k beauties of the sent detachments must be distinct numbers. Help Payne choose k detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. -----Input----- The first line contains two integers n, k (1 ≤ n ≤ 50; 1 ≤ k ≤ $\frac{n(n + 1)}{2}$) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^7) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. -----Output----- Print k lines: in the i-th line print the description of the detachment that will participate in the pageant on the i-th day. The description consists of integer c_{i} (1 ≤ c_{i} ≤ n) — the number of soldiers in the detachment on the i-th day of the pageant and c_{i} distinct integers p_{1, }i, p_{2, }i, ..., p_{c}_{i}, i — the beauties of the soldiers in the detachment on the i-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them. -----Examples----- Input 3 3 1 2 3 Output 1 1 1 2 2 3 2 Input 2 1 7 12 Output 1 12
n, k = map(int, input().split()) l = sorted(map(int, input().split()), reverse=True) c = 0 for i in range(n): for x in l[i:]: if c == k: exit() c += 1 print(i + 1, end=" ") for y in l[:i]: print(y, end=" ") print(x, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR