description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) a = list(map(int, input().split())) t = 0 if k == 1: t = a[n - 1] - a[0] + 1 elif k >= n: t = n else: x = n - k t = k - x c = [0] * (n - 1) for i in range(1, n): c[i - 1] = a[i] - a[i - 1] + 1 c = sorted(c) for i in range(x): t += c[i] print(t)
ASSIGN 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 NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
nums = input().split() n = int(nums[0]) m = int(nums[1]) k = int(nums[2]) b = input().split() for i in range(n): b[i] = int(b[i]) a = [(0) for i in range(n - 1)] for i in range(n - 1): a[i] = b[i + 1] - b[i] a.sort() if k == n: print(k) else: sum = k for i in range(n - k): sum += a[i] print(sum)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) b = list(map(int, input().split())) c = [] for i in range(n - 1): c.append(b[i + 1] - b[i] - 1) c = list(sorted(c))[::-1] print(b[n - 1] - b[0] + 1 - sum(c[: k - 1]))
ASSIGN 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 LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) a = list(map(int, input().split())) for i in range(len(a) - 1): a[i] = a[i + 1] - a[i] del a[n - 1] a.sort() s = 0 for i in range(n - k): s += a[i] print(s + k)
ASSIGN 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 FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) b = list(map(int, input().split())) d = [] i = b[0] for j in b[1:]: d.append(j - i) i = j d.sort() ans = b[-1] - b[0] + 1 if k > 1: ans = ans - sum(d[-k + 1 :]) + k - 1 print(ans)
ASSIGN 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 LIST ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
mod = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) n, m, k = f() l = il() ldif = [] for i in range(1, n): ldif.append(l[i] - l[i - 1]) ldif.sort() print(sum(ldif[: n - k]) + k)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
def f(k, a): arr = [(el - a[i]) for i, el in enumerate(a[1:])] arr.sort() total = a[-1] - a[0] if k == 1: return total + 1 return total - sum(arr[-(k - 1) :]) + k here = [int(el) for el in input().split(" ")] n = here[0] m = here[1] k = here[2] a = [int(el) for el in input().split(" ")] print(f(k, a))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) b = list(map(int, input().split())) if k == 1: print(b[-1] - b[0] + 1) else: dists = list(sorted(b[i] - b[i - 1] for i in range(1, len(b)))) print(sum(dists[: -(k - 1)]) + k)
ASSIGN 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 IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) a = list(map(int, input().split())) b = [0] * (len(a) - 1) for i in range(len(a) - 1): b[i] = a[i + 1] - a[i] - 1 if len(a) > k: b.sort(reverse=True) print(sum(b[k - 1 :]) + len(a)) else: print(len(a))
ASSIGN 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 BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
l = input().split() n = int(l[0]) m = int(l[1]) k = int(l[2]) l = input().split() li = [int(i) for i in l] z = li[-1] - li[0] l = [] for i in range(1, n): l.append(li[i] - li[i - 1]) l.sort() l.reverse() for i in range(k - 1): z = z - l[i] print(z + k)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n_num, m_num, k_num = map(int, input().split(" ")) b_list = list(map(int, input().split(" "))) b_space = {} for i in range(n_num - 1): b_space[i] = b_list[i + 1] - b_list[i] sort_b_scape = {} for k, v in sorted(b_space.items(), key=lambda x: -x[1]): sort_b_scape[k] = v cut_list = [] if 1 < k_num: cut_list = list(sort_b_scape)[: k_num - 1] cut_list.sort() repair_len = 0 prev = 0 for b_pos in cut_list: cut_gp = b_list[prev : b_pos + 1] repair_len += cut_gp[len(cut_gp) - 1] - cut_gp[0] + 1 prev = b_pos + 1 cut_gp = b_list[prev:] repair_len += cut_gp[len(cut_gp) - 1] - cut_gp[0] + 1 print(repair_len)
ASSIGN VAR VAR 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 DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) x = [*map(int, input().split())] diff = [] for i in range(n - 1): diff.append([x[i + 1] - x[i] - 1, i]) diff.sort(reverse=True) ori = x[-1] - x[0] + 1 for i in range(k - 1): ori -= diff[i][0] print(ori)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
from sys import stdin n, m, k = map(int, stdin.readline().strip().split()) s = list(map(int, stdin.readline().strip().split())) ans = s[-1] - s[0] + 1 s1 = [] for i in range(1, n): s1.append(s[i] - s[i - 1] - 1) s1.sort() for i in range(len(s1) - k + 1, len(s1)): ans -= s1[i] print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) a = [int(x) for x in input().split()] b = [0] * (n - 1) for i in range(n - 1): b[i] = a[i + 1] - a[i] b.sort() print(sum(b[: n - k]) + k)
ASSIGN 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) a = list(map(int, input().split())) i = 0 y = [0] * (n - 1) while i < n - 1: y[i] = a[i + 1] - a[i] i += 1 y.sort() res = p = n j = n - k if p == k: print(res) else: i = 0 while p > k: res += y[i] - 1 i += 1 p -= 1 print(res)
ASSIGN 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
s1 = input().split() s2 = input().split() n = int(s1[0]) m = int(s1[1]) k = int(s1[2]) a = list(s2) dis = [] length = len(a) for i in range(0, n): dis.append(0) ans = n for i in range(1, n): dis[i] = int(a[i]) - int(a[i - 1]) - 1 dis.sort() for i in range(1, n - k + 1): ans += dis[i] print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
p = [] n, m, k = map(int, input().split()) a = [int(z) for z in input().split()] for i in range(n - 1): p.append(a[i + 1] - a[i] - 1) p.sort() p.reverse() print(n + sum(p) - sum(p[: k - 1]))
ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) b = list(map(int, input().split())) lis = [([0] * 2) for i in range(n)] k -= 1 ans = 0 for i in range(1, n): lis[i][0] = b[i] - b[i - 1] lis[i][1] = i lis.sort() li = lis[n - k :] li = [[0, 0]] + sorted(li, key=lambda x: x[1]) + [[0, n]] for i in range(1, len(li)): a = li[i][1] z = li[i - 1][1] ans += b[a - 1] - b[z] + 1 print(ans)
ASSIGN 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 BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST LIST NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER LIST LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
entrada = input().split(" ") n = int(entrada[0]) m = int(entrada[1]) k = int(entrada[2]) broken_segment = input().split(" ") segment_distance = [] for i in range(n): if i > 0: temp = int(broken_segment[i]) - int(broken_segment[i - 1]) segment_distance.append(temp) min_tape = int(broken_segment[n - 1]) - int(broken_segment[0]) + 1 ans = 1 i = len(segment_distance) - 1 segment_distance.sort() for j in range(k - 1): temp = segment_distance[i] if temp: min_tape -= temp - 1 i -= 1 print(min_tape)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = map(int, input().split()) b = list(map(int, input().split())) if k >= n: print(n) exit() dist = [] for i in range(n - 1): dist.append(b[i + 1] - b[i]) dist.sort() print(k + sum(dist[: n - k]))
ASSIGN 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 IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
( lambda N, M, K, n: print( N + sum(sorted(n[i + 1] - n[i] - 1 for i in range(N - 1))[: N - K]) ) )(*map(int, input().split()), list(map(int, input().split())))
EXPR FUNC_CALL FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
You have a long stick, consisting of $m$ segments enumerated from $1$ to $m$. Each segment is $1$ centimeter long. Sadly, some segments are broken and need to be repaired. You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length $t$ placed at some position $s$ will cover segments $s, s+1, \ldots, s+t-1$. You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap. Time is money, so you want to cut at most $k$ continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces? -----Input----- The first line contains three integers $n$, $m$ and $k$ ($1 \le n \le 10^5$, $n \le m \le 10^9$, $1 \le k \le n$) — the number of broken segments, the length of the stick and the maximum number of pieces you can use. The second line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le m$) — the positions of the broken segments. These integers are given in increasing order, that is, $b_1 < b_2 < \ldots < b_n$. -----Output----- Print the minimum total length of the pieces. -----Examples----- Input 4 100 2 20 30 75 80 Output 17 Input 5 100 3 1 2 4 60 87 Output 6 -----Note----- In the first example, you can use a piece of length $11$ to cover the broken segments $20$ and $30$, and another piece of length $6$ to cover $75$ and $80$, for a total length of $17$. In the second example, you can use a piece of length $4$ to cover broken segments $1$, $2$ and $4$, and two pieces of length $1$ to cover broken segments $60$ and $87$.
n, m, k = [int(i) for i in input().split()] s = [int(i) for i in input().split()] e = sorted([(s[i] - s[i - 1] - 1) for i in range(1, n)]) if k == 1: print(sum(e) + n) elif k == n: print(k) else: print(sum(e[: -k + 1]) + n)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
def check_dist(d, a): left = 0 right = 1 while right < len(a): if a[right] - a[left] == d: return left + 1 if a[right] - a[left] > d: left += 1 else: right += 1 return 0 def check_dist_rev(d, a): left = len(a) - 2 right = len(a) - 1 while left > -1: if a[right] - a[left] == d: return left + 1 if a[right] - a[left] > d: right -= 1 else: left -= 1 return 0 def main(): n, l, x, y = read() a = read() x_sat = check_dist(x, a) y_sat = check_dist(y, a) if x_sat and y_sat: print(0) return elif x_sat and not y_sat: print(1) print(y) return elif not x_sat and y_sat: print(1) print(x) return add_sat = check_dist(x + y, a) sub_sat = check_dist(y - x, a) if add_sat: print(1) print(a[add_sat - 1] + x) elif sub_sat: p = sub_sat - 1 if a[p] + y < l: print(1) print(a[p] + y) elif a[p] - x > 0: print(1) print(a[p] - x) else: p = check_dist_rev(y - x, a) - 1 if a[p] + y < l: print(1) print(a[p] + y) elif a[p] - x > 0: print(1) print(a[p] - x) else: print(2) print(x, y) else: print(2) print(x, y) def read(mode=2): inputs = input().strip() if mode == 0: return inputs if mode == 1: return inputs.split() if mode == 2: return list(map(int, inputs.split())) def write(s="\n"): if s is None: s = "" if isinstance(s, list): s = " ".join(map(str, s)) s = str(s) print(s, end="") write(main())
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN FUNC_CALL VAR IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_DEF STRING IF VAR NONE ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
n, l, x, y = map(int, input().split()) a = set(map(int, input().split())) boy = False girl = False one = False where = -1 for i in a: if i + x in a: boy = True if i + y in a: girl = True if i - x > 0 and i - x + y in a: one = True where = i - x if i + x < l and i + x - y in a: one = True where = i + x if i + x + y in a: one = True where = i + x if boy and girl: print(0) if boy and not girl: print(1) print(y) if girl and not boy: print(1) print(x) if not boy and not girl: if one: print(1) print(where) if not one: print(2) print(x, y)
ASSIGN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
n, l, x, y = map(int, input().split(" ")) li = list(map(int, input().split(" ", n)[:n])) li.sort() dic = {} a1, a2 = 0, 0 ans = 2 x1 = x y1 = y xi = -1 yi = -1 for i in li: dic[i] = 1 for i in range(n): if li[i] - x >= 0: if li[i] - x in dic: a1 = 1 xi = i if li[i] + x <= l: if li[i] + x in dic: a1 = 1 xi = i if li[i] - y >= 0: if li[i] - y in dic: a2 = 1 yi = i if li[i] + y <= l: if li[i] + y in dic: a2 = 1 yi = i if a1 == 1 and a2 == 1: print(0) elif a1 == 1: if li[xi] - x >= 0: if li[xi] - x in dic: if li[xi] - x + y <= l and li[xi] - x + y in dic: a2 = 1 if li[xi] - x - y >= 0 and li[xi] - x - y in dic: a2 = 1 if li[xi] + x <= l: if li[xi] + x in dic: if li[xi] + x + y <= l and li[xi] + x + y in dic: a2 = 1 if li[xi] + x - y >= 0 and li[xi] + x - y in dic: a2 = 1 if a2 == 1: print(0) else: print(1) print(y) elif a2 == 1: if li[yi] - y >= 0: if li[yi] - y in dic: if li[yi] - y + x <= l and li[yi] - y + x in dic: a1 = 1 if li[yi] - y - x >= 0 and li[yi] - y - x in dic: a1 = 1 if li[yi] + y <= l: if li[yi] + x in dic: if li[yi] + y + x <= l and li[yi] + y + x in dic: a1 = 1 if li[yi] + y - x >= 0 and li[yi] + y - x in dic: a1 = 1 if a1 == 1: print(0) else: print(1) print(x) else: for i in range(n): if li[i] - x >= 0: a1 = 1 xi = i if li[xi] - x + y <= l and li[xi] - x + y in dic: a2 = 1 if li[xi] - x - y >= 0 and li[xi] - x - y in dic: a2 = 1 if a2 == 1: print(1) print(li[i] - x) break else: a1 = 0 if li[i] + x <= l: a1 = 1 xi = i if li[xi] + x + y <= l and li[xi] + x + y in dic: a2 = 1 if li[xi] + x - y >= 0 and li[xi] + x - y in dic: a2 = 1 if a2 == 1: print(1) print(li[i] + x) break else: a1 = 0 if li[i] - y >= 0: a2 = 1 yi = i if li[yi] - y + x <= l and li[yi] - y + x in dic: a1 = 1 if li[yi] - y - x >= 0 and li[yi] - y - x in dic: a1 = 1 if a1 == 1: print(1) print(li[i] - y) break else: a2 = 0 if li[i] + y <= l: a2 = 1 yi = i if li[yi] + y + x <= l and li[yi] + y + x in dic: a1 = 1 if li[yi] + y - x >= 0 and li[yi] + y - x in dic: a1 = 1 if a1 == 1: print(1) print(li[i] + y) break else: a2 = 0 if a1 == 0 and a2 == 0: print(2) print(x, y)
ASSIGN VAR VAR VAR 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 VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
R = lambda: map(int, input().split()) n, l, x, y = R() arr = set(R()) x_good, y_good = False, False for m in arr: if m + x in arr or m - x in arr: x_good = True break for m in arr: if m + y in arr or m - y in arr: y_good = True break if x_good and y_good: print(0) exit() elif x_good: print(1) print(y) exit() elif y_good: print(1) print(x) exit() else: for m in arr: if m + x <= l and (m + x + y in arr or m + x - y in arr): print(1) print(m + x) exit() if m - x >= 0 and (m - x + y in arr or m - x - y in arr): print(1) print(m - x) exit() if m + y <= l and (m + y + x in arr or m + y - x in arr): print(1) print(m + y) exit() if m - y >= 0 and (m - y + x in arr or m - y - x in arr): print(1) print(m - y) exit() print(2) print(x, y)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
class CodeforcesTask480BSolution: def __init__(self): self.result = "" self.n_l_x_y = [] self.ruler = [] def read_input(self): self.n_l_x_y = [int(x) for x in input().split(" ")] self.ruler = [int(x) for x in input().split(" ")] def process_task(self): dists = {} for a in self.ruler: dists[a] = True hasx = False hasy = False for a in self.ruler: try: if dists[a - self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a + self.n_l_x_y[2]]: hasx = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass try: if dists[a - self.n_l_x_y[3]]: hasy = True except KeyError: pass if hasx and hasy: break if hasx and hasy: self.result = "0" elif hasx: self.result = "1\n{0}".format(self.n_l_x_y[3]) elif hasy: self.result = "1\n{0}".format(self.n_l_x_y[2]) else: res = [0, 0] sgn = False dst = self.n_l_x_y[2] + self.n_l_x_y[3] diff = self.n_l_x_y[3] - self.n_l_x_y[2] for a in self.ruler: try: if dists[a - dst]: if a - self.n_l_x_y[2] > 0: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass try: if dists[a + dst]: if a + self.n_l_x_y[2] < self.n_l_x_y[1]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a - diff]: if a + self.n_l_x_y[2] < self.n_l_x_y[1]: sgn = True res = a + self.n_l_x_y[2] except KeyError: pass try: if dists[a + diff]: if a - self.n_l_x_y[2] > 0: sgn = True res = a - self.n_l_x_y[2] except KeyError: pass if sgn: break if sgn: self.result = "1\n{0}".format(res) else: self.result = "2\n{0} {1}".format(self.n_l_x_y[2], self.n_l_x_y[3]) def get_result(self): return self.result Solution = CodeforcesTask480BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF 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 FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR IF VAR VAR ASSIGN VAR STRING IF VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has n marks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0, an = l). Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d). Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers n, l, x, y (2 ≤ n ≤ 105, 2 ≤ l ≤ 109, 1 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin. Output In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler. In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Output 1 230 Input 4 250 185 230 0 20 185 250 Output 0 Input 2 300 185 230 0 300 Output 2 185 230 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.
def main(): n, l, x, y = map(int, input().split()) arr = set(map(int, input().split())) first = False second = False for i in arr: if i + x in arr: first = True if i + y in arr: second = True if first and not second: print(1) print(y) return if second and not first: print(1) print(x) return if first and second: print(0) return found = False for i in arr: if i + x - y in arr and i + x <= l: found = True coord = i + x break if i + y - x in arr and i + y <= l: found = True coord = i + y break if i + x + y in arr and i + min(x, y) <= l: found = True coord = i + min(x, y) if i - x - y in arr and i - max(x, y) >= 0: found = True coord = i - max(x, y) if i - x + y in arr and i - x >= 0: found = True coord = i - x break if i - y + x in arr and i - y >= 0: found = True coord = i - y break if found: break if found: print(1) print(coord) return print(2) print(x, y) main()
FUNC_DEF ASSIGN 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
There are n railway stations in Berland. They are connected to each other by n-1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree. You have a map of that network, so for each railway section you know which stations it connects. Each of the n-1 sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don't know them. All these values are from 1 to 10^6 inclusive. You asked m passengers some questions: the j-th one told you three values: * his departure station a_j; * his arrival station b_j; * minimum scenery beauty along the path from a_j to b_j (the train is moving along the shortest path from a_j to b_j). You are planning to update the map and set some value f_i on each railway section — the scenery beauty. The passengers' answers should be consistent with these values. Print any valid set of values f_1, f_2, ..., f_{n-1}, which the passengers' answer is consistent with or report that it doesn't exist. Input The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of railway stations in Berland. The next n-1 lines contain descriptions of the railway sections: the i-th section description is two integers x_i and y_i (1 ≤ x_i, y_i ≤ n, x_i ≠ y_i), where x_i and y_i are the indices of the stations which are connected by the i-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway. The next line contains a single integer m (1 ≤ m ≤ 5000) — the number of passengers which were asked questions. Then m lines follow, the j-th line contains three integers a_j, b_j and g_j (1 ≤ a_j, b_j ≤ n; a_j ≠ b_j; 1 ≤ g_j ≤ 10^6) — the departure station, the arrival station and the minimum scenery beauty along his path. Output If there is no answer then print a single integer -1. Otherwise, print n-1 integers f_1, f_2, ..., f_{n-1} (1 ≤ f_i ≤ 10^6), where f_i is some valid scenery beauty along the i-th railway section. If there are multiple answers, you can print any of them. Examples Input 4 1 2 3 2 3 4 2 1 2 5 1 3 3 Output 5 3 5 Input 6 1 2 1 6 3 1 1 5 4 1 4 6 1 3 3 4 1 6 5 2 1 2 5 Output 5 3 1 2 1 Input 6 1 2 1 6 3 1 1 5 4 1 4 6 1 1 3 4 3 6 5 3 1 2 4 Output -1
import sys sys.setrecursionlimit(5233) n = int(input()) G = [[] for i in range(n + 1)] dep = [(0) for i in range(n + 1)] par = [(-1) for i in range(n + 1)] x = [(0) for i in range(n + 1)] y = [(0) for i in range(n + 1)] for i in range(n - 1): x[i], y[i] = map(int, input().split()) G[x[i]].append(y[i]) G[y[i]].append(x[i]) m = int(input()) a = [(0) for i in range(m + 1)] b = [(0) for i in range(m + 1)] g = [[] for i in range(m + 1)] ans = [(0) for i in range(n + 1)] def bfs(): q = [1] par[1] = -2 front = 0 while front < len(q): u = q[front] front += 1 for v in G[u]: if par[v] == -1: par[v] = u dep[v] = dep[u] + 1 q.append(v) def lca(u, v, w): while dep[u] > dep[v]: ans[u] = max(ans[u], w) u = par[u] while dep[v] > dep[u]: ans[v] = max(ans[v], w) v = par[v] while u != v: ans[u] = max(ans[u], w) ans[v] = max(ans[v], w) u = par[u] v = par[v] def check(u, v, w): while dep[u] > dep[v]: if ans[u] == w: return True u = par[u] while dep[v] > dep[u]: if ans[v] == w: return True v = par[v] while u != v: if ans[u] == w: return True if ans[v] == w: return True u = par[u] v = par[v] return False bfs() for i in range(m): a[i], b[i], g[i] = map(int, input().split()) lca(a[i], b[i], g[i]) f = True for i in range(m): if check(a[i], b[i], g[i]) == False: f = False print(-1) break if f == True: for i in range(n - 1): if dep[x[i]] > dep[y[i]]: print(max(1, ans[x[i]]), end=" ") else: print(max(1, ans[y[i]]), end=" ")
IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR STRING
There are n railway stations in Berland. They are connected to each other by n-1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree. You have a map of that network, so for each railway section you know which stations it connects. Each of the n-1 sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don't know them. All these values are from 1 to 10^6 inclusive. You asked m passengers some questions: the j-th one told you three values: * his departure station a_j; * his arrival station b_j; * minimum scenery beauty along the path from a_j to b_j (the train is moving along the shortest path from a_j to b_j). You are planning to update the map and set some value f_i on each railway section — the scenery beauty. The passengers' answers should be consistent with these values. Print any valid set of values f_1, f_2, ..., f_{n-1}, which the passengers' answer is consistent with or report that it doesn't exist. Input The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of railway stations in Berland. The next n-1 lines contain descriptions of the railway sections: the i-th section description is two integers x_i and y_i (1 ≤ x_i, y_i ≤ n, x_i ≠ y_i), where x_i and y_i are the indices of the stations which are connected by the i-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway. The next line contains a single integer m (1 ≤ m ≤ 5000) — the number of passengers which were asked questions. Then m lines follow, the j-th line contains three integers a_j, b_j and g_j (1 ≤ a_j, b_j ≤ n; a_j ≠ b_j; 1 ≤ g_j ≤ 10^6) — the departure station, the arrival station and the minimum scenery beauty along his path. Output If there is no answer then print a single integer -1. Otherwise, print n-1 integers f_1, f_2, ..., f_{n-1} (1 ≤ f_i ≤ 10^6), where f_i is some valid scenery beauty along the i-th railway section. If there are multiple answers, you can print any of them. Examples Input 4 1 2 3 2 3 4 2 1 2 5 1 3 3 Output 5 3 5 Input 6 1 2 1 6 3 1 1 5 4 1 4 6 1 3 3 4 1 6 5 2 1 2 5 Output 5 3 1 2 1 Input 6 1 2 1 6 3 1 1 5 4 1 4 6 1 1 3 4 3 6 5 3 1 2 4 Output -1
n = int(input()) e = [[] for _ in range(n + 1)] for i in range(n - 1): u, v = map(int, input().split()) e[u].append((v, i)) e[v].append((u, i)) p = [(-1, -1) for _ in range(n + 1)] p[1] = -2, -1 h = [(0) for _ in range(n + 1)] q = [1] front = 0 while front < len(q): u = q[front] front += 1 for v in e[u]: if p[v[0]][0] == -1: p[v[0]] = u, v[1] h[v[0]] = h[u] + 1 q.append(v[0]) m = int(input()) q = [() for _ in range(m)] for i in range(m): q[i] = tuple(list(map(int, input().split()))) q.sort(key=lambda u: u[2], reverse=True) f = [(0) for _ in range(n)] for i in range(m): u, v, c = q[i][0], q[i][1], q[i][2] ok = False while h[u] > h[v]: if not f[p[u][1]] > c: ok = True f[p[u][1]] = c u = p[u][0] while h[v] > h[u]: if not f[p[v][1]] > c: ok = True f[p[v][1]] = c v = p[v][0] while u != v: if not f[p[u][1]] > c: ok = True f[p[u][1]] = c if not f[p[v][1]] > c: ok = True f[p[v][1]] = c u = p[u][0] v = p[v][0] if not ok: print(-1) exit() for i in range(n - 1): print(max(f[i], 1), end=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def check(lst, val): visited = set() Flag = False ck = set() for i in range(n): can = False for j in range(m): if lst[j][i] >= val: can = True if j in visited: Flag = True visited.add(j) if not can: return False if Flag: return True return False def solve(m, n, lst): lo = 1 hi = 10**9 + 1 mid = (lo + hi) // 2 ans = 0 while lo <= hi: mid = (lo + hi) // 2 if check(lst, mid): ans = mid lo = mid + 1 else: hi = mid - 1 print(ans) t = int(input()) for i in range(t): x = input() m, n = map(int, input().split()) lst = [] for i in range(m): temp = list(map(int, input().split())) lst.append(temp) solve(m, n, lst)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN 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 ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER 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 ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): input() m, n = list(map(int, input().split())) l = [] maxx = 0 for i in range(m): l.append(list(map(int, input().split()))) maxx = max(maxx, max(l[-1])) lft, rig = 1, maxx + 1 l.sort(reverse=True) while lft < rig: pos, ct, mid, vis = 0, 0, (lft + rig + 1) // 2, [0] * n for i in range(m): ct = 0 for j in range(n): if l[i][j] >= mid: vis[j] = 1 ct += 1 if ct > 1: pos = 1 pos = sum(vis) == n and pos if pos: lft = mid else: rig = mid - 1 print(lft)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): input() m, n = map(int, input().split()) s = [] for i in range(m): s.append(list(map(int, input().split()))) g = [] for i in range(n): x = [] for j in range(m): x.append(s[j][i]) g.append(x) ans = 10**9 vis = [0] * m for i in range(n): M = max(g[i]) ans = min(M, ans) for j in range(m): if g[i][j] == M: vis[j] += 1 f = 0 for i in range(m): if vis[i] > 1: f = 1 break if f: print(ans) else: x = 0 for j in range(m): s[j].sort() x = max(x, s[j][n - 2]) ans = min(ans, x) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys def read_line(): return sys.stdin.readline().strip() def get_max_2_value(arr): max_0 = -1 max_1 = -1 for i in range(len(arr)): if max_0 == -1 or arr[i] >= arr[max_0]: max_1 = max_0 max_0 = i elif max_1 == -1 or arr[i] >= arr[max_1]: max_1 = i return arr[max_1] t = int(read_line()) for _ in range(t): read_line() nm = read_line().split() m = int(nm[0]) n = int(nm[1]) p = [list(map(lambda x: int(x), read_line().split())) for _ in range(m)] max_possible = [max([p[i][x] for i in range(m)]) for x in range(n)] best_result = min(max_possible) if m < n: print(best_result) else: best_reduction = max([get_max_2_value(p[i]) for i in range(m)]) print(min([best_reduction, best_result]))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def search(x, m, n, arr): pair = False valid = [(False) for _ in range(n)] for store in range(m): c = 0 for friend in range(n): if arr[store][friend] >= x: valid[friend] = True c += 1 if c >= 2: pair = True if not pair and m > 1: return False ans = True for truth in valid: ans = ans and truth return ans def solve(m, n, arr): l = 0 r = 10000000000.0 dup = 0 while l < r: mid = (l + r) // 2 if mid == dup: return -1 if search(mid, m, n, arr): if not search(mid + 1, m, n, arr): return int(mid) else: l = mid else: r = mid dup = mid return -1 tests = int(input()) for _ in range(tests): _ = input() m, n = map(int, input().split()) arr = [] for _ in range(m): arr.append(list(map(int, input().split()))) print(solve(m, n, arr))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys def new_years_problem(m, n, joy_by_shop): best_present_by_friend = [0] * n for i in range(m): for j in range(n): best_present_by_friend[j] = max( best_present_by_friend[j], joy_by_shop[i][j] ) best_present_with_min_joy = min(best_present_by_friend) optimal_joy = 0 if n > m: return best_present_with_min_joy for i in range(m): joys = sorted(zip(joy_by_shop[i], range(n)), key=lambda x: -x[0]) optimal_joy = max(optimal_joy, min(best_present_with_min_joy, joys[1][0])) return optimal_joy for _ in range(int(input())): input() m, n = map(int, input().split()) joy_by_shop = [None] * m for i in range(m): joy_by_shop[i] = list(map(int, input().split())) sys.stdout.write(f"{new_years_problem(m, n, joy_by_shop)}\n")
IMPORT FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def OK(x): a = [(0) for i in range(n)] max_c = 0 for i in range(m): c = 0 for j in range(n): if s[i][j] >= x: a[j] = 1 c += 1 max_c = max(c, max_c) return all(a) and max_c > 1 for _ in range(int(input())): input() m, n = map(int, input().split()) s = [list(map(int, input().split())) for i in range(m)] L = 1 R = 10**9 + 1 while R - L > 1: M = (R + L) // 2 if OK(M): L = M else: R = M print(L)
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys input = lambda: sys.stdin.readline().rstrip() INF = 10**19 def solve(): m, n = map(int, input().split()) p = [list(map(int, input().split())) for _ in range(m)] def check(x): canbuy = [[(False) for _ in range(n)] for _ in range(m)] buy = [set() for _ in range(m)] for i in range(m): for j in range(n): if p[i][j] >= x: canbuy[i][j] = True s = set() cann = set() for i in range(m): for j in range(n): if canbuy[i][j]: if i not in s: cann.add(j) s.add((i, j)) if len(cann) != n: return False for i, me in s: for j in range(n): if canbuy[i][j] and j != me: return True ok = 0 ng = 10**18 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if check(mid): ok = mid else: ng = mid print(ok) t = int(input()) for _ in range(t): input() solve()
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys I = lambda: [*map(int, sys.stdin.readline().split())] (t,) = I() for _ in range(t): I() m, n = I() p = [I() for i in range(m)] q = [[p[i][j] for i in range(m)] for j in range(n)] big = [max(q[i]) for i in range(n)] if m < n: print(min(big)) continue inds = [set(range(m)) for i in range(n)] ones = 0 order = [] for i in range(m): for j in range(n): order.append((p[i][j], i, j)) order.sort() uses = [n] * m ind = 0 done = False for i in range(m * n): val, store, person = order[i] inds[person].remove(store) uses[store] -= 1 while ind < m and uses[ind] <= 1: ind += 1 if ind == m: print(val) done = True break if len(inds[person]) == 0: print(val) done = True break if not done: print(min(big))
IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) for i in range(t): input() x, y = [], [] n, m = map(int, input().split()) for j in range(n): x.append(list(map(int, input().split()))) for j in range(n): for g in range(m): y.append([x[j][g], j, g]) y.sort() y.reverse() found = [(0) for i in range(m)] deg = [(0) for j in range(n)] deg2 = False rem = m for [v, i, j] in y: deg[i] += 1 if deg[i] >= 2: deg2 = True if found[j] == 0: rem -= 1 found[j] = 1 if deg2 and rem == 0: print(v) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR LIST VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): input() arr = [int(x) for x in input().split()] m, n = arr[0], arr[1] arr = [] for i in range(m): li = [int(x) for x in input().split()] arr.append(li) high, low = 10**14, 0 ans = 0 while low <= high: mid = (low + high) // 2 s = set() can2 = False for j in range(n): can = False for i in range(m): if arr[i][j] >= mid: can = True if i in s: can2 = True else: s.add(i) if can == False: break if can and can2: ans = mid low = mid + 1 else: high = mid - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def find(arr, m, n): maxArr = [0] * n for j in range(n): for i in range(m): if arr[i][j] > maxArr[j]: maxArr[j] = arr[i][j] r = min(maxArr) l = 0 ans = 0 while r >= l: mid = (r + l) // 2 for i in range(m): count = 0 for j in range(n): if arr[i][j] >= mid: count += 1 if count > 1: ans = mid l = mid + 1 break if count < 2: r = mid - 1 return ans res = [] r = int(input()) for i in range(r): input() [m, n] = list(map(int, input().split())) arr = [] for i in range(m): arr.append(list(map(int, input().split()))) res.append(find(arr, m, n)) for i in res: print(i)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def solve(): input() m, n = list(map(int, input().split())) p = [] for _ in range(m): p.append(list(map(int, input().split()))) def ops(num): flag = True temp = [(0) for _ in range(m)] for j in range(n): tmp = 0 for i in range(m): if p[i][j] >= num: temp[i] += 1 tmp = 1 if tmp == 0: flag = False return True if flag and max(temp) >= 2 else False l, r = 0, 10**9 + 1 while r - l > 1: mid = l + r >> 1 if ops(mid): l = mid else: r = mid print(l) for _ in range(int(input())): solve()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def func(): emp = input() m, n = map(int, input().split()) inp = [] for i in range(m): inp.append(list(map(int, input().split()))) bdp = [(-1) for i in range(n)] for i in range(m): for j in range(n): bdp[j] = max(bdp[j], inp[i][j]) ans = min(bdp) if m < n: return ans req = -1 for i in range(m): inp[i].sort(reverse=True) req = max(req, min(ans, inp[i][1])) return req t = int(input()) for i in range(t): print(func())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
numeroTestes = int(input()) results = [] for a in range(numeroTestes): input() lojas, amigos = [int(t) for t in input().split()] presentesPorLojas = [] for i in range(lojas): pesentes = [int(x) for x in input().split()] presentesPorLojas.append(pesentes) res = 0 for i in range(lojas): res = max(res, sorted(presentesPorLojas[i])[-2]) result = float("inf") for i in range(amigos): maioDiversao = 0 for j in range(lojas): maioDiversao = max(presentesPorLojas[j][i], maioDiversao) result = min(result, maioDiversao) results.append(min(res, result)) for r in results: print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def check(r): flag = False cols = [(False) for i in range(n)] for i in range(m): kolvo = 0 for j in range(n): if d[i][j] >= r: cols[j] = True kolvo += 1 if kolvo >= 2: flag = True if flag and False not in cols: return True else: return False t = int(input()) for i in range(t): input() m, n = map(int, input().split()) d = [] for j in range(m): d.append(list(map(int, input().split()))) lev = 1 prav = 10**10 u = (lev + prav) // 2 while lev + 1 < prav: if check(u): lev = u else: prav = u u = (lev + prav) // 2 print(lev)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): input() n, m = map(int, input().split()) rd = [] for i in range(m): rd.append(0) top2 = [] for i in range(n): fr = list(map(int, input().split())) tt = [0, 0] for f in range(m): if fr[f] > rd[f]: rd[f] = fr[f] if fr[f] > tt[1]: if fr[f] >= tt[0]: tt[1] = tt[0] tt[0] = fr[f] else: tt[1] = fr[f] top2.append(tt[1]) a = min(rd[i] for i in range(m)) b = max(top2) print(min(a, b))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) p = [] m, n = 0, 0 def check(mid): flag = False person = [False] * n for i in range(m): c = 0 for j in range(n): if p[i][j] >= mid: person[j] = True c = c + 1 if c > 1: flag = True if not flag and n > 1: return False ans = True for i in range(n): ans = ans & person[i] return ans for _ in range(t): input() p = [] m, n = map(int, input().split(" ")) for _ in range(m): p.append(list(map(int, input().split(" ")))) l, r = 0, 10**9 while l < r: mid = (l + r + 1) // 2 if check(mid): l = mid else: r = mid - 1 print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def solve(): def possible(k): a = [(0) for i in range(n)] max_v = 0 for i in range(m): c = 0 for j in range(n): if arr[i][j] >= k: a[j] = 1 c += 1 max_v = max(max_v, c) return all(a) and max_v > 1 input() m, n = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(m)] low, high = 1, 10**9 + 1 while low <= high: mid = low + high >> 1 if possible(mid): ans = mid low = mid + 1 else: high = mid - 1 return ans t = int(input()) while t: print(solve()) t -= 1
FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def czy_sie_da(x, sklepy, iloS, iloO): osoTmp = [False] * iloO for i in range(iloO): for j in range(iloS): if sklepy[j][i] >= x: osoTmp[i] = True if False in osoTmp: return False for i in range(iloS): tmp = 0 for j in range(iloO): if sklepy[i][j] >= x: tmp += 1 if tmp == 2: return True return False def main(): iloP = int(input()) for i in range(iloP): input() iloS, iloO = list(map(int, input().split())) sklepy = [0] * iloS for j in range(iloS): sklepy[j] = list(map(int, input().split())) pocz = 0 kon = 10**9 wyn = 0 while pocz < kon: mid = (pocz + kon + 1) // 2 if czy_sie_da(mid, sklepy, iloS, iloO): pocz = mid wyn = max(mid, wyn) else: kon = mid - 1 print(wyn) main()
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys def check(x, mat, n, m): flag = False pair = [False] * m for i in range(n): c = 0 for j in range(m): if mat[i][j] >= x: pair[j] = True c += 1 if c > 1: flag = True if not flag and m > 1: return False ans = sum(pair) if ans == m: return True return False def solve(): for t in range(1): s = input() n, m = map(int, input().split()) arr = [] mx = sys.maxsize * -1 for i in range(n): a = list(map(int, input().split())) mx = max(mx, max(a)) arr.append(a) start = 1 end = mx ans = None while start <= end: mid = (start + end) // 2 if check(mid, arr, n, m): ans = mid start = mid + 1 else: end = mid - 1 print(ans) for _ in range(int(input())): solve()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def main(): def segfunc(x, y): return min(x, y) INF = 10**10 ide_ele = INF class SegTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num for i in range(n): self.tree[self.num + i] = init_val[i] for i in range(self.num - 1, 0, -1): self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res def answer(): m, n = map(int, input().split()) p = [list(map(int, input().split())) for _ in range(m)] p = list(zip(*p)) M = [max(p[i]) for i in range(n)] T = SegTree(M, segfunc, ide_ele) if n > m: print(min(M)) return ans = -1 for i in range(n - 1): tm = T.query(0, i) if tm == -1: tm = INF pi = p[i] for j in range(i + 1, n): pj = p[j] tmp = -1 for k in range(m): tmp = max(tmp, min(pi[k], pj[k])) tij = T.query(i + 1, j) if tij == -1: tij = INF tj = T.query(j + 1, n) if tj == -1: tj = INF t_ans = min(tmp, tm, tij, tj) ans = max(ans, t_ans) print(ans) for _ in range(int(input())): x = input() answer() main()
FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
from sys import stdin for _ in range(int(input())): x = input() s, f = map(int, stdin.readline().split()) shops = [i for i in range(f)] d = set() mat = [] for i in range(s): m = list(map(int, stdin.readline().split())) mat.append(m) l = [] for j in range(f): M = 0 ind = -1 for i in range(s): if mat[i][j] >= M: M = mat[i][j] ind = i shops[j] = ind d.add(ind) l.append(M) ans = -1 if len(d) < f: ans = min(l) print(ans) if ans == -1: M = 0 for i in range(s): mat[i].sort(reverse=True) M = max(M, mat[i][1]) print(min(min(l), M))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
Z = input Y = lambda: map(int, Z().split()) for _ in range(int(Z())): Z() n, m = Y() a = [[*Y()] for i in range(n)] print( min( min(max(a[j][i] for j in range(n)) for i in range(m)), max(sorted(a[i])[-2] for i in range(n)), ) )
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys input = sys.stdin.readline for _ in range(int(input())): aux = input() m, n = map(int, input().split()) mat = [] for i in range(m): l = list(map(int, input().split())) mat.append(l) d = {} h = {} s = set() flag = 0 cnt = 10**18 for i in range(n): som = -1 c = {} for j in range(m): if mat[j][i] in c: c[mat[j][i]].append(j) else: c[mat[j][i]] = [j] if mat[j][i] > som: som = mat[j][i] d[i] = som h[i] = c[som] cnt = min(cnt, som) for x in h[i]: if x in s: flag = 1 break s.add(x) if flag: print(cnt) continue ans = -1 for i in range(m): o = mat[i] o.sort() ans = max(ans, min(cnt, o[-2])) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR LIST VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def happywith(x): happy = [False] * friends possible = False for shop in range(shops): count = 0 for friend in range(friends): if mat[shop][friend] >= x: happy[friend] = True count += 1 if count > 1: possible = True return possible and all(happy) for _ in range(int(input())): _ = input() shops, friends = map(int, input().strip().split()) mat = (*((*map(int, input().split()),) for i in range(shops)),) if shops < friends: print( min( max(mat[shop][friend] for shop in range(shops)) for friend in range(friends) ) ) else: right = 1 while happywith(right): right *= 2 left = right // 2 while left < right - 1: mid = (left + right) // 2 if happywith(mid): left = mid else: right = mid print(left)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): input() m, n = map(int, input().split()) p = [] for i in range(m): a = list(map(int, input().split())) for j in range(n): p.append([a[j], i, j]) p.sort() got = False sf = set() ss = set() for i in range(m * n - 1, -1, -1): sf.add(p[i][2]) if p[i][1] in ss: got = True ss.add(p[i][1]) if len(sf) == n and got: ans = p[i][0] break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) for _ in range(t): _ = input() m, n = map(int, input().split()) a = [] for _ in range(m): a.append(list(map(int, input().split()))) mx, p = [0] * n, [0] * n for i in range(m): for j in range(n): if a[i][j] > mx[j]: mx[j], p[j] = a[i][j], i ans = min(mx) if n <= m and len(set(p)) == n: ans = 0 for i in range(n - 1): for j in range(i + 1, n): t, t1, t2 = 0, mx[i], mx[j] for _ in range(m): t = max(t, min(a[_][i], a[_][j])) mx[i], mx[j] = t, t ans = max(ans, min(mx)) mx[i], mx[j] = t1, t2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) while t: def fun(num, n, m, li): arr = [False] * m flag = 0 for i in range(n): c = 0 for j in range(m): if li[i][j] >= num: arr[j] = True c += 1 if c > 1: flag = 1 if not flag and m > 1: return False ans = True for i in arr: ans = ans and i return ans space = input() n, m = map(int, input().split()) li = [] for i in range(n): a = list(map(int, input().split())) li.append(a) low = 1 high = 2 while fun(high, n, m, li): high *= 2 while high - low > 1: mid = (low + high) // 2 if fun(mid, n, m, li): low = mid else: high = mid print(low) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def solve(mid, p, m, n): for i in range(n): flag = 1 for j in range(m): if p[j][i] >= mid: flag = 0 break if flag == 1: return False for i in range(m): count = 0 for j in range(n): if p[i][j] >= mid: count += 1 if count >= 2: return True return False t = int(input()) for _ in range(t): input() m, n = map(int, input().split()) p = [] mini = 10**8 maxi = 0 mid = (mini + maxi) // 2 for i in range(m): arr = list(map(int, input().split())) mini = min(mini, min(arr)) maxi = max(maxi, max(arr)) p.append(arr) ans = 0 while mini <= maxi: mid = (mini + maxi) // 2 if solve(mid, p, m, n): mini = mid + 1 ans = max(ans, mid) else: maxi = mid - 1 print(ans)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
from sys import stdin input = stdin.readline rn = lambda: int(input()) rns = lambda: map(int, input().split()) rl = lambda: list(map(int, input().split())) rs = lambda: input().strip() YN = lambda x: print("YES") if x else print("NO") ceil_div = lambda a, b: -(-a // b) mod = 10**9 + 7 for _ in range(rn()): rs() m, n = rns() grid = [rl() for i in range(m)] lo = 1 hi = 10**9 ans = 1 while lo <= hi: mid = (lo + hi) // 2 s = set() need = 0 br = False for i in range(n): good = [j for j in range(m) if grid[j][i] >= mid] if len(good) == 0: br = True break f = False for num in good: if num in s: f = True break s.add(num) if not f: need += 1 if need < n and not br: ans = max(ans, mid) lo = mid + 1 else: hi = mid - 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def nypb(m, n, l2): ms = n - 1 mv = [0] * n for i in range(m): cm = 0 for j in range(n): mv[j] = max(mv[j], l2[i][j]) ans = mv[0] for x in range(len(mv)): ans = min(ans, mv[x]) if m <= n - 1: return ans cmm = 0 mx1 = [] mx2 = [] for i in range(m): l5 = l2[i].copy() l5.sort() mx1.append(l5[-1]) mx2.append(l5[-2]) ans = min(ans, max(mx1), max(mx2)) return ans t = int(input()) ls = [] l = [] _ = str(input()) for x in range(t): s = str(input()) ls.append(s) l1 = [] for y in range(int(s.split(" ")[0])): s1 = str(input()) l1.append(s1) l.append(l1) if x == t - 1: break _ = str(input()) la = [] for i in range(len(l)): m, n = int(ls[i].split(" ")[0]), int(ls[i].split(" ")[1]) l2 = [] for x in l[i]: y = x.split(" ") l3 = [] for z in y: l3.append(int(z)) l2.append(l3) ans = nypb(m, n, l2) la.append(ans) for x in la: print(x)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) for _ in range(t): input() m, n = [int(x) for x in input().split()] max_joy = [0] * n alfa = 0 for i in range(m): shop = [int(joy) for joy in input().split()] for j in range(n): max_joy[j] = max(max_joy[j], shop[j]) shop.sort() alfa = max(alfa, shop[n - 2]) alfa = min(alfa, min(max_joy)) print(alfa)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys def solve(m, n, mat): mx = [0] * n mx2 = 0 for row in mat: n1, n2 = 0, 0 for i, x in enumerate(row): mx[i] = max(mx[i], x) if x > n1: n1, n2 = x, n1 elif x > n2: n2 = x mx2 = max(mx2, n2) if m < n: return min(mx) else: return min(min(mx), mx2) t = int(sys.stdin.readline()) for _ in range(t): sys.stdin.readline() m, n = map(int, sys.stdin.readline().split()) mat = [] for _ in range(m): mat.append(list(map(int, sys.stdin.readline().split()))) print(solve(m, n, mat))
IMPORT FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
q = int(input()) for qwerty in range(q): kpz70 = input() x, y = map(int, input().split()) a = [(0) for i in range(y)] b = 0 for i in range(x): z = list(map(int, input().split())) for j in range(y): if z[j] > a[j]: a[j] = z[j] if x > y - 1: z.sort() if z[y - 2] > b: b = z[y - 2] if b > 0: a.append(b) print(min(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
t = int(input()) for t in range(t): input() m, n = list(map(int, input().split())) a = [([0] * n) for i in range(m)] for i in range(m): a[i] = list(map(int, input().split())) def chk(x): ok = 1 c = [0] * m for j in range(n): tmp = 0 for i in range(m): if a[i][j] >= x: c[i] += 1 tmp = 1 if tmp == 0: ok = 0 if ok and max(c) >= 2: return 1 return 0 l = 0 r = 1000000000 + 1 while r - l > 1: mid = l + r >> 1 if chk(mid): l = mid else: r = mid print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER 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 NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def check(b, c, m, n, k): if m <= n - 1: return min(*b) >= k return min(*b) >= k and max(*c) >= k def solve(): input() a = list(map(int, input().split(" "))) m = a[0] n = a[1] a = [list(map(int, input().split(" "))) for i in range(m)] b = [(0) for i in range(n)] c = [(0) for i in range(m)] r = 0 for i in range(m): for j in range(n): r = max(r, a[i][j]) b[j] = max(b[j], a[i][j]) a[i].sort() c[i] = a[i][n - 2] l = 1 ans = r while l <= r: mid = l + r >> 1 if check(b, c, m, n, mid): ans = mid l = mid + 1 else: r = mid - 1 print(ans) for t in range(int(input())): solve()
FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): aaa = input() m, n = map(int, input().split()) A = [] for i in range(m): an = list(map(int, input().split())) A.append(an) l = 1 ans = 1 h = 10**9 last = -1 while l <= h: mid = (l + h) // 2 s = set() can, can2 = True, False for j in range(n): can = False for i in range(m): if A[i][j] >= mid: can = True if i in s: can2 = True s.add(i) if not can: break if can and can2: l = mid + 1 ans = mid else: h = mid - 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def main(): test = int(input()) for idt in range(test): _ = input() m, n = map(int, input().split()) store_person = [([0] * n) for _ in range(m)] for i in range(m): a = list(map(int, input().split())) for j in range(n): store_person[i][j] = a[j] person_store = [[] for _ in range(n)] for i in range(n): for j in range(m): person_store[i].append([store_person[j][i], j, i]) person_store[i].sort(reverse=True) selected = set() maxs = [0] * n for i in range(n): score, store, person = person_store[i][0] maxs[person] = max(maxs[person], score) selected.add(store) if len(selected) <= n - 1: print(min(maxs)) continue ans = 0 for j in range(n): for k in range(j + 1, n): cur = float("inf") for i in range(n): if i == j or i == k: continue cur = min(cur, maxs[i]) opt = 0 for i in range(m): opt = max(opt, min(store_person[i][j], store_person[i][k])) cur = min(cur, opt) ans = max(ans, cur) print(ans) return main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys def find(x): two = False ans = [(False) for _ in range(m)] for i in range(n): k = 0 for j in range(m): if joys[i][j] >= x: k += 1 ans[j] = True if k >= 2: two = True if two and all(ans): return True else: return False k = int(sys.stdin.readline()) for _ in range(k): sys.stdin.readline() n, m = map(int, sys.stdin.readline().split()) joys = [] max_joy = 0 min_joy = 10**6 for _ in range(n): joy = list(map(int, sys.stdin.readline().split())) joys.append(joy) ma, mi = max(joy), min(joy) if max_joy < ma: max_joy = ma if min_joy > mi: min_joy = mi mid = (min_joy + max_joy) // 2 while mid >= min_joy: if find(mid): min_joy = mid + 1 else: max_joy = mid - 1 mid = (min_joy + max_joy) // 2 print(mid)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
from sys import stdin input = stdin.readline def answer(): ans = float("inf") for i in range(n): ans = min(ans, max(joy[i])) ansval = -float("inf") for i in range(m): ansval = max(ansval, min(ans, shop[i])) return ansval for T in range(int(input())): input().strip() m, n = map(int, input().split()) shop = [(0) for i in range(m)] joy = [[] for i in range(n)] for i in range(m): x = list(map(int, input().split())) for j in range(n): joy[j].append(x[j]) shop[i] = sorted(x)[-2] print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR 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 VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
import sys t = int(input()) for _ in range(t): sys.stdin.readline() m, n = map(int, input().split()) lst = [] res = 0 for i in range(m): ll = list(map(int, input().split())) lst.append(ll) res = max(res, sorted(lst[-1])[-2]) for i in range(n): aa = 0 for j in range(m): aa = max(aa, lst[j][i]) res = min(res, aa) print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
for _ in range(int(input())): input() m, n = map(int, input().split()) happiness = [[int(x) for x in input().split()] for _ in range(m)] max_happiness_by_friend = [max(hap[i] for hap in happiness) for i in range(n)] max_happiness_by_shop = [0] * m for i, hap in enumerate(happiness): hap.sort() max_happiness_by_shop[i] = hap[-2] mmhbs = max(max_happiness_by_shop) mmhbf = min(max_happiness_by_friend) print(min(mmhbs, mmhbf))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Vlad has $n$ friends, for each of whom he wants to buy one gift for the New Year. There are $m$ shops in the city, in each of which he can buy a gift for any of his friends. If the $j$-th friend ($1 \le j \le n$) receives a gift bought in the shop with the number $i$ ($1 \le i \le m$), then the friend receives $p_{ij}$ units of joy. The rectangular table $p_{ij}$ is given in the input. Vlad has time to visit at most $n-1$ shops (where $n$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $j$-th friend receive $a_j$ units of joy from Vlad's gift. Let's find the value $\alpha=\min\{a_1, a_2, \dots, a_n\}$. Vlad's goal is to buy gifts so that the value of $\alpha$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $m = 2$, $n = 2$. Let the joy from the gifts that we can buy in the first shop: $p_{11} = 1$, $p_{12}=2$, in the second shop: $p_{21} = 3$, $p_{22}=4$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $3$, and for the second — bringing joy $4$. In this case, the value $\alpha$ will be equal to $\min\{3, 4\} = 3$ Help Vlad choose gifts for his friends so that the value of $\alpha$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $n-1$ shops (where $n$ is the number of friends). In the shop, he can buy any number of gifts. -----Input----- The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $m$ and $n$ ($2 \le n$, $2 \le n \cdot m \le 10^5$) separated by a space — the number of shops and the number of friends, where $n \cdot m$ is the product of $n$ and $m$. Then $m$ lines follow, each containing $n$ numbers. The number in the $i$-th row of the $j$-th column $p_{ij}$ ($1 \le p_{ij} \le 10^9$) is the joy of the product intended for friend number $j$ in shop number $i$. It is guaranteed that the sum of the values $n \cdot m$ over all test cases in the test does not exceed $10^5$. -----Output----- Print $t$ lines, each line must contain the answer to the corresponding test case — the maximum possible value of $\alpha$, where $\alpha$ is the minimum of the joys from a gift for all of Vlad's friends. -----Examples----- Input 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 Output 3 2 4 8 2 -----Note----- None
def solve(m, n, lst): temp = [] res = 0 for i in range(m): res = max(res, sorted(lst[i])[-2]) result = 10**9 for i in range(n): x = 0 for j in range(m): x = max(lst[j][i], x) result = min(result, x) print(min(result, res)) t = int(input()) for i in range(t): x = input() m, n = map(int, input().split()) lst = [] for i in range(m): temp = list(map(int, input().split())) lst.append(temp) solve(m, n, lst)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
A gallery with plants is divided into n parts, numbered : 0,1,2,3...n-1. There are provisions for attaching water sprinklers at every partition. A sprinkler with range x at partition i can water all partitions from i-x to i+x. Given an array gallery[ ] consisting of n integers, where gallery[i] is the range of sprinkler at partition i (power==-1 indicates no sprinkler attached), return the minimum number of sprinklers that need to be turned on to water the complete gallery. If there is no possible way to water the full length using the given sprinklers, print -1. Example 1: Input: n = 6 gallery[ ] = {-1, 2, 2, -1, 0, 0} Output: 2 Explanation: Sprinklers at index 2 and 5 can water thefull gallery, span of sprinkler at index 2 = [0,4] and span of sprinkler at index 5 = [5,5]. Example 2: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0} Output: -1 Explanation: No sprinkler can throw water at index 7. Hence all plants cannot be watered. Example 3: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 0, 0, 0, 0, 0} Output: 3 Explanation: Sprinkler at indexes 2, 7 and 8 together can water all plants. Your task: Your task is to complete the function min_sprinklers() which takes the array gallery[ ] and the integer n as input parameters and returns the value to be printed. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ n ≤ 10^{5} gallery[i] ≤ 50
class Solution: def min_sprinklers(self, gallery, n): sprinklers = [] for i in range(n): if gallery[i] > -1: sprinklers.append([i - gallery[i], i + gallery[i]]) sprinklers.sort() target = 0 sprinklers_on = 0 i = 0 while target < n: if i == len(sprinklers) or sprinklers[i][0] > target: return -1 max_range = sprinklers[i][1] while i + 1 < len(sprinklers) and sprinklers[i + 1][0] <= target: i += 1 max_range = max(max_range, sprinklers[i][1]) if max_range < target: return -1 sprinklers_on += 1 target = max_range + 1 i += 1 return sprinklers_on
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
A gallery with plants is divided into n parts, numbered : 0,1,2,3...n-1. There are provisions for attaching water sprinklers at every partition. A sprinkler with range x at partition i can water all partitions from i-x to i+x. Given an array gallery[ ] consisting of n integers, where gallery[i] is the range of sprinkler at partition i (power==-1 indicates no sprinkler attached), return the minimum number of sprinklers that need to be turned on to water the complete gallery. If there is no possible way to water the full length using the given sprinklers, print -1. Example 1: Input: n = 6 gallery[ ] = {-1, 2, 2, -1, 0, 0} Output: 2 Explanation: Sprinklers at index 2 and 5 can water thefull gallery, span of sprinkler at index 2 = [0,4] and span of sprinkler at index 5 = [5,5]. Example 2: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0} Output: -1 Explanation: No sprinkler can throw water at index 7. Hence all plants cannot be watered. Example 3: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 0, 0, 0, 0, 0} Output: 3 Explanation: Sprinkler at indexes 2, 7 and 8 together can water all plants. Your task: Your task is to complete the function min_sprinklers() which takes the array gallery[ ] and the integer n as input parameters and returns the value to be printed. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ n ≤ 10^{5} gallery[i] ≤ 50
class Solution: def min_sprinklers(self, gallery, n): l = [] for i in range(n): if gallery[i] == -1: continue l.append([max(0, i - gallery[i]), min(n - 1, i + gallery[i])]) l.sort() l1 = [] for i in l: a, b = i[0], i[1] flag = 0 while len(l1) > 0 and l1[len(l1) - 1][0] >= a and l1[len(l1) - 1][1] <= b: l1.pop() flag = 1 continue if len(l1) > 0 and l1[len(l1) - 1][0] <= a and l1[len(l1) - 1][1] >= b: continue if len(l1) > 0 and l1[len(l1) - 1][1] < b: if i[0] <= l1[len(l1) - 1][1]: l1.append([l1[len(l1) - 1][1] + 1, i[1]]) continue l1.append(i) if len(l1) == 0: l1.append(i) continue g1 = dict() for i in range(n): g1[i] = True for i in l1: for j in range(i[0], i[1] + 1): g1[j] = False for i in g1.keys(): if g1[i] == True: return -1 if len(l1) == 0: return -1 return len(l1)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR
A gallery with plants is divided into n parts, numbered : 0,1,2,3...n-1. There are provisions for attaching water sprinklers at every partition. A sprinkler with range x at partition i can water all partitions from i-x to i+x. Given an array gallery[ ] consisting of n integers, where gallery[i] is the range of sprinkler at partition i (power==-1 indicates no sprinkler attached), return the minimum number of sprinklers that need to be turned on to water the complete gallery. If there is no possible way to water the full length using the given sprinklers, print -1. Example 1: Input: n = 6 gallery[ ] = {-1, 2, 2, -1, 0, 0} Output: 2 Explanation: Sprinklers at index 2 and 5 can water thefull gallery, span of sprinkler at index 2 = [0,4] and span of sprinkler at index 5 = [5,5]. Example 2: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0} Output: -1 Explanation: No sprinkler can throw water at index 7. Hence all plants cannot be watered. Example 3: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 0, 0, 0, 0, 0} Output: 3 Explanation: Sprinkler at indexes 2, 7 and 8 together can water all plants. Your task: Your task is to complete the function min_sprinklers() which takes the array gallery[ ] and the integer n as input parameters and returns the value to be printed. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ n ≤ 10^{5} gallery[i] ≤ 50
class Solution: def min_sprinklers(self, gallery, n): water = [] for i in range(n): if gallery[i] != -1: water.append((i - gallery[i], i + gallery[i])) if len(water) == 0: return -1 water.sort() ans = 0 left, right = 0, 0 if water[0][0] > right: return -1 else: ans = 1 right = water[0][1] for i in range(1, len(water)): if right >= n - 1: break if water[i][0] <= left: right = max(right, water[i][1]) elif water[i][0] > right + 1: return -1 else: ans += 1 left = right + 1 right = water[i][1] return ans if right >= n - 1 else -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR NUMBER
A gallery with plants is divided into n parts, numbered : 0,1,2,3...n-1. There are provisions for attaching water sprinklers at every partition. A sprinkler with range x at partition i can water all partitions from i-x to i+x. Given an array gallery[ ] consisting of n integers, where gallery[i] is the range of sprinkler at partition i (power==-1 indicates no sprinkler attached), return the minimum number of sprinklers that need to be turned on to water the complete gallery. If there is no possible way to water the full length using the given sprinklers, print -1. Example 1: Input: n = 6 gallery[ ] = {-1, 2, 2, -1, 0, 0} Output: 2 Explanation: Sprinklers at index 2 and 5 can water thefull gallery, span of sprinkler at index 2 = [0,4] and span of sprinkler at index 5 = [5,5]. Example 2: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0} Output: -1 Explanation: No sprinkler can throw water at index 7. Hence all plants cannot be watered. Example 3: Input: n = 9 gallery[ ] = {2, 3, 4, -1, 0, 0, 0, 0, 0} Output: 3 Explanation: Sprinkler at indexes 2, 7 and 8 together can water all plants. Your task: Your task is to complete the function min_sprinklers() which takes the array gallery[ ] and the integer n as input parameters and returns the value to be printed. Expected Time Complexity: O(NlogN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ n ≤ 10^{5} gallery[i] ≤ 50
class Solution: def min_sprinklers(self, gallery, n): target = 0 count = 0 di = [] for i in range(n): if gallery[i] != -1: di.append([i - gallery[i], i + gallery[i]]) di.sort(key=lambda x: x[0]) m = len(di) idx = 0 while target < n and idx < m: next_target = -1 if di[idx][0] > target: return -1 while idx < m and di[idx][0] <= target: next_target = max(next_target, di[idx][1]) idx += 1 target = next_target + 1 count += 1 return count if target >= n else -1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR NUMBER
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
read = lambda: map(int, input().split()) n = int(input()) Max = {} for i in range(n): x, y = read() s = y - x if s not in Max or y > Max[s]: Max[s] = y cur = {i: max(i, 0) for i in Max} ans = [] def no(): print("NO") exit() for i in read(): if i not in cur: no() y = cur[i] f2 = y > Max[i] f3 = i + 1 in cur and cur[i + 1] != y + 1 f4 = i - 1 in cur and cur[i - 1] != y if f2 or f3 or f4: no() ans.append("%d %d" % (y - i, y)) cur[i] = y + 1 print("YES\n" + "\n".join(ans))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING VAR
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
import sys n = int(input()) max_y = {} for i in range(n): x, y = map(int, input().split()) key = y - x if key not in max_y or y > max_y[key]: max_y[key] = y curr_y = {key: max(key, 0) for key in max_y} result = [] def fail(): print("NO") import sys sys.exit() for key in map(int, input().split()): if key not in curr_y: fail() y = curr_y[key] if y > max_y[key]: fail() if key + 1 in curr_y and curr_y[key + 1] != y + 1: fail() if key - 1 in curr_y and curr_y[key - 1] != y: fail() result.append("%d %d" % (y - key, y)) curr_y[key] = y + 1 print("YES") print("\n".join(result))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR STRING IMPORT EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
n = int(input()) opens = {} sums = [(n - i) for i in range(n)] + [0] * (n + 1) for i in range(n): t = tuple(map(int, input().split())) opens[t] = 1 nums = list(map(int, input().split())) res = 1 res_nums = [] for elem in nums: f = 1 x = 0 try: x = sums[elem + n] except: f = 0 y = x + elem try: u = opens[x, y] except KeyError: f = 0 try: if opens[x, y - 1] == 1: f = 0 except KeyError: pass try: if opens[x - 1, y] == 1: f = 0 except KeyError: pass if f == 0: res = 0 break sums[elem + n] += 1 opens[x, y] = 0 res_nums.append((x, y)) print("YES" if res else "NO") if res: for elem in res_nums: print(str(elem[0]) + " " + str(elem[1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING IF VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
n = int(input()) MAX = 100000 coord = [list() for i in range(2 * MAX + 1)] for i in range(n): x, y = map(int, input().split()) coord[y - x - MAX].append((x, y)) w = list(map(int, input().split())) for i in range(2 * MAX + 1): coord[i].sort() ans = [(0, 0) for i in range(n)] possible = True last_x = [-1] * (MAX + 1) last_y = [-1] * (MAX + 1) for i in range(n): if len(coord[w[i] - MAX]) > 0: x = coord[w[i] - MAX][0][0] y = coord[w[i] - MAX][0][1] if last_x[y] == x - 1 and last_y[x] == y - 1: last_x[y] += 1 last_y[x] += 1 ans[i] = x, y coord[w[i] - MAX].pop(0) else: possible = False break else: possible = False break if possible: print("YES") print("\n".join([" ".join(map(str, coords)) for coords in ans])) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
def main(): n = int(input()) diag = [0] * 200005 use = [0] * 200005 for i in range(n): x, y = [int(i) for i in input().split(" ")] c = y - x if c > 0: diag[c] = max(x + 1, diag[c]) else: diag[c] = max(y + 1, diag[c]) table = set() for i in range(100005): table.add((-1, i)) table.add((i, -1)) result = [] for v in [int(i) for i in input().split(" ")]: if v > 0: x = use[v] y = x + v else: y = use[v] x = y - v use[v] += 1 if use[v] > diag[v]: print("NO") return if (x - 1, y) not in table or (x, y - 1) not in table: print("NO") return table.add((x, y)) result.append((x, y)) print("YES") for v in result: print(v[0], v[1]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
n = int(input().rstrip()) l = set() for i in range(n): l.add(tuple(map(int, input().rstrip().split()))) w = list(map(int, input().rstrip().split())) d = {(0): (0, 0)} s = "" visited = set() visited.add((0, 0)) for k in w: if d.get(k, None) is None: print("NO") exit(0) p = d[k] del d[k] s += "{} {}\n".format(*p) if p[1] == 0 and (p[0] + 1, p[1]) in l: visited.add((p[0] + 1, p[1])) d[-p[0] - 1] = p[0] + 1, 0 elif (p[0] + 1, p[1] - 1) in visited and (p[0] + 1, p[1]) in l: visited.add((p[0] + 1, p[1])) d[p[1] - p[0] - 1] = p[0] + 1, p[1] if p[0] == 0 and (0, p[1] + 1) in l: d[p[1] + 1] = 0, p[1] + 1 visited.add((0, p[1] + 1)) elif (p[0] - 1, p[1] + 1) in visited and (p[0], p[1] + 1) in l: visited.add((p[0], p[1] + 1)) d[p[1] - p[0] + 1] = p[0], p[1] + 1 print("YES") print(s, end="")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NONE NONE EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL STRING VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
def __starting_point(): n = int(input()) maxX = [-1] * 100005 for _ in range(n): px, py = [int(x) for x in input().split()] maxX[py] = max(maxX[py], px) w = [int(x) for x in input().split()] p = [-1] * 100005 p[0] = 0 wdict = dict() wdict[0] = 0, 0 res = [] for wi in w: if wi in wdict: px, py = wdict.pop(wi) res.append((px, py)) if maxX[py] > px: wdict[py - (px + 1)] = px + 1, py p[py] += 1 if maxX[py + 1] != -1 and p[py + 1] == -1: wdict[py + 1] = 0, py + 1 p[py + 1] += 1 else: break if len(res) == n: print("YES") for ares in res: print(ares[0], ares[1]) else: print("NO") __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
from sys import stdin _data = iter(stdin.read().split("\n")) input = lambda: next(_data) n = int(input()) ref, front = {}, {} ans = [] max_x, max_y = 0, 0 for _ in range(n): x, y = list(map(int, input().split())) max_x = max(max_x, x) max_y = max(max_y, y) ref[x, y] = 2 if x == 0: ref[x, y] -= 1 if y == 0: ref[x, y] -= 1 if (x, y) == (0, 0): del ref[x, y] front[y - x] = x, y ws = list(map(int, input().split())) for w in ws: if w not in front: ans = [] break x, y = front.pop(w) ans.append((x, y)) for dx, dy in ((1, 0), (0, 1)): nx, ny = x + dx, y + dy if (nx, ny) not in ref: continue ref[nx, ny] -= 1 if ref[nx, ny] == 0: del ref[nx, ny] front[ny - nx] = nx, ny if ans: print("YES") print("\n".join("{} {}".format(x, y) for x, y in ans)) else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER 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 VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
n = int(input()) p = sorted([tuple(map(int, input().split())) for _ in range(n)]) arr = list(map(int, input().split())) w, r, pr = {}, {}, {} for i, wi in enumerate(arr, 1): if wi not in w: w[wi] = [] w[wi].append(i) def is_nbr(nb, i): return 0 if pr.get(nb, 0) > i else 1 def check_nbrs(p, i): n1 = p[0] - 1, p[1] n2 = p[0], p[1] - 1 n3 = p[0] - 1, p[1] - 1 return is_nbr(n1, i) and is_nbr(n2, i) and is_nbr(n3, i) def solve(): for i in range(len(p)): d = p[i][1] - p[i][0] if d not in w: return 0 q = w[d] if not q: return 0 ind = q.pop(0) r[ind] = p[i] pr[p[i]] = ind if not check_nbrs(p[i], ind): return 0 return 1 if solve() == 0: print("NO") else: print("YES") for k, v in sorted(r.items()): print(v[0], v[1])
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 VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR DICT DICT DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w_1, w_2,..., w_{n}, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to w_{i}, that is s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}. Now Wilbur asks you to help him with this challenge. -----Input----- The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with. Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input. The last line of the input contains n integers. The i-th of them is w_{i} ( - 100 000 ≤ w_{i} ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering. -----Output----- If there exists an aesthetically pleasant numbering of points in the set, such that s(x_{i}, y_{i}) = y_{i} - x_{i} = w_{i}, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them. -----Examples----- Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Output YES 0 0 1 0 2 0 0 1 1 1 Input 3 1 0 0 0 2 0 0 1 2 Output NO -----Note----- In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and y_{i} - x_{i} = w_{i}. In the second sample, the special values of the points in the set are 0, - 1, and - 2 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
def binsearch(lofpoints, l, r, w, arr): if l > r: return "None" mid = (l + r) // 2 if lofpoints[mid][0] == w and arr[mid] == 1: if mid == 0: arr[mid] = 0 return mid elif lofpoints[mid - 1][0] != w or arr[mid - 1] == 0: arr[mid] = 0 return mid else: return binsearch(lofpoints, l, mid - 1, w, arr) if lofpoints[mid][0] == w and arr[mid] == 0: return binsearch(lofpoints, mid + 1, r, w, arr) if lofpoints[mid][0] < w: return binsearch(lofpoints, mid + 1, r, w, arr) if lofpoints[mid][0] > w: return binsearch(lofpoints, l, mid - 1, w, arr) n = int(input()) lofpoints = [] for i in range(n): l = input().split() x = int(l[0]) y = int(l[1]) lofpoints.append((y - x, x, y)) lofpoints.sort() w = input().split() wi = [int(i) for i in w] arr = [(1) for i in range(n)] lsol = [] done = 1 for i in range(n): x = binsearch(lofpoints, 0, n - 1, wi[i], arr) if x == "None": done = 0 break elif lsol == []: lsol.append((lofpoints[x][1], lofpoints[x][2])) elif lofpoints[x][1] < lsol[-1][0] and lofpoints[x][2] < lsol[-1][1]: done = 0 break else: lsol.append((lofpoints[x][1], lofpoints[x][2])) if done == 1: hashi = dict() for i in range(n): hashi[lsol[i][0], lsol[i][1]] = i for i in hashi: x = i[0] y = i[1] t = hashi[i] if (x, y + 1) in hashi: if hashi[x, y + 1] < t: done = 0 break if (x + 1, y) in hashi: if hashi[x + 1, y] < t: done = 0 break if done == 0: print("NO") else: print("YES") for i in lsol: print(i[0], i[1]) else: print("NO")
FUNC_DEF IF VAR VAR RETURN STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR STRING ASSIGN VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline maxn = 100005 hell = 1000000007 vis = [0] * 3 * maxn def meowmeow321(): n, m = map(int, input().split()) for i in range(3 * n + 1): vis[i] = 0 elst = [] for i in range(m): x, y = map(int, input().split()) if not vis[x] and not vis[y]: vis[x] = 1 vis[y] = 1 elst.append(i + 1) if len(elst) >= n: print("Matching") for i in range(n): print(elst[i], end=" ") print("") else: print("IndSet") cnt = 0 cur = 1 while cnt < n and cur <= 3 * n: if not vis[cur]: print(cur, end=" ") cnt += 1 cur += 1 print("") t = int(input()) for xxx in range(t): meowmeow321()
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, m = map(int, input().split()) v = [True] * (3 * n + 1) e = [0] * n ptr = 0 for i in range(1, m + 1): a, b = map(int, input().split()) if ptr < n and v[a] and v[b]: e[ptr] = i ptr += 1 v[a] = False v[b] = False if ptr == n: print("Matching") print(*e) else: print("IndSet") cnt = 0 for i in range(1, n * 3 + 1): if v[i]: print(i, end=" ") cnt += 1 if cnt == n: print() break
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
from sys import stdin input = stdin.readline def solve_graph(): n, m = map(int, input().split()) included = [False] + [True] * (3 * n) matching = [] for i in range(1, m + 1): e1, e2 = map(int, input().split()) if included[e1] and included[e2]: matching.append(i) included[e1] = False included[e2] = False if len(matching) >= n: print("Matching") print(*matching[:n]) return intset = [x for x, b in enumerate(included) if b] print("IndSet") print(*list(intset)[:n]) t = int(input()) for _ in range(t): solve_graph()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline T = int(input()) ver = set() for _ in range(T): ver.clear() n, m = map(int, input().split()) n *= 3 edge = [] for __ in range(m): u, v = map(int, input().split()) if u not in ver and v not in ver: edge.append(__ + 1) ver.add(u) ver.add(v) if len(edge) == n // 3: print("Matching") print(*edge[: n // 3]) for x in range(__ + 1, m): input() break if len(edge) < n // 3: asd = [] for x in range(1, n + 1): if x not in ver: asd.append(x) if len(asd) == n // 3: print("IndSet") print(*asd[: n // 3]) break if len(asd) < n // 3: print("Impossible")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys def main(): T = int(input()) v_cover = set() for _ in range(T): n, m = map(int, input().split()) v_count = 3 * n v_cover.clear() i_cover = [] for k in range(m): edge = list(map(int, input().split())) if edge[0] not in v_cover and edge[1] not in v_cover: v_cover.add(edge[0]) v_cover.add(edge[1]) i_cover.append(k + 1) if len(i_cover) == n: print("Matching") print(*i_cover[:n]) for _ in range(k + 1, m): input() break if len(i_cover) < n: v_independent = [] for vert in range(1, 3 * n + 1): if vert not in v_cover: v_independent.append(vert) if len(v_independent) == n: print("IndSet") print(*v_independent) break if len(v_independent) < n: print("Impossible") input = sys.stdin.readline main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
from sys import stdin input = stdin.readline q = int(input()) for query in range(q): n, m = map(int, input().split()) dupa = [0] * (3 * n + 1) edges = [] for i in range(m): u, v = map(int, input().split()) if dupa[u] == 0 and dupa[v] == 0: dupa[u] = 1 dupa[v] = 1 if len(edges) < n: edges.append(i + 1) if len(edges) == n: print("Matching") print(*edges) else: print("IndSet") chuj = 0 for i in range(1, 3 * n + 1): if chuj == n: break if dupa[i] == 0: if chuj < n - 1: print(i, end=" ") else: print(i) chuj += 1
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, m = map(int, input().split()) v = set(range(1, 3 * n + 1)) e = [] for i in range(1, m + 1): a, b = map(int, input().split()) if a in v and b in v: e.append(i) v.remove(a) v.remove(b) if len(e) >= n: print("Matching") print(*e[:n]) else: print("IndSet") print(*list(v)[:n])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
from sys import setrecursionlimit as SRL from sys import stdin SRL(10**7) rd = stdin.readline rrd = lambda: map(int, rd().strip().split()) t = int(input()) while t: n, m = map(int, rd().split()) out = [0] * (3 * n + 1) mans = [] nans = [] for i in range(1, m + 1): u, v = map(int, rd().split()) if not out[u] and not out[v]: out[u] = out[v] = 1 mans.append(i) if len(mans) >= n: print("Matching") print(*mans[:n]) else: print("IndSet") for i in range(1, 1 + 3 * n): if not out[i]: nans.append(i) if len(nans) >= n: break print(*nans) t -= 1
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline def main(): tes = int(input()) for testcase in [0] * tes: n, m = map(int, input().split()) new = [True] * (3 * n) res = [] for i in range(1, m + 1): u, v = map(int, input().split()) if new[u - 1] and new[v - 1]: if len(res) < n: res.append(i) new[u - 1] = new[v - 1] = False if len(res) >= n: print("Matching") print(*res) else: vs = [] for i in range(3 * n): if new[i]: vs.append(i + 1) if len(vs) >= n: break print("IndSet") print(*vs) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, m = map(int, input().split()) edges = [0] * m for i in range(m): edges[i] = tuple(map(int, input().split())) used = [0] * (3 * n) edgess = [] check = 0 for i in range(m): if used[edges[i][0] - 1] == used[edges[i][1] - 1] == 0: edgess.append(i + 1) used[edges[i][0] - 1] = 1 used[edges[i][1] - 1] = 1 check += 1 if check >= n: useful = edgess[:n] edgess = [str(guy) for guy in useful] print("Matching") print(" ".join(edgess)) else: indep = [] for i in range(1, 3 * n + 1): if used[i - 1] == 0: indep.append(i) useful = indep[:n] verts = [str(guy) for guy in useful] print("IndSet") print(" ".join(verts))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): N, M = list(map(int, input().split())) X = [[] for i in range(3 * N)] for i in range(M): x, y = list(map(int, input().split())) x, y = min(x, y), max(x, y) X[x - 1].append((y - 1, i + 1)) MAT = [] IND = [] DONE = [0] * 3 * N for i in range(3 * N): if DONE[i]: continue for j, ind in X[i]: if DONE[j] == 0: MAT.append(ind) DONE[i] = 1 DONE[j] = 1 break else: IND.append(i + 1) if len(MAT) >= N: print("Matching") print(*MAT[:N]) else: print("IndSet") print(*IND[:N])
IMPORT ASSIGN 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 LIST VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR FOR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR
You are given a graph with $3 \cdot n$ vertices and $m$ edges. You are to find a matching of $n$ edges, or an independent set of $n$ vertices. A set of edges is called a matching if no two edges share an endpoint. A set of vertices is called an independent set if no two vertices are connected with an edge. -----Input----- The first line contains a single integer $T \ge 1$ — the number of graphs you need to process. The description of $T$ graphs follows. The first line of description of a single graph contains two integers $n$ and $m$, where $3 \cdot n$ is the number of vertices, and $m$ is the number of edges in the graph ($1 \leq n \leq 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$). Each of the next $m$ lines contains two integers $v_i$ and $u_i$ ($1 \leq v_i, u_i \leq 3 \cdot n$), meaning that there is an edge between vertices $v_i$ and $u_i$. It is guaranteed that there are no self-loops and no multiple edges in the graph. It is guaranteed that the sum of all $n$ over all graphs in a single test does not exceed $10^{5}$, and the sum of all $m$ over all graphs in a single test does not exceed $5 \cdot 10^{5}$. -----Output----- Print your answer for each of the $T$ graphs. Output your answer for a single graph in the following format. If you found a matching of size $n$, on the first line print "Matching" (without quotes), and on the second line print $n$ integers — the indices of the edges in the matching. The edges are numbered from $1$ to $m$ in the input order. If you found an independent set of size $n$, on the first line print "IndSet" (without quotes), and on the second line print $n$ integers — the indices of the vertices in the independent set. If there is no matching and no independent set of the specified size, print "Impossible" (without quotes). You can print edges and vertices in any order. If there are several solutions, print any. In particular, if there are both a matching of size $n$, and an independent set of size $n$, then you should print exactly one of such matchings or exactly one of such independent sets. -----Example----- Input 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 Output Matching 2 IndSet 1 IndSet 2 4 Matching 1 15 -----Note----- The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer. The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly $n$. The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
import sys def main(): it = iter(map(int, sys.stdin.read().split())) t = next(it) for _ in range(t): n = next(it) m = next(it) total_node = 3 * n is_node_covered = [(False) for _ in range(total_node + 1)] is_edge_in_matching = [(False) for _ in range(m + 1)] matching_edge_count = 0 for i in range(1, m + 1): u = next(it) v = next(it) if not is_node_covered[u] and not is_node_covered[v]: is_node_covered[u] = is_node_covered[v] = True is_edge_in_matching[i] = True matching_edge_count += 1 ansL = [] if matching_edge_count >= n: ansL.append("Matching\n") edge_taken = 0 for i in range(1, m + 1): if is_edge_in_matching[i]: ansL.append(str(i) + " ") edge_taken += 1 if edge_taken == n: break else: ansL.append("IndSet\n") node_taken = 0 for i in range(1, total_node + 1): if not is_node_covered[i]: ansL.append(str(i) + " ") node_taken += 1 if node_taken == n: break ansL.append("\n") sys.stdout.write("".join(ansL)) def __starting_point(): main() __starting_point()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR