description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $x$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $x$ consecutive (successive) days visiting Coronavirus-chan. They use a very unusual calendar in Naha: there are $n$ months in a year, $i$-th month lasts exactly $d_i$ days. Days in the $i$-th month are numbered from $1$ to $d_i$. There are no leap years in Naha. The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $j$ hugs if you visit Coronavirus-chan on the $j$-th day of the month. You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. -----Input----- The first line of input contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $n$ integers $d_1, d_2, \ldots, d_n$, $d_i$ is the number of days in the $i$-th month ($1 \le d_i \le 10^6$). It is guaranteed that $1 \le x \le d_1 + d_2 + \ldots + d_n$. -----Output----- Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. -----Examples----- Input 3 2 1 3 1 Output 5 Input 3 6 3 3 3 Output 12 Input 5 6 4 2 3 1 3 Output 15 -----Note----- In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $\{1,1,2,3,1\}$. Coronavirus-chan will hug you the most if you come on the third day of the year: $2+3=5$ hugs. In the second test case, the numbers of the days are $\{1,2,3,1,2,3,1,2,3\}$. You will get the most hugs if you arrive on the third day of the year: $3+1+2+3+1+2=12$ hugs. In the third test case, the numbers of the days are $\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $2+3+1+2+3+4=15$ times.
n, x = map(int, input().split()) d = list(map(int, input().split())) for i in range(n): d.append(d[i]) s = 0 s_hug = 0 st = 0 sum_day = [0] month_hug = [0] for i in range(len(d)): s += d[i] hug = (d[i] + 1) * d[i] // 2 s_hug += hug month_hug.append(s_hug) sum_day.append(s) if st == 0 and s >= x: st = i + 1 le = 0 maxx = 0 for i in range(st, len(sum_day)): while sum_day[i] - sum_day[le + 1] >= x: le += 1 hug = month_hug[i] - month_hug[le] day = sum_day[i] - sum_day[le] hug -= (day - x) * (day - x + 1) // 2 maxx = max(maxx, hug) print(maxx)
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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a permutation $a$ of length $n$. Recall that permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. You have a strength of $s$ and perform $n$ moves on the permutation $a$. The $i$-th move consists of the following: Pick two integers $x$ and $y$ such that $i \leq x \leq y \leq \min(i+s,n)$, and swap the positions of the integers $x$ and $y$ in the permutation $a$. Note that you can select $x=y$ in the operation, in which case no swap will occur. You want to turn $a$ into another permutation $b$ after $n$ moves. However, some elements of $b$ are missing and are replaced with $-1$ instead. Count the number of ways to replace each $-1$ in $b$ with some integer from $1$ to $n$ so that $b$ is a permutation and it is possible to turn $a$ into $b$ with a strength of $s$. Since the answer can be large, output it modulo $998\,244\,353$. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $s$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s \leq n$) — the size of the permutation and your strength, respectively. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of $a$. All elements of $a$ are distinct. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$) — the elements of $b$. All elements of $b$ that are not equal to $-1$ are distinct. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer — the number of ways to fill up the permutation $b$ so that it is possible to turn $a$ into $b$ using a strength of $s$, modulo $998\,244\,353$. -----Examples----- Input 6 3 1 2 1 3 3 -1 -1 3 2 2 1 3 3 -1 -1 4 1 1 4 3 2 4 3 1 2 6 4 4 2 6 3 1 5 6 1 5 -1 3 -1 7 4 1 3 6 2 7 4 5 2 5 -1 -1 -1 4 -1 14 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Output 1 2 0 2 12 331032489 -----Note----- In the first test case, $a=[2,1,3]$. There are two possible ways to fill out the $-1$s in $b$ to make it a permutation: $[3,1,2]$ or $[3,2,1]$. We can make $a$ into $[3,1,2]$ with a strength of $1$ as follows: $$[2,1,3] \xrightarrow[x=1,\,y=1]{} [2,1,3] \xrightarrow[x=2,\,y=3]{} [3,1,2] \xrightarrow[x=3,\,y=3]{} [3,1,2].$$ It can be proven that it is impossible to make $[2,1,3]$ into $[3,2,1]$ with a strength of $1$. Thus only one permutation $b$ satisfies the constraints, so the answer is $1$. In the second test case, $a$ and $b$ the same as the previous test case, but we now have a strength of $2$. We can make $a$ into $[3,2,1]$ with a strength of $2$ as follows: $$[2,1,3] \xrightarrow[x=1,\,y=3]{} [2,3,1] \xrightarrow[x=2,\,y=3]{} [3,2,1] \xrightarrow[x=3,\,y=3]{} [3,2,1].$$ We can still make $a$ into $[3,1,2]$ using a strength of $1$ as shown in the previous test case, so the answer is $2$. In the third test case, there is only one permutation $b$. It can be shown that it is impossible to turn $a$ into $b$, so the answer is $0$.
t = int(input()) for _ in range(t): n, s = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) visited = [0] * (n + 1) m = 1 empty = [] for a_i, b_i in zip(a, b): if b_i != -1 and a_i - b_i > s: m = 0 break elif b_i == -1: empty.append(a_i) else: visited[b_i] += 1 else: missing = [i for i in range(1, n + 1) if visited[i] == 0] empty.sort() l_max = len(missing) e_hi = 0 for e_lo, m_i in enumerate(missing): while e_hi != l_max and m_i + s >= empty[e_hi]: e_hi += 1 m = m * max(0, e_hi - e_lo) % 998244353 print(m)
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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a permutation $a$ of length $n$. Recall that permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. You have a strength of $s$ and perform $n$ moves on the permutation $a$. The $i$-th move consists of the following: Pick two integers $x$ and $y$ such that $i \leq x \leq y \leq \min(i+s,n)$, and swap the positions of the integers $x$ and $y$ in the permutation $a$. Note that you can select $x=y$ in the operation, in which case no swap will occur. You want to turn $a$ into another permutation $b$ after $n$ moves. However, some elements of $b$ are missing and are replaced with $-1$ instead. Count the number of ways to replace each $-1$ in $b$ with some integer from $1$ to $n$ so that $b$ is a permutation and it is possible to turn $a$ into $b$ with a strength of $s$. Since the answer can be large, output it modulo $998\,244\,353$. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $s$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s \leq n$) — the size of the permutation and your strength, respectively. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the elements of $a$. All elements of $a$ are distinct. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$ or $b_i = -1$) — the elements of $b$. All elements of $b$ that are not equal to $-1$ are distinct. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single integer — the number of ways to fill up the permutation $b$ so that it is possible to turn $a$ into $b$ using a strength of $s$, modulo $998\,244\,353$. -----Examples----- Input 6 3 1 2 1 3 3 -1 -1 3 2 2 1 3 3 -1 -1 4 1 1 4 3 2 4 3 1 2 6 4 4 2 6 3 1 5 6 1 5 -1 3 -1 7 4 1 3 6 2 7 4 5 2 5 -1 -1 -1 4 -1 14 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 Output 1 2 0 2 12 331032489 -----Note----- In the first test case, $a=[2,1,3]$. There are two possible ways to fill out the $-1$s in $b$ to make it a permutation: $[3,1,2]$ or $[3,2,1]$. We can make $a$ into $[3,1,2]$ with a strength of $1$ as follows: $$[2,1,3] \xrightarrow[x=1,\,y=1]{} [2,1,3] \xrightarrow[x=2,\,y=3]{} [3,1,2] \xrightarrow[x=3,\,y=3]{} [3,1,2].$$ It can be proven that it is impossible to make $[2,1,3]$ into $[3,2,1]$ with a strength of $1$. Thus only one permutation $b$ satisfies the constraints, so the answer is $1$. In the second test case, $a$ and $b$ the same as the previous test case, but we now have a strength of $2$. We can make $a$ into $[3,2,1]$ with a strength of $2$ as follows: $$[2,1,3] \xrightarrow[x=1,\,y=3]{} [2,3,1] \xrightarrow[x=2,\,y=3]{} [3,2,1] \xrightarrow[x=3,\,y=3]{} [3,2,1].$$ We can still make $a$ into $[3,1,2]$ using a strength of $1$ as shown in the previous test case, so the answer is $2$. In the third test case, there is only one permutation $b$. It can be shown that it is impossible to turn $a$ into $b$, so the answer is $0$.
def convert(nums_a, nums_b, s): zips = list([(a, b) for a, b in zip(nums_a, nums_b)]) perm = [x[1] for x in sorted(zips)] free_nums = set(nums_a) - set(perm) available_nums = set(nums_a) count_ar = [0] * len(perm) count = 1 for i, v in enumerate(perm): rm_num = i + 1 - s - 1 if rm_num in free_nums: free_nums.remove(rm_num) if rm_num in available_nums: available_nums.remove(rm_num) count_ar[i] = len(free_nums) rm_count = 0 for i, v in reversed(list(enumerate(perm))): if perm[i] == -1: count *= count_ar[i] - rm_count count = count % 998244353 rm_count += 1 elif i - perm[i] >= s: count = 0 print(count) t = int(input()) for _ in range(t): _, s = list(map(int, input().split())) nums_a = list(map(int, input().split())) nums_b = list(map(int, input().split())) convert(nums_a, nums_b, s)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
for t in range(int(input())): n = int(input()) l = list(map(int, input().split())) l.sort() m = [] mi = [] for k in range(1, n - 1): i = k - 1 j = k + 1 x = l[i] - l[k] y = l[j] - l[k] s = x + y m.append(abs(s)) while i > 0 or j < n - 1: if s > 0 and i > 0: i = i - 1 s = s + l[i] - l[i + 1] m.append(abs(s)) elif s < 0 and j < n - 1: j = j + 1 s = s + l[j] - l[j - 1] m.append(abs(s)) else: break mi.append(min(m)) m = [] print(min(mi))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
def min_beauty(a): min_mean = float("inf") a.sort() n = len(a) res = float("inf") for i in range(1, n - 1): l, r = 0, n - 1 while l < i and i < r: sum = a[l] + a[r] if sum == 2 * a[i]: return 0 if sum > 2 * a[i]: r -= 1 else: l += 1 min_mean = min(min_mean, abs(sum - 2 * a[i])) res = min(res, min_mean) return res for z in range(int(input())): n = int(input()) l = list(map(int, input().split())) print(min_beauty(l))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
n = int(input()) while n: x = int(input()) lis = list(map(int, input().split())) lis.sort() ans = 10000000000.0 for b in range(1, x - 1): a = b - 1 c = b + 1 while a >= 0 and c <= x - 1: ans = min(ans, abs(lis[a] + lis[c] - 2 * lis[b])) if lis[c] - lis[b] > lis[b] - lis[a]: a -= 1 else: c += 1 if ans == 0: break print(ans) n -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
def T(A, N, k): i = 0 j = N - 1 min1 = abs(A[i] + A[j] - 2 * A[k]) while i < k and k < j: if A[i] + A[j] - 2 * A[k] == 0: return 0 elif A[i] + A[j] > 2 * A[k]: min1 = min(min1, A[i] + A[j] - 2 * A[k]) j -= 1 else: min1 = min(min1, 2 * A[k] - A[i] - A[j]) i += 1 return min1 a = int(input()) for i in range(a): N = int(input()) A = list(map(int, input().split())) A.sort() ans1 = A[2] - A[0] ans2 = A[-1] - A[-3] ans = T(A, N, 1) if ans == 0: print(ans) else: for k in range(1, N - 1): ans = min(ans, T(A, N, k)) if ans == 0: break if A[0] == A[1]: for k in range(2, N): ans1 = min(ans1, A[k] - A[0]) ans = min(ans, ans1) if A[-1] == A[-2]: for k in range(0, N - 2): ans2 = min(ans2, A[-1] - A[k]) ans = min(ans, ans2) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR WHILE VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
t = int(input()) for i in range(t): n = int(input()) A = sorted(list(map(int, input().split()))) i = 1 main = 10000000000 while i < n - 1: j = i - 1 k = i + 1 while j >= 0 and k < n: if main > abs(A[j] + A[k] - 2 * A[i]): main = abs(A[j] + A[k] - 2 * A[i]) if A[j] + A[k] >= 2 * A[i]: j = j - 1 else: k = k + 1 if main == 0: break i = i + 1 print(main)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) A.sort() res = float("inf") for i in range(1, n - 1): x = A[i] L = [] for j in range(i - 1, -1, -1): L.append(x - A[j]) k = 0 for j in range(i + 1, n): v = A[j] - x while k < len(L) and v >= L[k]: k += 1 if k < len(L): res = min(res, abs(v - L[k])) if k > 0: res = min(res, abs(v - L[k - 1])) if res == 0: break if res == 0: break print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
def beauty(l1, l2, a): l1.sort(reverse=True) l2.sort() i = 0 j = 0 ans = 10000000000000 while i < len(l1) and j < len(l2): ans = min(ans, abs(a - l1[i] - l2[j])) if l1[i] + l2[j] > a: i += 1 else: j += 1 return ans for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() b = 10000000001 for i in range(1, n - 1): if beauty(a[:i], a[i + 1 :], a[i] * 2) < b: b = beauty(a[:i], a[i + 1 :], a[i] * 2) print(b)
FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() mini = 10000000000.0 for i in range(1, n - 1): a = 0 b = 2 * arr[i] c = n - 1 while a < i and c > i: new = arr[a] + arr[c] - b if new < 0: a += 1 else: c -= 1 mini = min(mini, abs(new)) if mini == 0: break else: continue break print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Chefina has an array A consisting of N non-negative integers. The *beauty* of a subsequence S is defined as 3 \cdot \texttt{abs(means(S) - median(S))}, where \texttt{mean(S)} denotes the [mean] of subsequence S and \texttt{median(S)} denotes the [median] of subsequence S. Now, Chefina asks Chef to choose a subsequence S of size 3 from array A such that the *beauty* of S is minimised. Your task is to find the minimum *beauty* possible. As a reminder, a subsequence of an array is obtained by deleting some elements from the array without changing the order of the remaining elements. Note: It can be proven that the answer is always an integer. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of multiple lines of input. - The first line of each test case contains an integer N — the number of elements in the array. - The second line contains N space-separated integers, the elements of the array A. ------ Output Format ------ For each test case, output on a new line, the minimum *beauty* possible of any subsequence of array A, having length 3. ------ Constraints ------ $1 ≤ T ≤ 500$ $3 ≤ N ≤ 5\cdot 10^{3}$ $0 ≤ A_{i} ≤ 10^{9} $ - Sum of $N$ over all test cases does not exceed $5\cdot 10^{3}$. ------ subtasks ------ Subtask 1 (10 points): $1 ≤ M ≤ 10$ Subtask 2 (20 points): The sum of $N$ across all test cases won't exceed $20$. Subtask 3 (70 points): No further constraints. ----- Sample Input 1 ------ 2 4 1 6 8 0 10 5 5 7 0 1 0 4 7 9 4 ----- Sample Output 1 ------ 3 0 ----- explanation 1 ------ Test case $1$: For the given array $[1, 6, 8, 0]$, Chef will choose the subsequence $[1, 6, 8]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 6 + 8)}{3} = 5$ - The median of subsequence will be $6$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(5-6)}$ $= 3$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$. Test case $2$: For the given array $[5, 5, 7, 0, 1, 0, 4, 7, 9, 4]$, Chef will choose subsequence $[1, 4, 7]$ for minimum beauty. - The mean of subsequence will be $\frac{(1 + 4 + 7)}{3} = 4$ - The median of subsequence will be $4$ (as it is the middle element after sorting the subsequence). - The beauty of the subsequence is $3\cdot$ $\texttt{abs(4-4)}$ $= 0$. It can be shown that this is the minimum beauty that can be obtained using any subsequence of size $3$.
from sys import stdin def solve(): n = int(stdin.readline()) arr = sorted(map(int, stdin.readline().split())) best = 10**10 for left in range(n): i = left + 1 for right in range(left + 2, n): s = arr[left] + arr[right] x = arr[i] while 2 * x < s: i += 1 x = arr[i] if i == left + 1: score = abs(s - 2 * x) elif i == right: score = abs(s - 2 * arr[i - 1]) else: s1 = abs(s - 2 * x) s2 = abs(s - 2 * arr[i - 1]) if s1 < s2: score = s1 else: score = s2 if score < best: best = score print(best) def main(): for _ in range(int(stdin.readline())): solve() main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) if k >= min(n, m): print(-1) else: result = -1 for i in range(k + 1): num = A[i] + ta left = -1 right = m while left + 1 != right: mid = (left + right) // 2 if B[mid] >= num: right = mid else: left = mid end = right + k - i if end < m: result = max(result, B[end] + tb) else: print(-1) break else: print(result)
ASSIGN VAR VAR VAR 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 IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def dostuff(n, m, ta, tb, k): a = list(map(int, input().split())) b = list(map(int, input().split())) bs, ans = 0, 0 for ai in range(k + 1): try: while b[bs] < a[ai] + ta: bs += 1 except IndexError: print(-1) return None bi = k - ai try: if ans < b[bs + bi] + tb: ans = b[bs + bi] + tb except IndexError: print(-1) return None print(ans) n, m, ta, tb, k = map(int, input().split()) dostuff(n, m, ta, tb, k)
FUNC_DEF 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN NONE ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def ii(): return int(input()) def fi(): return float(input()) def si(): return input() def mi(): return map(int, input().split()) def li(): return list(mi()) n, m, ta, tb, k = mi() a = li() b = li() ans = -1 j = 0 if k < len(a): for i in range(k + 1): if i < len(a): t = a[i] + ta while j < len(b) and b[j] < t: j += 1 tg = k - i jj = j + tg if jj < len(b): ans = max(ans, b[jj] + tb) else: ans = -1 break print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) gone = 0 max_t = -1 if k >= n: print(-1) else: for i in range(k + 1): flight_a = a[i] while gone < m and b[gone] < a[i] + ta: gone += 1 j = gone + k - i if j >= m: max_t = -1 break flight_b = b[j] max_t = max(max_t, flight_b + tb) print(max_t)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def go(): n, m, ta, tb, k = [int(i) for i in input().split(" ")] a = [(int(i) + ta) for i in input().split(" ")] b = [int(i) for i in input().split(" ")] j = 0 answer = 0 if k >= n or k >= m: return -1 for i in range(k + 1): x = k - i while j < m and b[j] < a[i]: j += 1 if j + x >= m: return -1 answer = max(answer, b[j + x] + tb) return answer print(go())
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, x, y, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(n): a[i] += x i = 0 j = 0 r = -1 while i < n and j < m: if b[j] >= a[i]: if k > 0: if k == m - j: break else: k += -1 i += 1 else: r = b[j] + y break j += 1 print(r)
ASSIGN VAR VAR VAR 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) land = a[0] + ta ind = 0 while b[ind] < land and ind < m - 1: ind += 1 if ind + k >= m: print(-1) elif k >= n: print(-1) elif a[k] + ta > b[-1]: print(-1) else: froma = k best = 0 land = a[k] + ta while b[ind] < land: ind += 1 for i in range(k + 1): best = max(best, b[ind + k - froma] + tb) froma -= 1 land = a[froma] + ta while b[ind - 1] >= land and ind > 0: ind -= 1 print(best)
ASSIGN VAR VAR VAR 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 BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
import sys def LI(): return list(map(int, sys.stdin.readline().split())) def main(): n, m, ta, tb, k = LI() A = LI() B = LI() if k >= n or k >= m: return -1 maxt = 0 j = 0 for i in range(k + 1): c = A[i] + ta while j < m and c > B[j]: j += 1 if j + k - i >= m: return -1 maxt = max(B[j + k - i] + tb, maxt) return maxt print(main())
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
from sys import stdin def input(): return stdin.readline().strip() n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(n): a[i] += ta ca = 0 cb = 0 c = [-1] * n i = 0 j = 0 while i < n and j < m: while (i < n and j < m) and a[i] > b[j]: j += 1 if j < m: c[i] = j i += 1 ans = 0 for i in range(min(k + 1, n)): if c[i] == -1: ans = m break if c[i] + k - i > ans: ans = c[i] + k - i if ans >= m or k >= min(n, m): print(-1) else: print(b[ans] + tb)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR 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 FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def bs(v: int): global n, m, ta, tb, k, a, b l, r = -1, m - 1 while r - l > 1: mid = (l + r) // 2 if b[mid] >= v: r = mid else: l = mid return r n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 if k >= n or k >= m: print(-1) exit(0) for cur in range(k + 1): pos = bs(a[cur] + ta) if pos + (k - cur) >= m: print(-1) exit() if b[pos + k - cur] < a[cur] + ta: print(-1) exit() ans = max(ans, b[pos + (k - cur)] + tb) print(ans)
FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR 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 NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
import sys input = sys.stdin.readline n, mm, ta, tb, k = map(int, input().split()) def bsl(a, val): l = 0 r = len(a) - 1 ans = len(a) while l <= r: m = l + (r - l) // 2 if a[m] == val: r = m - 1 ans = m elif a[m] > val: ans = m r = m - 1 else: l = m + 1 return ans a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] maxi = -99 if k >= n: print(-1) exit(0) for i in range(k + 1): j = bsl(b, a[i] + ta) if j + k - i >= mm: print(-1) exit(0) else: maxi = max(maxi, b[j + k - i] + tb) print(maxi)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] res = -1 j = 0 if k < len(A): for i in range(k + 1): if i < len(A): t = A[i] + ta while j < len(B) and B[j] < t: j += 1 togo = k - i jj = j + togo if jj < len(B): res = max(res, B[jj] + tb) else: res = -1 break print(res)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) a = [(i + ta) for i in a] i = 0 j = 0 ans = 0 for _k in range(k + 1): dk = _k i = 0 if dk >= n: print(-1) return else: key = a[dk] dk = k - _k while j < m and b[j] < key: j += 1 if j is m: print(-1) return elif m <= dk + j: print(-1) return else: ans = max(ans, b[j + dk] + tb) print(ans)
ASSIGN VAR VAR VAR 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 BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) ar = [(int(x) + ta) for x in input().split()] arr = [int(x) for x in input().split()] r = [m] * n aptr = 0 bptr = 0 while aptr < n and bptr < m: if arr[bptr] >= ar[aptr]: r[aptr] = bptr aptr += 1 else: bptr += 1 if k >= n: print(-1) else: ans = 0 for p in range(0, k + 1): if r[p] + k - p >= m: ans = -1 break else: ans = max(arr[r[p] + k - p] + tb, ans) print(ans)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR 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 VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, a, b, k = [int(x) for x in input().split()] a1 = [(int(x) + a, 1) for x in input().split()] b1 = [(int(x), 2) for x in input().split()] c = sorted(a1[:] + b1[:]) counter = count1 = count2 = 0 for item in c: if item[1] == 1: count1 += 1 else: count2 += 1 if item[1] == 2: if count1 > k and count2 > k and counter == k: print(item[0] + b) break else: counter = min(counter + 1, count1, count2) else: print(-1)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def bbin(key, b): l = -1 r = len(b) while r > l + 1: mm = (l + r) // 2 if b[mm] >= key: r = mm else: l = mm return r n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans = 0 for i in range(k + 1): if i >= n: ans = -1 break key = a[i] + ta rr = bbin(key, b) + k - i if rr >= m: ans = -1 break ans = max(ans, b[rr] + tb) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) fa = list(map(int, input().split())) fb = list(map(int, input().split())) flighta = 0 flightb = 0 for i in range(n): fa[i] += ta ans = 0 while k >= 0: if flighta == n or flightb == m: print(-1) quit() if fb[flightb] < fa[flighta]: flightb += 1 continue if k == 0: ans = max(ans, fb[flightb]) break if flightb + k >= m: print(-1) quit() ans = max(fb[flightb + k], ans) flighta += 1 k -= 1 print(ans + tb)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
INF = 100000000 t = 1 for test in range(t): n, m, ta, tb, k = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in range(n): a[i] = a[i] + ta ans = 0 ind = 0 if k >= n or k >= m: print(-1) continue for i in range(k + 1): lo = a[i] while ind < m and b[ind] < lo: ind += 1 if ind == m: ans = -1 break else: if ind + k - i >= m: ans = -1 break ans = max(ans, b[ind + (k - i)] + tb) print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
m, n, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if m <= k: print(-1) else: ans = 0 count = 0 bef = 0 for i in range(m): now = a[i] for j in range(bef, n): if b[j] < a[i] + ta: if j == n - 1: j += 1 pass else: break if count > k: break if j == n: ans = 0 break if k - count < n - j: ans = max(ans, b[j + k - count] + tb) else: ans = 0 break bef = j count += 1 if ans == 0: print(-1) else: print(ans)
ASSIGN VAR VAR VAR 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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(lambda x: int(x), input().split()) aller = input().split() retour = input().split() for i in range(n): aller[i] = int(aller[i]) + ta for i in range(m): retour[i] = int(retour[i]) l = 0 h = 0 maxx = 0 s = k if n <= k or m <= k: print(-1) else: for i in range(s + 1): h = 0 for j in range(l, m): if retour[j] < aller[i]: h += 1 else: break l += h k += h try: maxx = max(maxx, retour[k - i]) ch = maxx + tb except: ch = -1 break print(ch)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) b = sorted(map(lambda x: int(x) + ta, input().split())) c = sorted(map(int, input().split())) i = j = 0 while k > 0 and i < n and j < m: while j < m and c[j] < b[i]: j += 1 if i < n and j < m: k -= 1 i += 1 j += 1 if i == n or j == m: print(-1) else: for k in range(j, m): if b[i] <= c[k]: print(c[k] + tb) break else: print(-1)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR 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 WHILE VAR NUMBER VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n <= k: print(-1) else: ind = 0 maxx = -1 for i in range(k + 1): arriv = a[i] + ta while ind < m and b[ind] < arriv: ind += 1 if ind + (k - i) >= m: maxx = -1 break else: maxx = max(maxx, b[ind + k - i] + tb) print(maxx)
ASSIGN VAR VAR VAR 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 IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) d = {} i = 0 j = 0 while i < n and j < m: if b[j] >= a[i] + ta: d[i] = j i += 1 else: j += 1 M = 0 for i in range(min(n, k) + 1): try: M = max(M, b[d[i] + k - i] + tb) except: M = -1 break print(M)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
import sys input = sys.stdin.readline n, m, ta, tb, k = map(int, input().split()) a = [(int(i) + ta) for i in input().split()] b = [int(i) for i in input().split()] if k >= n or k >= m: print(-1) exit(0) b = list(b) q = 0 ans = -1 for i in range(k + 1): x = a[i] l = k - i while b[q] < x: q += 1 if q >= m: print(-1) exit(0) try: ans = max(ans, b[q + l]) except: print(-1) exit(0) print(ans + tb)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, x, y, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) s = 0 p = 0 m1 = 0 if k >= n or k >= m: print(-1) else: for i in range(k + 1): s = a[i] + x for j in range(p, m): if s > b[j]: continue else: break else: j = m p = j q = p + k - i if q >= m: print(-1) exit() else: ans = b[q] + y if ans > m1: m1 = ans print(m1)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = [int(t) for t in input().split(" ")] da = [int(t) for t in input().split(" ")] db = [int(t) for t in input().split(" ")] best = None i = 0 j = 0 while i <= k and k < n: arrive = da[i] + ta while j < m and db[j] < arrive: j += 1 b_cancel = k - i if j + b_cancel >= m: best = None break final = db[j + b_cancel] + tb best = max(final, best) if best is not None else final i += 1 print(-1 if best is None else best)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NONE FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NONE NUMBER VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, a, b, k = map(int, input().split()) arr = [] brr = [] arr = list(map(int, input().split())) brr = list(map(int, input().split())) t = min(n, m) q = -1 if t <= k: print(q) exit() x = arr[k] + a y = brr[m - 1] if x > y: print(q) exit() else: crr = [] ind = 0 for i in range(0, k + 1): u = arr[i] + a for j in range(ind, len(brr)): v = k - i if brr[j] >= u: ind = j if v >= len(brr) - j: print(q) exit() else: crr.append(j + v) break z = max(crr) print(brr[z] + b)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST 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 ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def match(aa, ia, ta, bb, ib): if ia > len(aa) - 1: return -1 if ib > len(bb) - 1: return -1 for i in range(ib, len(bb)): if bb[i] >= aa[ia] + ta: return i return -1 n, m, ta, tb, k = map(int, input().split()) aa = list(map(int, input().split())) bb = list(map(int, input().split())) ia = 0 ib = match(aa, ia, ta, bb, 0) if k >= n or k >= m: print(-1) exit(0) if ib == -1 or ib + k >= m: print(-1) exit(0) r = bb[ib + k] + tb while ia < k: ia += 1 ib = match(aa, ia, ta, bb, ib) if ib == -1 or ib + k - ia >= m: print(-1) exit(0) else: r = max(r, bb[ib + k - ia] + tb) print(r)
FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
t = list(map(int, input().split(" "))) n = t[0] m = t[1] ta = t[2] tb = t[3] k = t[4] a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) b1 = [] j = 0 q = 0 for i in range(n): a[i] = a[i] + ta while j < m: if a[i] <= b[j]: q += 1 b1.append(b[j]) j += 1 break j += 1 if q <= k: print(-1) else: print(b1[k] + tb)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
from sys import stdin n, m, ta, tb, k = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) b = list(map(int, stdin.readline().split())) i = 0 j = 0 tik = 0 maxi = 0 if n <= k or m <= k: print(-1) elif True: for l in range(k + 1): while j < m and b[j] < a[l] + ta: j = j + 1 f = k - l if j + f >= m: print(-1) tik = 1 break elif b[j + f] > maxi: maxi = b[j + f] if tik == 0: print(maxi + tb)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) j = 0 ans = -1 for i in range(m): if j < n and a[j] + ta <= b[i]: k -= 1 j += 1 if k < 0: ans = b[i] + tb break if j >= n: break print(ans)
ASSIGN VAR VAR VAR 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) fa = list(map(int, input().split())) fb = list(map(int, input().split())) arrival = [(x + ta) for x in fa] i = 0 j = 0 while k > 0: next = arrival[i] fb_time = fb[j] while fb_time < next: j += 1 if j >= m: print(-1) exit() fb_time = fb[j] k -= 1 i += 1 if i >= n: print(-1) exit() j += 1 if j >= m: print(-1) exit() fb_time = arrival[i] for k in range(j, m): if fb_time <= fb[k]: idex = k print(fb[k] + tb) exit() print(-1)
ASSIGN VAR VAR VAR 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 BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def binsearch(arr, val, m, index1): l = index1 r = m - 1 while l <= r: mid = (l + r) / 2 if arr[mid] == val: return mid elif mid > 0 and arr[mid] > val and arr[mid - 1] < val: return mid elif arr[mid] > val: r = mid - 1 else: l = mid + 1 return -1 n, m, ta, tb, k = map(int, input().split()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) arr1.sort() arr2.sort() for i in range(n): arr1[i] += ta i = 0 j = 0 count = 0 if k >= n or k >= m: print(-1) else: i = 0 j = 0 ans = 0 index = 0 for i in range(k + 1): while j < m and arr2[j] < arr1[i]: j += 1 if j + (k - i) >= m: print(-1) exit(0) ans = max(ans, arr2[j + (k - i)] + tb) print(ans)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
import sys n, m, ta, tb, k = [int(i) for i in input().split(" ")] a = [int(i) for i in input().split(" ")] b = [int(i) for i in input().split(" ")] if k >= len(a) or k >= len(b): print(-1) sys.exit() a = [(i + ta) for i in a] c = [] bIdx = 0 for i in a: while bIdx < len(b) and i > b[bIdx]: bIdx += 1 c.append(bIdx) m = 0 for i in range(len(c)): if i > k: break if c[i] + k - i > c[m] + k - m: m = i bIdx = 0 while b[bIdx] < a[m]: bIdx += 1 if bIdx == len(b): print(-1) sys.exit() bIdx += k - m if bIdx >= len(b): print(-1) sys.exit() print(b[bIdx] + tb)
IMPORT ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] i = 0 for j in range(m): if i == n: print(-1) break if a[i] + ta <= b[j]: k -= 1 i += 1 if k < 0: print(b[j] + tb) break else: print(-1)
ASSIGN VAR VAR VAR 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 NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) an = list(map(int, input().split())) bn = list(map(int, input().split())) if n <= k or m <= k: print(-1) else: an = [(i + ta) for i in an] mta = 0 c1 = 0 c2 = 0 for i in range(0, k + 1): c1 = i s = an[c1] while s > bn[c2]: c2 += 1 if c2 + k - i >= m: break else: if bn[c2 + k - i] + tb > mta: mta = bn[c2 + k - i] + tb if c2 + k - i >= m: print(-1) break else: print(mta)
ASSIGN VAR VAR VAR 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 IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
import sys n, m, ta, tb, k = [int(i) for i in input().split()] a = [(int(i) + ta) for i in input().split()] b = [int(i) for i in input().split()] if n <= k or m <= k: print(-1) sys.exit() aind = 0 bind = 0 mx = -1 while True: if b[bind] < a[aind]: bind += 1 if bind == m: mx = -1 break else: h = bind + k - aind if h >= m: mx = -1 break mx = max(mx, b[h] + tb) aind += 1 if aind == k + 1 or aind == n: break print(mx)
IMPORT ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
q = 1 for i in range(q): n, m, t1, t2, k = map(int, input().split()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) l1.append(10**15) l2.append(10**15) if k >= n: print(-1) else: ans = 0 p = 0 for i in range(k + 1): x = i b = l1[x] + t1 while p < m and l2[p] < b: p += 1 ans = max(ans, l2[min(m, p + k - i)] + t2) if ans >= 10**15: print(-1) else: print(ans)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR 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 EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def gns(): return [int(x) for x in input().split()] n, m, ta, tb, k = gns() ns = gns() ms = gns() ns = [(x + ta + tb) for x in ns] ms = [(x + tb) for x in ms] if k >= min(n, m): print(-1) quit() ans = -1 j = 0 for i in range(k + 1): s = ns[i] while j < len(ms) and ms[j] < s: j += 1 if j == len(ms): print(-1) quit() jj = j + k - i if jj >= m: print(-1) quit() ans = max(ans, ms[jj]) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = [int(i) for i in input().split()] a = [(int(i) + ta) for i in input().split()] b = [int(i) for i in input().split()] i, j = 0, 0 while k: if i == n - 1: print(-1) exit() nj = j while j < m and b[j] < a[i]: j += 1 while nj < m and b[nj] < a[i + 1]: nj += 1 j = nj + (nj == j) i += 1 k -= 1 if j >= m: print(-1) exit() print(b[j] + tb)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = list(map(int, input().split())) j = 0 a = list(map(int, input().split())) b = list(map(int, input().split())) t = 0 if k >= min(n, m): t = -1 else: for i in range(k + 1): while j < m and b[j] < a[i] + ta: j += 1 if j + k - i >= m: t = -1 break t = max(t, b[j + k - i] + tb) print(t)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
def bs(num): low = 0 high = m - 1 while low < high: mid = (low + high) // 2 if l2[mid] >= num: high = mid - 1 else: low = mid + 1 if l2[low] < num: return low else: return low - 1 n, m, ta, tb, k = map(int, input().split()) l1 = list(map(int, input().split())) new = list(map(int, input().split())) l2 = [] for i in new: if i >= l1[0] + ta: l2.append(i) m = len(l2) if k >= min(n, m): print(-1) exit() ans = l2[k] + tb for i in range(1, k + 1): num = l1[i] pos = bs(num + ta) if pos == m - 1 and l2[pos] < num + ta: print(-1) exit() if pos + k - i + 1 > m - 1: print(-1) exit() ans = max(ans, l2[pos + k - i + 1] + tb) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR VAR 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 LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) if k >= n or k >= m: print(-1) exit() arr = [] for i in range(n): arr.append(a[i] + ta) arr.append(100000000000000) mm = -1 for i in range(0, min(n, k) + 1): rem = k - i start = 0 end = m - 1 ans = 10000000 while start <= end: mid = start + end >> 1 if b[mid] >= arr[i]: end = mid - 1 ans = min(ans, mid) else: start = mid + 1 if ans + rem >= m: print(-1) exit() else: mm = max(mm, b[ans + rem] + tb) print(mm)
ASSIGN VAR VAR VAR 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 IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady bought an air ticket from a city A to a city C. Unfortunately, there are no direct flights, but there are a lot of flights from A to a city B, and from B to C. There are $n$ flights from A to B, they depart at time moments $a_1$, $a_2$, $a_3$, ..., $a_n$ and arrive at B $t_a$ moments later. There are $m$ flights from B to C, they depart at time moments $b_1$, $b_2$, $b_3$, ..., $b_m$ and arrive at C $t_b$ moments later. The connection time is negligible, so one can use the $i$-th flight from A to B and the $j$-th flight from B to C if and only if $b_j \ge a_i + t_a$. You can cancel at most $k$ flights. If you cancel a flight, Arkady can not use it. Arkady wants to be in C as early as possible, while you want him to be in C as late as possible. Find the earliest time Arkady can arrive at C, if you optimally cancel $k$ flights. If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. -----Input----- The first line contains five integers $n$, $m$, $t_a$, $t_b$ and $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le k \le n + m$, $1 \le t_a, t_b \le 10^9$) — the number of flights from A to B, the number of flights from B to C, the flight time from A to B, the flight time from B to C and the number of flights you can cancel, respectively. The second line contains $n$ distinct integers in increasing order $a_1$, $a_2$, $a_3$, ..., $a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 10^9$) — the times the flights from A to B depart. The third line contains $m$ distinct integers in increasing order $b_1$, $b_2$, $b_3$, ..., $b_m$ ($1 \le b_1 < b_2 < \ldots < b_m \le 10^9$) — the times the flights from B to C depart. -----Output----- If you can cancel $k$ or less flights in such a way that it is not possible to reach C at all, print $-1$. Otherwise print the earliest time Arkady can arrive at C if you cancel $k$ flights in such a way that maximizes this time. -----Examples----- Input 4 5 1 1 2 1 3 5 7 1 2 3 9 10 Output 11 Input 2 2 4 4 2 1 10 10 20 Output -1 Input 4 3 2 3 1 1 999999998 999999999 1000000000 3 4 1000000000 Output 1000000003 -----Note----- Consider the first example. The flights from A to B depart at time moments $1$, $3$, $5$, and $7$ and arrive at B at time moments $2$, $4$, $6$, $8$, respectively. The flights from B to C depart at time moments $1$, $2$, $3$, $9$, and $10$ and arrive at C at time moments $2$, $3$, $4$, $10$, $11$, respectively. You can cancel at most two flights. The optimal solution is to cancel the first flight from A to B and the fourth flight from B to C. This way Arkady has to take the second flight from A to B, arrive at B at time moment $4$, and take the last flight from B to C arriving at C at time moment $11$. In the second example you can simply cancel all flights from A to B and you're done. In the third example you can cancel only one flight, and the optimal solution is to cancel the first flight from A to B. Note that there is still just enough time to catch the last flight from B to C.
n, m, ta, tb, k = map(int, input().split()) a = [(int(i) + ta) for i in input().split()] b = [int(i) for i in input().split()] j = 0 if k >= min(n, m): print(-1) exit() while j < m and b[j] < a[0]: j += 1 if j == m: print(-1) exit() ma = b[j] + tb for i in range(k + 1): if m - j - k + i <= 0: print(-1) exit() else: ma = max(ma, b[j + k - i] + tb) while j < m and i + 1 < n and b[j] < a[i + 1]: j += 1 print(ma)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = [int(i) for i in input().split()] min_l = 0 max_l = 0 min_r = 0 found_max = False found_min = False for i in range(n - 1): if a[i] in a[i + 1 :]: min_r = i if not found_min: found_min = True min_l = i for i in range(1, n): if a[i] in a[:i] and not found_max: found_max = True max_l = i if not found_min: print(0) else: ans = min_r - min_l + 1 act_r = min_r for i in range(min_l, max_l): while act_r < n - 1 and a[i] in a[act_r + 1 :]: act_r += 1 ans = min([ans, act_r - i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input().strip()) li = [int(x) for x in input().split()] se = {} sese = set() for i, nu in enumerate(li): if nu not in se: se[nu] = i else: break res = len(se) ma = 10**10 for i, nu in enumerate(reversed(li), 1): if nu not in sese: sese.add(nu) if nu not in se: ma = min(ma, len(se)) res = max(res, ma + i) else: ma = min(se[nu], ma) res = max(res, ma + i) else: break se = {} sese = set() la = list(reversed(li)) for i, nu in enumerate(la): if nu not in se: se[nu] = i else: break res = max(res, len(se)) ma = 10**10 for i, nu in enumerate(reversed(la), 1): if nu not in sese: sese.add(nu) if nu not in se: ma = min(ma, len(se)) res = max(res, ma + i) else: ma = min(se[nu], ma) res = max(res, ma + i) else: break print(n - res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) A = list(map(int, input().split())) if n == len(set(A)): print(0) exit(0) l = 0 r = n while r - l > 1: gg = False x = (l + r) // 2 G = [set()] H = [] s1 = set() for i in range(n - x): s1.add(A[i]) G.append(s1.copy()) s2 = set() for i in range(n - 1, x - 1, -1): if ( s2.intersection(G[i - x + 1]) == set() and len(s2) == n - i - 1 and len(G[i - x + 1]) == i - x + 1 ): gg = True break s2.add(A[i]) if len(s2) == n - x: gg = True if gg: r = x else: l = x print(l + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = [-1] + list(map(int, input().split())) v = {} def vis(x): if not x in v: v[x] = 1 else: v[x] += 1 def uv(x): v[x] -= 1 ans = n - 1 n += 1 for i, x in enumerate(a): vis(x) if v[x] > 1: break j = n while j > 1: j -= 1 y = a[j] vis(y) if v[y] > 1: break ans = min(ans, max(0, j - i)) while j < n: uv(a[j]) j += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def inp(): return list(map(int, input().split())) (n,) = inp() l = inp() a = set() aa = set() ans = n - 1 for i in range(-1, n): if i >= 0: if l[i] in a: break a.add(l[i]) aa.clear() for j in range(n - 1, -1, -1): if l[j] in aa or l[j] in a: break aa.add(l[j]) ans = min(ans, j - i) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
class Solution: def uniquenessArray(n, arr) -> int: ret = n - 1 for i in range(len(arr)): larr = arr[:i] if len(set(larr)) < len(larr): break l, r, j = i, len(arr), len(arr) + 1 while l <= r: m = (l + r) // 2 rarr = arr[m:] if len(set(larr + rarr)) < len(larr + rarr): l = m + 1 else: r = m - 1 j = m if ret > j - i and j < len(arr) + 1: ret = j - i return ret print(Solution.uniquenessArray(int(input()), [int(x) for x in input().strip().split()]))
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def get_smallest_segment_uniqueness(num, elements): num_not_unique_elements = 0 frequencies = dict() for element in elements: if element not in frequencies: frequencies[element] = 1 else: if frequencies[element] == 1: num_not_unique_elements += 1 frequencies[element] += 1 if num_not_unique_elements == 0: return 0 min_segment_length = None right = 1 left = 0 frequencies[elements[0]] -= 1 if frequencies[elements[0]] == 1: num_not_unique_elements -= 1 if num_not_unique_elements == 0: return 1 while left < num and left < right: while right < num and num_not_unique_elements != 0: frequencies[elements[right]] -= 1 if frequencies[elements[right]] == 1: num_not_unique_elements -= 1 right += 1 if num_not_unique_elements == 0: length = right - left if min_segment_length is None or min_segment_length > length: min_segment_length = length frequencies[elements[left]] += 1 if frequencies[elements[left]] == 2: num_not_unique_elements += 1 left += 1 return min_segment_length def solve(): num = int(input()) line = input() elements = [int(el_str) for el_str in line.split()] print(get_smallest_segment_uniqueness(num, elements)) solve()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
from sys import stdin n = int(stdin.readline().strip()) s = list(map(int, stdin.readline().strip().split())) dp = [[(False) for i in range(n + 1)] for j in range(n + 1)] st = set() for i in range(n): if s[i] in st: break st.add(s[i]) st1 = set() dp[i][n] = True for j in range(n - 1, i, -1): if s[j] in st or s[j] in st1: break dp[i][j] = True st1.add(s[j]) st = set() ans = 100000 for i in range(n - 1, -1, -1): if s[i] in st: break st.add(s[i]) ans = min(ans, n - len(st)) for i in range(n): for j in range(i + 1, n + 1): if dp[i][j] == True: ans = min(ans, j - i - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) nums = list(map(int, input().split(" "))) left = set() res = n for i in range(n): tmp = left.copy() for j in range(n - 1, -1, -1): if nums[j] in tmp: break tmp.add(nums[j]) res = min(res, j - i) if nums[i] in left: break left.add(nums[i]) res = min(res, n - i - 1) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
from sys import stdin, stdout input = stdin.readline print = stdout.write n = int(input()) a = list(map(int, input().split())) def solve(n, a): l = -1 s = set() while l + 1 < n and a[l + 1] not in s: s.add(a[l + 1]) l += 1 r = n while r - 1 >= 0 and a[r - 1] not in s: s.add(a[r - 1]) r -= 1 res = l + 1 + (n - r) while l >= 0: s.remove(a[l]) l -= 1 while r - 1 >= 0 and a[r - 1] not in s: s.add(a[r - 1]) r -= 1 res = max(res, l + 1 + n - r) return n - res print(str(solve(n, a)))
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = [int(x) for x in input().split()] dic = {} arr = 10**100 counter = 0 for item in a: if item not in dic: dic[item] = 1 else: dic[item] += 1 counter += 1 if counter == 0: arr = 0 for i in range(n): for j in range(i, n): dic[a[j]] -= 1 if dic[a[j]] != 0: counter -= 1 if counter == 0: arr = min(arr, j - i + 1) counter = 0 for item in a: dic[item] = 0 for item in a: if dic[item] != 0: counter += 1 dic[item] += 1 print(arr)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
from sys import stdin def solve(nums, n): dl = set() mx = 0 for il, xl in enumerate(nums): dr = set() for ir, xr in enumerate(nums[::-1]): if ir + il >= n - 1 or xr in dr or xr in dl: break dr.add(xr) if xl in dl: mx = max(mx, len(dl) + len(dr)) break if xl not in dr: mx = max(mx, len(dl) + len(dr) + 1) else: mx = max(mx, len(dl) + len(dr)) dl.add(xl) return n - mx def main(): from sys import stdin n = int(stdin.readline().strip()) nums = list(map(int, stdin.readline().strip().split())) print(solve(nums, n)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def check(arr, n, val): for i in range(0, n - val + 1): d = {} flag = 0 for j in range(i): if arr[j] not in d.keys(): d[arr[j]] = 1 else: flag = 1 break if flag == 0: for j in range(i + val, n): if arr[j] not in d.keys(): d[arr[j]] = 1 else: flag = 1 break if flag == 0: return True return False n = int(input()) arr = [int(x) for x in input().split()] left, right = 0, n ans = 0 while left <= right: mid = (left + right) // 2 if check(arr, n, mid) == True: ans = mid right = mid - 1 else: left = mid + 1 print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
from sys import stdin def solve1(nums, n): dl = set() mx = 0 for il, xl in enumerate(nums): dr = set() for ir, xr in enumerate(nums[::-1]): if ir + il >= n - 1 or xr in dr or xr in dl: break dr.add(xr) if xl in dl: mx = max(mx, len(dl) + len(dr)) break if xl not in dr: mx = max(mx, len(dl) + len(dr) + 1) else: mx = max(mx, len(dl) + len(dr)) dl.add(xl) return n - mx def solve2(nums, n): last = {} mx = 0 i = 0 for j, num in enumerate(nums + nums): if num in last: i = max(i, last[num] + 1) last[num] = j if i == 0 or j == n - 1 or 0 < i < n and n <= j: mx = max(mx, j - i + 1) return n - mx solve = solve2 def main(): from sys import stdin n = int(stdin.readline().strip()) nums = list(map(int, stdin.readline().strip().split())) print(solve(nums, n)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def check(arr, k): dictionary = {} for i in range(len(arr) - k): if arr[i] in dictionary: dictionary[arr[i]] += 1 else: dictionary[arr[i]] = 1 if len(dictionary) == len(arr) - k: return True for i in range(len(arr) - k): delete = len(arr) - k - i - 1 add = len(arr) - 1 - i dictionary[arr[delete]] -= 1 if dictionary[arr[delete]] == 0: del dictionary[arr[delete]] if arr[add] in dictionary: dictionary[arr[add]] += 1 else: dictionary[arr[add]] = 1 if len(dictionary) == len(arr) - k: return True return False N = int(input()) line = input().split() arr = [0] * N for i in range(N): arr[i] = int(line[i]) a = 0 b = N while a < b: p = (a + b) // 2 if check(arr, p): b = p else: a = p + 1 print(a)
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
d1 = {} color = [0] * 3000 Main = [] ip = 0 n = int(input()) s1 = input() ans = 0 for data in s1.split(): if data not in d1: d1[data] = ip ip += 1 Main.append(int(d1[data])) for i in range(0, n): take = 0 for j in range(0, n): if color[Main[n - 1 - j]] == 1: break take += 1 color[Main[n - 1 - j]] = 1 ans = max(ans, take + i) for j in range(0, take): color[Main[n - 1 - j]] = 0 if color[Main[i]] == 1: break color[Main[i]] = 1 print(n - ans)
ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
import sys n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) s1 = set() s2 = set() a = [0] + a ans = n for i in range(n + 1): if a[i] not in s1: s1.add(a[i]) else: break s2.clear() for j in range(n, i - 1, -1): if a[j] in s1 or a[j] in s2: ans = min(ans, j - i) break else: s2.add(a[j]) print(ans)
IMPORT 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) l = list(map(int, input().strip().split())) d = {} for i in range(len(l)): t = d.get(l[i], []) t.append(i) d[l[i]] = t left = {} right = {} rid = {} l = [] num = 0 k = 0 m = {} u = {} r = -1 for i in d.keys(): if len(d[i]) == 1: continue elif len(d[i]) == 2: left[num] = d[i][0] l.append([d[i][0], num, k]) right[num] = d[i][0] rid[num] = k num += 1 left[num] = d[i][1] l.append([d[i][1], num, k]) right[num] = d[i][1] rid[num] = k num += 1 m[k] = 0 u[k] = d[i][1] k += 1 r = max(r, d[i][0]) else: left[num] = d[i][0] l.append([d[i][0], num, k]) right[num] = d[i][-2] rid[num] = k num += 1 left[num] = d[i][1] l.append([d[i][1], num, k]) right[num] = d[i][-1] rid[num] = k num += 1 m[k] = 0 u[k] = d[i][-1] k += 1 r = max(r, d[i][-2]) if len(l) == 0: print(0) else: used = 0 le = 1000000000000 l.sort() ans = 1000000000000 for i in l: m[i[2]] += 1 r = max(r, right[i[1]]) ans = min(ans, r - min(i[0], le) + 1) if m[i[2]] == 1: r = max(r, u[i[2]]) if m[i[2]] == 2: le = min(le, i[0]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) m = input().split() mass = [int(m[i]) for i in range(n)] m = [] if len(set(mass)) == len(mass): print(0) else: for i in range(n): if len(set(mass[:i])) == i: beg, end = i, len(mass) - 1 while beg != end: mid = (beg + end + 1) // 2 if len(set(mass[:i] + mass[mid:])) == len(mass[:i] + mass[mid:]): end = mid - 1 else: beg = mid m.append(end + 1 - i) print(min(m))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
import sys input = sys.stdin.readline def main(): N = int(input()) A = list(map(int, input().split())) ans = N for l in range(N): ok = True checked = set() for i in range(l): if A[i] in checked: ok = False break checked.add(A[i]) if not ok: break L = 0 for r in reversed(range(l, N)): if A[r] in checked: L = r - l + 1 break checked.add(A[r]) ans = min(ans, L) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
R = lambda: map(int, input().split()) n = int(input()) L = list(R()) s = set() i = 0 res = n + 1 while i < n and L[i] not in s: s.add(L[i]) i += 1 j = n while i >= 0: while j > i and L[j - 1] not in s: s.add(L[j - 1]) j -= 1 res = min(res, j - i) i -= 1 s -= {L[i]} print(res)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) s, d = {}, {} for q in range(n): s[a[q]] = q d[a[q]] = d.get(a[q], 0) + 1 q2, ans = 0, n - 1 for q1 in d: while d[q1] > 1: d[a[q2]] -= 1 q2 += 1 f = set() for q in range(n): ans = min(ans, q2 - q) if a[q] in f: break f.add(a[q]) q2 = max(q2, s[a[q]] + 1, q + 1) 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 VAR DICT DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) min1 = n + 1 myset = set() for i in range(n): temp = myset.copy() min1 = min(min1, n - i) for j in range(n - 1, i - 1, -1): if a[j] in temp: break temp.add(a[j]) min1 = min(min1, j - i) if a[i] in myset: break myset.add(a[i]) print(min1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) lst = [int(i) for i in input().split()] d, count1 = {}, 0 for elem in lst: d[elem] = d.get(elem, 0) + 1 if d[elem] == 2: count1 += 1 result = n if len(d) == n: result = 0 for i in range(n): f = d.copy() count2 = count1 for j in range(i, n): f[lst[j]] -= 1 if f[lst[j]] == 1: count2 -= 1 if count2 == 0: result = min(result, j - i + 1) break print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split(" "))) lset = set() min_size = n left = -1 right = -1 while left < n: if left != -1: if a[left] in lset: print(min_size) break lset.add(a[left]) diffs = lset.copy() for j in range(n - 1, right, -1): if a[j] in diffs: right = j break else: diffs.add(a[j]) min_size = min(min_size, right - left) left += 1 if left == n: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
import sys input = sys.stdin.readline def getInt(): return int(input()) def getVars(): return map(int, input().split()) def getArr(): return list(map(int, input().split())) def getStr(): return input().strip() def addDictList(d, key, val): if key not in d: d[key] = [] d[key].append(val) def addDictInt(d, key, val): if key not in d: d[key] = 0 d[key] = val def addDictCount(d, key): if key not in d: d[key] = 0 d[key] += 1 def addDictSum(d, key, val): if key not in d: d[key] = 0 d[key] += val def foo(arr): global d2 for i in d2: if arr.count(i) > 1: return False return True def foo2(begin, end): global d2 for k in d2: if not ( d2[k][0] >= begin and d2[k][-2] <= end or d2[k][1] >= begin and d2[k][-1] <= end ): return False return True n = getInt() a = getArr() m = 2001 d = {} for i in range(n): addDictList(d, a[i], i) d2 = {} minLen = 2001 for k in d: if len(d[k]) > 1: d2[k] = d[k] minLen = min(minLen, d[k][-1] - d[k][1] + 1, d[k][-2] - d[k][0] + 1) if len(d2) == 0: print(0) else: for l in range(minLen, n): for begin in range(n - l + 1): res = foo2(begin, begin + l - 1) if res: exit(print(l)) break
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) d = {} for i in range(n): d[a[i]] = 0 i = 0 j = n - 1 length = -1 while i < n: if d[a[i]] == 0: d[a[i]] = 1 i += 1 else: break length = max(i, length) while j >= i: if d[a[j]] == 0: d[a[j]] = 1 j -= 1 elif i > 0: length = max(length, i + n - 1 - j) i -= 1 d[a[i]] = 0 else: length = max(i + n - 1 - j, length) break print(n - length)
ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) ans = n for i in range(n): falg = False u = set() for j in range(i): if a[j] in u: falg = True break u.add(a[j]) if falg: continue j = n - 1 while not a[j] in u: u.add(a[j]) j -= 1 ans = min(ans, j - i + 1) 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
class Uniqueness: def __init__(self): self.N = 0 self.elementList = [] self.counterList = dict() def getMinLengthSubStringForUniqueString(self): self.N = int(input()) tmp = str(input()).split(" ") for x in range(self.N): element = int(tmp[x]) self.elementList.append(element) if element not in self.counterList: self.counterList[element] = 1 else: self.counterList[element] += 1 minLength = self.N if len(self.counterList) == self.N: print(0) return for i in range(self.N): tmpCounterList = self.counterList.copy() for j in range(i, self.N): tmpCounterList[self.elementList[j]] -= 1 if tmpCounterList[self.elementList[j]] == 0: tmpCounterList.pop(self.elementList[j]) if len(tmpCounterList) == self.N - j + i - 1: minLength = min(minLength, j - i + 1) print(minLength) u = Uniqueness() u.getMinLengthSubStringForUniqueString()
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) ans = n - 1 f = dict() for i in range(n): p = True for j in range(i): if a[j] in f: f[a[j]] += 1 else: f[a[j]] = 1 if f[a[j]] == 2: p = False break m = n for j in range(n - 1, i - 1, -1): if a[j] in f: f[a[j]] += 1 else: f[a[j]] = 1 if f[a[j]] == 1: m = j else: break if p: ans = min(ans, m - i) f.clear() 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 BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
import sys input = lambda: sys.stdin.readline().strip() n = int(input()) ls = list(map(int, input().split())) d = [{}] works = {} for i in range(n): works[i] = True for i in range(n): d.append(dict(d[-1])) try: d[-1][ls[i]] += 1 works[i] = False except: d[-1][ls[i]] = 1 m = n for l in range(n): for r in range(n - 1, l - 1, -1): try: d[l][ls[r]] += 0 break except: d[l][ls[r]] = 1 pass if works[l]: m = min(m, r - l + 1) else: m = min(m, r - l + 1) break if len(set(ls)) == n: print(0) else: print(m)
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR 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 DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) ai = list(map(int, input().split())) ar = {} num = 1 for i in range(n): try: ar[ai[i]] += 1 num = i break except: ar[ai[i]] = 1 ans = num if ans == n: print(0) else: for i in range(num - 1, -2, -1): ar3 = {} for j in range(i + 1): ar3[ai[j]] = 1 for j in range(n - 1, -1, -1): try: ar3[ai[j]] += 1 ans = max(ans, i + 1 + n - 1 - j) break except: ar3[ai[j]] = 1 print(n - 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 DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = [int(s) for s in input().split()] r = n - 1 left = set() for i in range(n): right = set() for j in range(1, n + 1): if a[-j] in left or a[-j] in right: r = min(r, n - i - j + 1) break right.add(a[-j]) else: print(0) break if a[i] not in left: left.add(a[i]) else: print(r) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def main(): n = int(input()) aa = list(map(int, input().split())) sa = set() for j in range(n - 1, -1, -1): if aa[j] in sa: best = j + 1 break sa.add(aa[j]) else: print(0) return sa = set() for i, a in enumerate(aa): if a in sa: if n - i < best: best = n - i break sa.add(a) sb = set() for j in range(n - 1, i - 1, -1): if aa[j] in sa or aa[j] in sb: break sb.add(aa[j]) if j - i < best: best = j - i print(best) main()
FUNC_DEF 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def unique(arr): check = set() res = 1000000 for i in range(len(arr)): valid = True for j in range(i): if arr[j] not in check: check.add(arr[j]) else: valid = False break if not valid: continue min_index = len(arr) for j in range(len(arr) - 1, i - 1, -1): if arr[j] not in check: min_index = j check.add(arr[j]) else: break if valid: res = min(res, min_index - i) check.clear() return res n = int(input()) arr = list(map(int, input().split())) print(unique(arr))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split(" "))) if len(set(a)) == n: print(0) else: first = dict() last = dict() for i in range(n): if not a[i] in first: first[a[i]] = i last[a[i]] = i r = n while last[a[r - 1]] == r - 1: r -= 1 ans = r for i in range(n): if first[a[i]] != i: break r = max(r, i + 1, last[a[i]] + 1) ans = min(ans, r - i - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = [int(x) for x in input().split()] mp = {} rep = 0 for i in range(n): if a[i] in mp: mp[a[i]] += 1 rep += 1 else: mp[a[i]] = 1 ans = 1000000 if rep == 0: print(0) else: for i in range(n): z = [] flag = 0 for j in range(i, n): if mp[a[j]] > 1: z.append(a[j]) mp[a[j]] -= 1 rep -= 1 if rep == 0: ans = min(ans, j - i + 1) break rep += len(z) for i in z: mp[i] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def main(): n = int(input()) a = [int(x) for x in input().split()] best = 0 s = set() for i in range(n): if a[i] in s: break else: s.add(a[i]) best = i + 1 s.clear() for i in range(n - 1, -1, -1): if a[i] in s: break else: s.add(a[i]) best = max(best, n - 1 - i + 1) s.clear() p = set() for i in range(n): if a[i] in s: break else: s.add(a[i]) p.clear() for j in range(n - 1, i, -1): if a[j] in s or a[j] in p: break else: p.add(a[j]) best = max(best, i + 1 + (n - 1) - j + 1) print(n - best) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
def answer(n, A): maxi = 0 for i in range(n + 1): s = set(A[:i]) if len(s) < i: break r = n - 1 while A[r] not in s: s.add(A[r]) r -= 1 maxi = max(maxi, len(s)) return n - maxi n = int(input()) arr = list(map(int, input().split())) print(answer(n, arr))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
r = n = int(input()) a = input().split() s = set() i = 0 while i < n and a[i] not in s: s |= {a[i]} i += 1 while i >= 0: while n > i and a[n - 1] not in s: s |= {a[n - 1]} n -= 1 r = min(r, n - i) i -= 1 s -= {a[i]} print(r)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
from sys import stdin input = stdin.readline n = int(input()) (*a,) = map(int, input().split()) if len(a) == len(set(a)): exit(print(0)) s = set() for i in range(n - 1, -1, -1): if a[i] in s: ans = i + 1 break s.add(a[i]) s = set() for i in range(n): if a[i] in s: break s.add(a[i]) tmp = set() for j in range(n - 1, i, -1): if a[j] in tmp or a[j] in s: ans = min(ans, j - i) break tmp.add(a[j]) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) data = [int(i) for i in input().split()] lb = n + 1 ub = -1 dic = {} for i in range(n): d = data[i] if d in dic: dic[d].append(i) else: dic[d] = [i] bounds = [] unique = True for k in dic.values(): if len(k) >= 2: unique = False bounds.append((k[0], k[-1])) lb = min(lb, k[1]) ub = max(ub, k[-2]) if unique: print(0) exit(0) ans = n + 1 for i in range(lb + 1): j = ub for bnd in bounds: if i > bnd[0]: j = max(j, bnd[1]) ans = min(ans, j - i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) k = set(a) m = len(k) if n == m: print(0) else: ans = 9999999999999999999999999999999999999999999999999999999999999999 for i in range(n): mp = {} for j in range(n): if a[j] in mp: mp[a[j]] += 1 else: mp[a[j]] = 1 for j in range(i, n): mp[a[j]] -= 1 if mp[a[j]] == 0: mp.pop(a[j]) p = len(mp) if p == n - j + i - 1: ans = min(ans, j - i + 1) break 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT 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 VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_{1}, a_{2}, \ldots, a_{n}$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and delete integers $a_l, a_{l+1}, \ldots, a_r$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 2000$) — the number of elements in the given array. The next line contains $n$ spaced integers $a_{1}, a_{2}, \ldots, a_{n}$ ($1 \le a_{i} \le 10^{9}$) — the elements of the array. -----Output----- Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $0$. -----Examples----- Input 3 1 2 3 Output 0 Input 4 1 1 2 2 Output 2 Input 5 1 4 1 4 9 Output 2 -----Note----- In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $2$ to $3$. In the third example you can remove the subsegments from index $1$ to $2$, or from index $2$ to $3$, or from index $3$ to $4$.
n = int(input()) a = list(map(int, input().split())) c = {} i = 0 while i < n and a[i] not in c: c[a[i]] = 1 i += 1 ans = n - i j = n while j > i: j -= 1 c[a[j]] = c.get(a[j], 0) + 1 while i > 0 and c[a[j]] > 1: i -= 1 c[a[i]] -= 1 if c[a[j]] > 1: break ans = min(ans, j - i) 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 DICT ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR