description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = list(map(int, input().split())) arr = list(map(int, input().split())) def checker(x, cur): ans = 0 for i in range(n): if cur == 0: ans += 1 cur = cur ^ 1 elif arr[i] <= x: ans += 1 cur ^= 1 if ans >= k: return 1 else: return 0 lo = 1 hi = 10**9 while lo < hi: m = (lo + hi) // 2 ans1 = checker(m, 0) ans2 = checker(m, 1) if ans1 == 1 or ans2 == 1: hi = m else: lo = m if hi - lo == 1: break ans1, ans2 = checker(lo, 0), checker(lo, 1) if ans1 == 1 or ans2 == 1: print(lo) else: print(hi)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**19 MOD = 10**9 + 7 def bisearch_min(mn, mx, func): ok = mx ng = mn while ng + 1 < ok: mid = (ok + ng) // 2 if func(mid): ok = mid else: ng = mid return ok def check(m): cnt1 = 0 cnt2 = 0 i = 0 while i < N: if A[i] <= m: cnt1 += 1 i += 1 if i < N: cnt2 += 1 i += 1 else: i += 1 cnt3 = 0 cnt4 = 1 i = 1 while i < N: if A[i] <= m: cnt3 += 1 i += 1 if i < N: cnt4 += 1 i += 1 else: i += 1 return ( cnt1 >= ceil(K, 2) and cnt1 + cnt2 >= K or cnt3 >= K // 2 and cnt3 + cnt4 >= K ) N, K = MAP() A = LIST() res = bisearch_min(0, 10**9 + 1, check) print(res)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().rstrip().split()) l = list(map(int, input().rstrip().split())) s = sorted(l) left = 0 right = len(s) - 1 while left < right: mid = (left + right) // 2 guess = s[mid] separatore = False counter = 0 cons = 0 for el in l: if el > guess: if cons == 0: pass elif cons % 2: counter += cons else: counter += cons - 1 cons = 0 if not separatore: separatore = True counter += 1 elif separatore: separatore = False cons = 1 else: cons += 1 if cons == 0: pass elif cons % 2: counter += cons else: counter += cons - 1 cons = 0 for i in range(n): if l[i] <= guess: cons += 1 else: break if cons > 0 and cons % 2 == 0 and cons < n - 1: counter += 1 cons = 0 for i in range(n - 1, -1, -1): if l[i] <= guess: cons += 1 else: break if cons > 0 and cons % 2 == 0: counter += 1 if counter >= k: right = mid else: left = mid + 1 print(s[left])
ASSIGN 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
ar = [] n = 0 k = 0 def check(x, f): cnt = 0 for i in range(n): if f == 0: cnt += 1 f = 1 elif ar[i] <= x: cnt += 1 f = 0 return cnt >= k inp = input() inp_stream = inp.split() n = int(inp_stream[0]) k = int(inp_stream[1]) inp = input() inp_stream = inp.split() for i in inp_stream: ar.append(int(i)) lo = 1 hi = 1000000007 ans = hi while lo <= hi: mid = (hi + lo) // 2 if check(mid, 0) or check(mid, 1): ans = mid hi = mid - 1 else: lo = mid + 1 print(ans)
ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN 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 NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys input = sys.stdin.readline def done(parity, l, k, req): co = 0 for i in range(len(l)): if parity == 0 and l[i] <= k: co += 1 parity ^= 1 elif parity == 1: parity ^= 1 co += 1 return co >= req n, k = map(int, input().split()) arr = list(map(int, input().split())) l = 1 r = 1000000000 ans = 9999999999999 while l < r: mid = (l + r) // 2 if done(0, arr, mid, k) or done(1, arr, mid, k): r = mid ans = min(ans, mid) else: l = mid + 1 print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def canform1(lst, n, k, x): count = 0 for i in range(n): if count % 2 == 0 and lst[i] <= x: count += 1 elif count % 2 != 0: count += 1 if count >= k: return True else: return False def canform2(lst, n, k, x): count = 0 for i in range(n): if count % 2 != 0 and lst[i] <= x: count += 1 elif count % 2 == 0: count += 1 if count >= k: return True else: return False n, k = map(int, input().split()) lst = list(map(int, input().split())) low, high = min(lst), max(lst) ans = high while low <= high: mid = (low + high) // 2 if canform1(lst, n, k, mid) or canform2(lst, n, k, mid): ans = mid high = mid - 1 else: low = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR 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
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split()) A = list(map(int, input().split())) end = max(A) beg = min(A) while beg < end: mid = (beg + end) // 2 oc = 0 ec = 0 for i in range(n): if oc % 2 == 0 or A[i] <= mid: oc = oc + 1 if ec % 2 != 0 or A[i] <= mid: ec = ec + 1 if ec >= k or oc >= k: end = mid else: beg = mid + 1 print(beg)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
A = input().split(" ") n = int(A[0]) k = int(A[1]) a = input().split(" ") for i in range(n): a[i] = int(a[i]) r = 1000000000 l = 0 even = 0 odd = 0 while r - l > 1: even = 0 odd = 0 mid = int((r + l) // 2) kk = 1 for j in range(n): if kk == 1: even += 1 kk = 2 elif a[j] <= mid: even += 1 kk = 1 kk = 1 for j in range(n): if kk == 2: odd += 1 kk = 1 elif a[j] <= mid: odd += 1 kk = 2 if odd >= k or even >= k: r = mid else: l = mid print(r)
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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split()) ls = list(map(int, input().split())) lo = 1 hi = max(ls) def solve(num): ln = 0 for i in range(n): if ln % 2: ln += 1 elif ls[i] <= num: ln += 1 if ln == k: return 1 ln = 0 for i in range(n): if ln % 2 == 0: ln += 1 elif ls[i] <= num: ln += 1 if ln == k: return 1 return 0 res = 1 while lo <= hi: mid = (lo + hi) // 2 if solve(mid): res = mid hi = mid - 1 else: lo = mid + 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR 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 EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys input = sys.stdin.buffer.readline n, k = map(int, input().split()) ls = list(map(int, input().split())) def check(mx): for open in range(2): cc = 0 for u in ls: if u <= mx and open: cc += 1 open = 0 elif not open: cc += 1 open = 1 if cc >= k: return True return False l, r = 1, max(ls) + 1 while l < r: m = (l + r) // 2 if check(m): r = m else: l = m + 1 print(l)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(x, fl): global n, a, k cur = [] for i in range(n): if (len(cur) + 1) % 2 == fl and a[i] <= x: cur += [a[i]] elif (len(cur) + 1) % 2 != fl: cur += [a[i]] return len(cur) >= k def Bin(): l = 1 r = int(1000000000.0) while l < r: mid = (l + r) // 2 if check(mid, 0) or check(mid, 1): r = mid else: l = mid + 1 return l n, k = map(int, input().split()) a = list(map(int, input().split())) print(Bin())
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR LIST VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR LIST VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def is_poss(x): count = 0 odd = True for ai in a: if odd: if ai <= x: count += 1 odd = False else: count += 1 odd = True if count == k: break if count == k: return True count = 0 even = False for ai in a: if even: if ai <= x: count += 1 even = False else: count += 1 even = True if count == k: break return count == k n, k = [int(i) for i in input().strip().split()] a = [int(i) for i in input().strip().split()] lo = min(a) hi = max(a) while lo <= hi: mid = (lo + hi) // 2 if is_poss(mid): ans = mid hi = mid - 1 else: lo = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
rr = lambda: input() rri = lambda: int(input()) rrm = lambda: list(map(int, input().split())) INF = float("inf") def check(val, cur, A): ans = 0 for v in A: if cur: ans += 1 cur ^= 1 elif v <= val: ans += 1 cur ^= 1 return ans >= k def solve(N, K, A): lo, hi = 1, 10**9 while lo <= hi: mid = lo + hi >> 1 if check(mid, 0, A) or check(mid, 1, A): hi = mid - 1 else: lo = mid + 1 return lo n, k = rrm() arr = rrm() print(solve(n, k, arr))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def find(s, num, arr, k): l = 0 for i in range(len(arr)): if s == 1 or arr[i] <= num: s = 1 - s l += 1 return l >= k n, k = map(int, input().split()) arr = list(map(int, input().split())) l, r = 1, max(arr) while l < r: mid = (l + r) // 2 if find(0, mid, arr, k) or find(1, mid, arr, k): r = mid else: l = mid + 1 print(l)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys n, k = 0, 0 ans = 1000000000 a = [] def ok(mid): for i in [0, 1]: l = 0 for val in a: if l % 2 != i or val <= mid: l += 1 if l >= k: return True return False def solve(): global n, k global a global ans n, k = map(int, input().split()) a = list(map(int, input().split())) lo = 1 hi = max(a[0:]) while lo <= hi: mid = int((lo + hi) / 2) if ok(mid): ans = mid hi = mid - 1 else: lo = mid + 1 print(ans) def main(): solve() main()
IMPORT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF FOR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN 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 ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split(" ")) a = list(map(int, input().split(" "))) a_sorted = sorted(a) def check(minn, ans): array = [0] for i in a: if len(array) % 2 == 0: array.append(i) elif i <= minn: array.append(i) if len(array) - 1 >= ans: return True array = [0] for i in a: if len(array) % 2 == 1: array.append(i) elif i <= minn: array.append(i) if len(array) - 1 >= ans: return True return False first = 0 last = len(a_sorted) - 1 while last > first: middle = (last + first) // 2 if check(a_sorted[middle], k) == True: last = middle - 1 else: first = middle + 1 if check(a_sorted[0], k) == True: print(a_sorted[0]) elif check(a_sorted[last], k) == True: print(a_sorted[last]) else: print(a_sorted[last + 1])
ASSIGN 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 FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = input().split() n = int(n) k = int(k) a = list(map(int, input().split())) b = sorted(list(set(a))) def check(x, flag): l = 0 for i in range(len(a)): if flag or a[i] <= b[x]: l += 1 flag ^= 1 return l >= k l = -1 r = len(b) while l + 1 < r: mid = l + r >> 1 if check(mid, 0) or check(mid, 1): r = mid else: l = mid print(b[r])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import maxsize n, k = map(int, input().split()) ls = list(map(int, input().split())) ar = sorted(ls) mx = max(ls) low = 0 high = n - 1 ans = maxsize while low <= high: mid = (low + high) // 2 f = False for turn in range(2): curr_t = turn took = 0 for i in range(n): if not curr_t or ls[i] <= ar[mid]: took += 1 curr_t = 1 - curr_t if took >= k: f = True break if f: ans = ar[mid] high = mid - 1 else: low = mid + 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + "\n") def wi(n): sys.stdout.write(str(n) + "\n") def wia(a): sys.stdout.write(" ".join([str(x) for x in a]) + "\n") def can(n, k, a, x): m1 = [] for i in range(n): if len(m1) % 2 == 0: m1.append(a[i]) elif a[i] <= x: m1.append(a[i]) m2 = [] for i in range(n): if len(m2) % 2 == 1: m2.append(a[i]) elif a[i] <= x: m2.append(a[i]) return len(m1) >= k or len(m2) >= k def solve(n, k, a): lo = 1 hi = max(a) while hi > lo: mid = (lo + hi) // 2 if can(n, k, a, mid): hi = mid else: lo = mid + 1 return hi def main(): n, k = ria() a = ria() wi(solve(n, k, a)) main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(x): ct1, ct2 = 0, 0 flg = 1 for i in range(n): if ar[i] <= x and flg: flg = 0 ct1 += 1 elif not flg: flg = 1 ct1 += 1 flg = 0 for i in range(n): if ar[i] <= x and flg: flg = 0 ct2 += 1 elif not flg: flg = 1 ct2 += 1 return ct1 >= k or ct2 >= k def src(l, r): while l < r: mid = (l + r) // 2 if check(mid): r = mid else: l = mid + 1 return l n, k = map(int, input().split()) ar = list(map(int, input().split())) r, l = max(ar), min(ar) print(src(l, r))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF 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 RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(x, flag) -> bool: l = 0 for i in range(len(a)): if flag or a[i] <= x: l += 1 flag ^= 1 return l >= k n, k = input().split() n = int(n) k = int(k) a = [int(n) for n in input().split()] l = 0 r = 1000000007 while l + 1 < r: mid = l + r >> 1 if check(mid, 0) or check(mid, 1): r = mid else: l = mid print(r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(m, f, l1, k): cnt = 0 for i in range(len(l1)): if l1[i] <= m or cnt % 2 == f: cnt += 1 if cnt >= k: ans = 1 else: ans = 0 return ans n, k = map(int, input().split()) l1 = list(map(int, input().split())) l = 1 h = 1000000000.0 + 7 while l < h: m = int((l + h) // 2) if check(m, 0, l1, k) == 1 or check(m, 1, l1, k) == 1: h = m else: l = m + 1 print(l)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def find(arr, x, start, k, n): done = start curr = 0 for i in range(start, n): if curr == 0: if arr[i] <= x: done += 1 curr = 1 - curr else: done += 1 curr = 1 - curr if done >= k: return True else: return False def binarysearch(arr, n, k): start = 1 end = 1000000000 last = 0 while start <= end: mid = (start + end) // 2 if find(arr, mid, 0, k, n) or find(arr, mid, 1, k, n): last = mid end = mid - 1 else: start = mid + 1 return last def help(): n, k = map(int, input().split(" ")) arr = list(map(int, input().split(" "))) ans = binarysearch(arr, n, k) print(ans) help()
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN 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 FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
N, K = map(int, input().split()) (*A,) = map(int, input().split()) def check(A, x, s): cnt = s i = s while i < N: if A[i] <= x: cnt += 1 if i == N - 1 else 2 i += 2 continue i += 1 if cnt >= K: return True else: return False L = 0 R = 10**9 while L + 1 < R: mid = (L + R) // 2 if check(A, mid, 0) or check(A, mid, 1): R = mid else: L = mid print(L + 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
global n, k def f(x): cc = 0 for i in range(n): if cc == k: break if cc % 2 != 1 and a[i] <= x: cc += 1 elif cc % 2 == 1: cc += 1 if cc == k: return True cc = 0 for i in range(n): if cc == k: break if cc % 2 == 1 and a[i] <= x: cc += 1 elif cc % 2 == 0: cc += 1 if cc == k: return True return False n, k = map(int, input().split()) a = [int(o) for o in input().split()] a1 = sorted(a) hi = n - 1 low = 0 while low <= hi: mid = (hi + low) // 2 if f(a1[mid]): hi = mid - 1 else: low = mid + 1 print(a1[low])
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, m = map(int, input().split()) arr = list(map(int, input().split())) left = 1 right = 1000000001 while abs(left - right) > 1: mid = (right + left) // 2 k = 0 f = False for i in range(n): if f: k += 1 f = False continue if arr[i] <= mid: k += 1 f = True continue if k >= m: right = mid else: left = mid mid = left k = 0 f = False for i in range(n): if f: k += 1 f = False continue if arr[i] <= mid: k += 1 f = True continue if k < m: ans = right else: ans = left k = 0 left = 1 right = 1000000001 while abs(left - right) > 1: mid = (right + left) // 2 k = 0 f = False k += 1 for i in range(1, n): if f: k += 1 f = False continue if arr[i] <= mid: k += 1 f = True continue if k >= m: right = mid else: left = mid mid = left k = 0 f = False k += 1 for i in range(1, n): if f: k += 1 f = False continue if arr[i] <= mid: k += 1 f = True continue if k < m: ans = min(ans, right) else: ans = min(ans, left) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def w(n): return n // 2 + n % 2 def main(): n, k = map(int, input().split(" ")) a = list(map(int, input().split(" "))) e = list(sorted(enumerate(a), key=lambda x: x[1])) d = 0 b = [False] * n for i in range(n): idx, val = e[i] left = 0 while idx - left - 1 >= 0 and b[idx - left - 1]: left += 1 right = 0 while idx + right + 1 < n and b[idx + right + 1]: right += 1 d += w(left + 1 + right) - w(left) - w(right) b[idx] = True if d >= k // 2: if d >= k // 2 + 1: return val first = 0 while b[first]: first += 1 last = 0 while b[-last - 1]: last += 1 if k % 2 == 0: if first % 2 == 0 or last % 2 == 0: return val elif first % 2 == 0 and last % 2 == 0: return val print(main())
FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import stdin n, k = map(int, stdin.readline().split()) arr = list(map(int, stdin.readline().split())) if k % 2 == 0: lo = min(arr) hi = max(arr) while lo <= hi: fl = 0 mid = (lo + hi) // 2 co = co1 = 0 fll = 0 fll1 = 1 aa = 0 for ind, val in enumerate(arr): vis = 0 if val <= mid and not fll: co += 1 vis += 1 if ind == 0 or ind == n - 1: aa += 0.5 if val <= mid and not fll1: co1 += 1 vis += 2 if vis == 0: fll = fll1 = 0 elif vis == 2: fll = 0 fll1 = 1 elif vis == 1: fll = 1 fll1 = 0 elif vis == 3: fll = fll1 = 1 if co >= k // 2 + int(aa) or co1 >= k // 2: hi = mid - 1 else: fl = 1 lo = mid + 1 print(mid + fl) else: lo = min(arr) hi = max(arr) while lo <= hi: fl = 0 mid = (lo + hi) // 2 co = co1 = 0 fll = 0 fll1 = 1 aa = 0 bb = 0 for ind, val in enumerate(arr): vis = 0 if val <= mid and not fll: co += 1 vis += 1 if ind == n - 1 or ind == 0: aa = 1 if val <= mid and not fll1: co1 += 1 vis += 2 if ind == n - 1: bb = 1 if vis == 0: fll = fll1 = 0 if vis == 2: fll = 0 fll1 = 1 elif vis == 1: fll = 1 fll1 = 0 elif vis == 3: fll = fll1 = 1 if co >= k // 2 + aa or co1 >= k // 2 + bb: hi = mid - 1 else: fl = 1 lo = mid + 1 print(mid + fl)
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 IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def main(): n, k = map(int, input().split()) a = list(map(int, input().split())) l = 0 r = 1000000005 while l < r - 1: mid = (l + r) // 2 count = 0 flag = a[0] <= mid if flag: count += 1 for i in range(1, n): if flag: count += 1 flag = False elif a[i] <= mid: count += 1 flag = True if count >= k: r = mid continue flag = False count = 1 for i in range(1, n): if flag: count += 1 flag = False elif a[i] <= mid: count += 1 flag = True if count >= k: r = mid else: l = mid print(r) return main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys input = lambda: sys.stdin.buffer.readline().rstrip() n, k = map(int, input().split()) a = list(map(int, input().split())) def z(s, m): c = 0 for i in range(n): if s or a[i] <= m: s ^= 1 c += 1 if c >= k: break return c >= k q = 1 w = max(a) while q < w: m = (q + w) // 2 if z(0, m) or z(1, m): w = m else: q = m + 1 print(q)
IMPORT ASSIGN VAR FUNC_CALL 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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import stdin input = stdin.readline N, K = [int(x) for x in input().split()] arr = list(map(int, input().split())) sizes = [(K + 1) // 2, K // 2] start = [0, 1] low, high, ans = 1, 10**9, 10**9 while low <= high: mid = (low + high) // 2 v = [] for i in range(0, N): if arr[i] <= mid: v.append(i) works = False for x in range(0, 2): lst = start[x] - 1 ptr = 0 bad = False for i in range(0, sizes[x]): while ptr < len(v) and v[ptr] <= lst: ptr += 1 if ptr == len(v): bad = True break lst = v[ptr] + 1 if lst == N and x == K % 2: bad = True if not bad: works = True break if works: ans = mid high = mid - 1 else: low = mid + 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split()) a = list(map(int, input().split())) l, r = 1, 2 * 10**9 odd = (k + 1) // 2 even = k // 2 ans = r while l < r: mid = l + r >> 1 ok = 0 tempk, i = odd, 0 nn = n nn -= k % 2 == 0 while i < nn: if a[i] <= mid: tempk -= 1 i += 1 i += 1 ok |= tempk <= 0 tempk, i, nn = even, 1, n nn -= k & 1 while i < nn: if a[i] <= mid: tempk -= 1 i += 1 i += 1 ok |= tempk <= 0 if ok: r = mid ans = min(ans, mid) else: l = mid + 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
t = 1 def fun_odd(num, a): cnt = 0 for i in a: if cnt % 2 == 0: if i <= num: cnt += 1 else: continue else: cnt += 1 return cnt def fun_ev(num, a): cnt = 0 for i in a: if cnt % 2 == 1: if i <= num: cnt += 1 else: continue else: cnt += 1 return cnt def fun(x, arr): t1 = fun_odd(x, arr) t2 = fun_ev(x, arr) if t1 >= k or t2 >= k: return True return False for _ in range(t): n, k = list(map(int, input().rstrip().split())) a = list(map(int, input().rstrip().split())) l = min(a) - 1 r = max(a) + 1 while l <= r: mid = (l + r) // 2 if fun(mid, a): r = mid - 1 else: l = mid + 1 print(l)
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR 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 BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def ki(i): c = 0 f = 0 for j, x in enumerate(X): if j == N - 1 and K % 2 == 0: continue if x <= i and not f: c += 1 f = 1 if c >= (K + 1) // 2: return True continue f = 0 return False def gu(i): c = 0 f = 0 for j, x in enumerate(X): if j == 0: continue if j == N - 1 and K % 2: continue if x <= i and not f: c += 1 f = 1 if c >= K // 2: return True continue f = 0 return False def f(i): return ki(i) or gu(i) def bsearch(mn, mx, func): idx = (mx + mn) // 2 while mx - mn > 1: if func(idx): idx, mx = (idx + mn) // 2, idx continue idx, mn = (idx + mx) // 2, idx return idx N, K = map(int, input().split()) X = list(map(int, input().split())) i = bsearch(min(X) - 1, max(X), f) print(i + 1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split()) a = list(map(int, input().split())) l = 0 r = max(a) + 1 while r - l > 1: mid = (r + l) // 2 turn = 1 cnt = 0 for i in range(n): if turn == 0: turn = 1 - turn cnt += 1 elif a[i] <= mid: cnt += 1 turn = 1 - turn if cnt >= k: r = mid continue turn = 0 cnt = 0 for i in range(n): if turn == 0: turn = 1 - turn cnt += 1 elif a[i] <= mid: cnt += 1 turn = 1 - turn if cnt >= k: r = mid else: l = mid print(r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import stdin, stdout n, k = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) def odd_even_subsequence(n, k, a): res1 = 10000000000 res2 = 10000000000 if k % 2 == 0: v = a.pop(-1) res1 = subsequence(n, k // 2 + k % 2, a) a.append(v) else: res1 = subsequence(n, k // 2 + k % 2, a) if k % 2 == 0: a.pop(0) res2 = subsequence(n, k // 2, a) else: a.pop(0) a.pop(-1) res2 = subsequence(n, k // 2, a) return min(res1, res2) def subsequence(n, k, a): l = 0 h = 10**9 while l < h: m = (l + h) // 2 if calc(a, m, k): h = m else: l = m + 1 return h def calc(a, v, k): pre1 = 0 pre2 = 0 for i in range(len(a)): cur = pre1 if a[i] <= v: cur = max(1 + pre2, pre1) pre2 = pre1 pre1 = cur return pre1 >= k print(str(odd_even_subsequence(n, k, a)))
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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def checker(mid, start): odd = False even = False if start == 1: odd = True else: even = True count, ind, ok = 0, start, False for i in range(n): if odd == True: if count % 2 == 0: if a[i] <= mid: count += 1 else: count += 1 if even == True: if count % 2 == 1: if a[i] <= mid: count += 1 else: count += 1 if count >= k: return True return False n, k = map(int, input().split()) a = [int(i) for i in input().split()] lo = 0 hi = 10**9 while lo < hi - 1: mid = (lo + hi) // 2 if checker(mid, 1) or checker(mid, 0): hi = mid else: lo = mid print(hi)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n, k = map(int, input().split()) arr = [int(x) for x in input().split()] def check(mid, arr, k, parity): ans = 0 for item in arr: if ans + 1 & 1 == parity: if item <= mid: ans += 1 else: ans += 1 return ans >= k def bsearch(arr, k): low, high = min(arr), max(arr) while low < high: mid = (low + high) // 2 if check(mid, arr, k, 0) or check(mid, arr, k, 1): high = mid else: low = mid + 1 return low print(bsearch(arr, k))
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def binarysearchodd(n, mid, k, arr): c = 0 p = 1 for i in range(n): if p % 2 == 1 and arr[i] <= mid: p += 1 c += 1 elif p % 2 == 0: p += 1 c += 1 return c def binarysearcheven(n, mid, k, arr): c = 0 p = 1 for i in range(n): if p % 2 == 0 and arr[i] <= mid: p += 1 c += 1 elif p % 2 == 1: p += 1 c += 1 return c n, k = map(int, input().split()) arr = [int(i) for i in input().split()] l = 1 r = max(arr) ans1 = r while l <= r: mid = (l + r) // 2 if binarysearchodd(n, mid, k, arr) >= k: ans1 = mid r = mid - 1 elif binarysearcheven(n, mid, k, arr) >= k: ans1 = mid r = mid - 1 else: l = mid + 1 print(ans1)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR 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 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
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(m): ans = [] for i in range(n): if len(ans) % 2 == 0 and len(ans) < k: if A[i] <= m: ans.append(A[i]) elif len(ans) % 2 == 1 and len(ans) < k: ans.append(A[i]) if len(ans) == k: return 1 else: ans = [] for i in range(n): if len(ans) % 2 == 1 and len(ans) < k: if A[i] <= m: ans.append(A[i]) elif len(ans) % 2 == 0 and len(ans) < k: ans.append(A[i]) if len(ans) == k: return 1 return 0 n, k = map(int, input().split()) A = list(map(int, input().split())) high = 10**9 low = 1 ans = 10**9 while low <= high: m = (high + low) // 2 if check(m): ans = m high = m - 1 else: low = m + 1 print(ans)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP 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 EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n, k = map(int, input().split()) arr = [int(x) for x in input().split()] low = min(arr) high = max(arr) def check(mid): subseq1, subseq2 = [], [] for item in arr: if len(subseq1) + 1 & 1: if item <= mid: subseq1.append(item) else: subseq1.append(item) for item in arr: if len(subseq2) + 1 & 1: subseq2.append(item) elif item <= mid: subseq2.append(item) return len(subseq1) >= k or len(subseq2) >= k while low < high: mid = (low + high) // 2 if check(mid): high = mid else: low = mid + 1 print(low)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR 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 EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys input = sys.stdin.readline n, k = map(int, input().split()) A = list(map(int, input().split())) OK = 10**9 NG = 0 while OK - NG > 1: mid = (OK + NG) // 2 B = [] for a in A: if a <= mid: B.append(0) else: B.append(1) if k % 2 == 1: MIN = k // 2 + 1 ind = 0 ANS = 0 while ind < n: if B[ind] == 0: ANS += 1 ind += 2 else: ind += 1 if ANS >= MIN: OK = mid continue MIN = k // 2 if B[0] == 0: B[0] = 1 if B[-1] == 0: B[-1] = 1 ind = 0 ANS = 0 while ind < n: if B[ind] == 0: ANS += 1 ind += 2 else: ind += 1 if ANS >= MIN: OK = mid continue else: NG = mid else: MIN = k // 2 ANS = 0 ind = 0 flag = 0 while ind < n: if B[ind] == 0: if ind == n - 1: flag = 1 ANS += 1 ind += 2 else: ind += 1 ANS = 0 ind = n - 1 flag2 = 0 while ind >= 0: if B[ind] == 0: if ind == 0: flag2 = 1 ANS += 1 ind -= 2 else: ind -= 1 if ANS == MIN and flag and flag2: NG = mid elif ANS >= MIN: OK = mid else: NG = mid print(OK)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, k = map(int, input().split()) a = list(map(int, input().split())) def check(x: int, s: int): global n, k, a res = s up = (k + (1 if s == 0 else 0)) // 2 i = s tag = 1 while i < n: if a[i] <= x and tag == 1: tag = 0 res += 1 elif tag == 0: tag = 1 res += 1 i += 1 if res >= k: return True else: return False l, r = 0, 10**9 + 7 while l + 1 < r: mid = (l + r) // 2 if check(mid, 0) or check(mid, 1): r = mid else: l = mid print(r)
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 FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
n, m = map(int, input().split()) l = list(map(int, input().split())) le = 0 re = 10**9 def solve(val): i = 0 j = 0 for ii in range(n): if i % 2 == 1 and l[ii] <= val: i += 1 elif i % 2 == 0: i += 1 if j % 2 == 0 and l[ii] <= val: j += 1 elif j % 2: j += 1 if i >= m or j >= m: return True return False while re - le >= 1: mid = le + (re - le) // 2 if solve(mid): ans = mid re = mid else: le = mid + 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import stdin, stdout input = stdin.readline def isValid(v, b, mid, k): sub = 0 for x in v: if b == 0: sub += 1 b = 1 elif x <= mid: b = 0 sub += 1 return sub >= k def solve(cs): x = input() n, m = list(map(int, x.split())) v = list(map(int, input().strip().split(" "))) lo = 1 hi = 1000000000 ans = -1 while lo <= hi: mid = lo + hi >> 1 if isValid(v, 0, mid, m) or isValid(v, 1, mid, m): hi = mid - 1 ans = mid else: lo = mid + 1 print(ans) for tc in range(1): solve(tc + 1)
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def check(x, cur): ans = 0 for i in range(n): if cur == 0: ans += 1 cur ^= 1 elif ar[i] <= x: ans += 1 cur ^= 1 if ans >= k: return True else: return False def binary(lo, hi): while lo < hi: mid = (lo + hi) // 2 if check(mid, 0) or check(mid, 1): hi = mid else: lo = mid + 1 return lo def solve(n, k, ar): a = binary(1, int(1000000000.0)) return a n, k = map(int, input().split()) ar = list(map(int, input().split())) ans = solve(n, k, ar) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
def is_possible(a, value, is_odd, k): seq_len = 0 looking_for_x = is_odd for el in a: if not looking_for_x or looking_for_x and el <= value: seq_len += 1 looking_for_x = not looking_for_x return seq_len >= k def find_max(a, is_odd, k): current = 1 count = max(a) + 1 while count > 0: step = count // 2 test = current + step if is_possible(a, test, is_odd, k): count = step else: current = test + 1 count -= step + 1 return current def solve(): n, k = list(map(int, input().split())) a = list(map(int, input().split())) return min(find_max(a, 0, k), find_max(a, 1, k)) def main(): t = 1 for _ in range(t): print(solve()) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
from sys import stdin input = stdin.readline def check(x, pos): length = 0 for i in range(n): if pos: if a[i] <= x: length += 1 pos ^= 1 else: length += 1 pos ^= 1 if length >= k: return True else: return False def answer(): l, h = 0, 10**9 ans = 0 while l <= h: x = (l + h) // 2 if check(x, 0) or check(x, 1): h = x - 1 else: l = x + 1 ans = l return ans for T in range(1): n, k = map(int, input().split()) a = list(map(int, input().split())) print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Ashish has an array $a$ of size $n$. A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$. Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$. For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$. Help him find the minimum cost of a subsequence of size $k$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β€” the size of the array $a$ and the size of the subsequence. The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β€” the elements of the array $a$. -----Output----- Output a single integer Β β€” the minimum cost of a subsequence of size $k$. -----Examples----- Input 4 2 1 2 3 4 Output 1 Input 4 3 1 2 3 4 Output 2 Input 5 3 5 3 4 2 6 Output 2 Input 6 4 5 3 50 2 4 5 Output 3 -----Note----- In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$. In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$. In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
import sys input = sys.stdin.buffer.readline def solution(): n, k = map(int, input().split()) l = list(map(int, input().split())) beg = 1 end = 10**9 ans = 2e19 while beg <= end: mid = (beg + end) // 2 l1 = 0 for i in range(n): if l1 % 2 == 1 and l[i] <= mid: l1 += 1 elif l1 % 2 == 0: l1 += 1 l2 = 0 for i in range(n): if l2 % 2 == 0 and l[i] <= mid: l2 += 1 elif l2 % 2 == 1: l2 += 1 if max(l2, l1) >= k: ans = min(ans, mid) end = mid - 1 else: beg = mid + 1 print(ans) solution()
IMPORT ASSIGN VAR 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 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 NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR 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 EXPR FUNC_CALL VAR
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass! On the field, there is a row of n units of grass, each with a sweetness s_i. Farmer John has m cows, each with a favorite sweetness f_i and a hunger value h_i. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: * The cows from the left and right side will take turns feeding in an order decided by Farmer John. * When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats h_i units. * The moment a cow eats h_i units, it will fall asleep there, preventing further cows from passing it from both directions. * If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo 10^9+7)? The order in which FJ sends the cows does not matter as long as no cows get upset. Input The first line contains two integers n and m (1 ≀ n ≀ 5000, 1 ≀ m ≀ 5000) β€” the number of units of grass and the number of cows. The second line contains n integers s_1, s_2, …, s_n (1 ≀ s_i ≀ n) β€” the sweetness values of the grass. The i-th of the following m lines contains two integers f_i and h_i (1 ≀ f_i, h_i ≀ n) β€” the favorite sweetness and hunger value of the i-th cow. No two cows have the same hunger and favorite sweetness simultaneously. Output Output two integers β€” the maximum number of sleeping cows that can result and the number of ways modulo 10^9+7. Examples Input 5 2 1 1 1 1 1 1 2 1 3 Output 2 2 Input 5 2 1 1 1 1 1 1 2 1 4 Output 1 4 Input 3 2 2 3 2 3 1 2 1 Output 2 4 Input 5 1 1 1 1 1 1 2 5 Output 0 1 Note In the first example, FJ can line up the cows as follows to achieve 2 sleeping cows: * Cow 1 is lined up on the left side and cow 2 is lined up on the right side. * Cow 2 is lined up on the left side and cow 1 is lined up on the right side. In the second example, FJ can line up the cows as follows to achieve 1 sleeping cow: * Cow 1 is lined up on the left side. * Cow 2 is lined up on the left side. * Cow 1 is lined up on the right side. * Cow 2 is lined up on the right side. In the third example, FJ can line up the cows as follows to achieve 2 sleeping cows: * Cow 1 and 2 are lined up on the left side. * Cow 1 and 2 are lined up on the right side. * Cow 1 is lined up on the left side and cow 2 is lined up on the right side. * Cow 1 is lined up on the right side and cow 2 is lined up on the left side. In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.
import sys from itertools import accumulate input = sys.stdin.readline mod = 10**9 + 7 n, m = map(int, input().split()) G = list(map(int, input().split())) CP = [([0] * (n + 1)) for i in range(n + 1)] for i in range(m): f, e = map(int, input().split()) CP[f][e] += 1 SUMCP = [] for i in range(n + 1): SUMCP.append(list(accumulate(CP[i]))) SUM = [0] * (n + 1) LAST = [] for g in G: LAST.append(g) SUM[g] += 1 MAX = 0 ANS = 0 PLUS = 1 for j in range(n + 1): if SUM[j] > 0 and SUMCP[j][SUM[j]] > 0: PLUS = PLUS * SUMCP[j][SUM[j]] % mod MAX += 1 ANS = PLUS MAX0 = MAX def PLUSVALUE(j): PLUS = 1 MAXC = 0 if cr[j] >= num_g: if SUMCP[j][cr[j]] > 1: MAXC += 1 PLUS = PLUS * (SUMCP[j][cr[j]] - 1) % mod elif SUMCP[j][cr[j]] >= 1: MAXC += 1 PLUS = PLUS * SUMCP[j][cr[j]] % mod return PLUS, MAXC def OPLUSVALUE(j): PLUS = 1 MAXC = 0 x, y = LEFT[j], cr[j] if x > y: x, y = y, x if SUMCP[j][x] == SUMCP[j][y] == 1: MAXC += 1 PLUS = PLUS * 2 % mod elif SUMCP[j][x] >= 1: MAXC += 2 PLUS = PLUS * SUMCP[j][x] * (SUMCP[j][y] - 1) % mod elif SUMCP[j][y] >= 1: MAXC += 1 PLUS = PLUS * SUMCP[j][y] % mod return PLUS, MAXC LEFT = [0] * (n + 1) g = LAST[0] LEFT[g] += 1 num_g = LEFT[g] PLUS = CP[g][num_g] OPLUS = 1 if CP[g][num_g] == 0: flag = 0 MAXC = 0 else: flag = 1 MAXC = 1 cr = SUM cr[g] -= 1 for j in range(n + 1): if j == g: v, p_m = PLUSVALUE(g) PLUS = PLUS * v % mod MAXC += p_m else: v, m = OPLUSVALUE(j) OPLUS = OPLUS * v % mod MAXC += m if MAXC > MAX and flag == 1: MAX = MAXC ANS = PLUS * OPLUS % mod elif MAXC == MAX and flag == 1: ANS += PLUS * OPLUS % mod if flag == 1: MAXC -= 1 + p_m else: MAXC -= p_m for i in range(1, n): g = LAST[i] past_g = LAST[i - 1] v0, m0 = OPLUSVALUE(past_g) v2, m2 = OPLUSVALUE(g) OPLUS = OPLUS * v0 * pow(v2, mod - 2, mod) % mod MAXC += m0 - m2 LEFT[g] += 1 num_g = LEFT[g] cr[g] -= 1 if CP[g][num_g] == 0: continue else: PLUS = CP[g][num_g] MAXC += 1 v, p_m = PLUSVALUE(g) PLUS = PLUS * v % mod MAXC += p_m if MAXC > MAX: MAX = MAXC ANS = PLUS * OPLUS % mod elif MAXC == MAX: ANS += PLUS * OPLUS % mod MAXC -= 1 + p_m print(MAX, ANS % mod)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
You are given a tree with $N$ vertices (numbered $1$ through $N$) and a sequence of integers $A_1, A_2, \ldots, A_N$. You may choose an arbitrary permutation $p_1, p_2, \ldots, p_N$ of the integers $1$ through $N$. Then, for each vertex $i$, you should assign the value $A_{p_i}$ to this vertex. The profit of a path between two vertices $u$ and $v$ is the sum of the values assigned to the vertices on that path (including $u$ and $v$). Let's consider only (undirected) paths that start at a leaf and end at a different leaf. Calculate the maximum possible value of the sum of profits of all such paths. Since this value could be very large, compute it modulo $10^9 + 7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. - Each of the following $Nβˆ’1$ lines contains two space-separated integers $u$ and $v$ denoting that vertices $u$ and $v$ are connected by an edge. -----Output----- For each test case, print a single line containing one integer β€” the maximum sum of profits, modulo $10^9 + 7$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 300,000$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 2 4 1 2 3 4 1 2 2 3 2 4 5 1 2 3 4 5 1 2 2 3 3 4 4 5 -----Example Output----- 24 15 -----Explanation----- Example case 1: $(1, 4, 2, 3)$ is one of the possible permutations that give the optimal answer. Then, the profits of paths between pairs of vertices $(1, 3)$, $(1, 4)$ and $(3, 4)$ are $7$, $8$ and $9$ respectively. Example case 2: Here, any permutation could be chosen.
T = int(input()) M = 10**9 + 7 for _ in range(T): N = int(input()) A = list(map(int, input().split())) if N == 1: print(0) continue B = {} C = {} for i in range(N - 1): u, v = input().split() u = int(u) - 1 v = int(v) - 1 if u not in B: B[u] = [] if v not in B: B[v] = [] B[u].append(v) B[v].append(u) total_leaves = 0 for i in B: if len(B[i]) == 1: total_leaves += 1 S = [0] visited = [False] * N parent = [-1] * N total_visits = [0] * N while len(S) > 0: current = S.pop(len(S) - 1) if visited[current]: p = parent[current] if p != -1: total_visits[p] += total_visits[current] if p not in C: C[p] = {} C[p][current] = total_visits[current] if current not in C: C[current] = {} C[current][p] = total_leaves - C[p][current] else: S.append(current) visited[current] = True for i, j in enumerate(B[current]): if not visited[j]: parent[j] = current S.append(j) if len(B[current]) == 1: total_visits[current] = 1 p = parent[current] if p != -1: if p not in C: C[p] = {} C[p][current] = 1 D = {} for i in C: sum1 = 0 for j in C[i]: sum1 += C[i][j] D[i] = sum1 E = [0] * N for i in C: sum1 = 0 for j in C[i]: D[i] -= C[i][j] sum1 += C[i][j] * D[i] E[i] = sum1 for i, j in enumerate(E): if j == 0: for k in C[i]: E[i] = C[i][k] E.sort() E.reverse() A.sort() A.reverse() E = [(x % M) for x in E] A = [(x % M) for x in A] ans = 0 for i, j in zip(E, A): a = i * j a %= M ans += a ans %= M print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a tree with $N$ vertices (numbered $1$ through $N$) and a sequence of integers $A_1, A_2, \ldots, A_N$. You may choose an arbitrary permutation $p_1, p_2, \ldots, p_N$ of the integers $1$ through $N$. Then, for each vertex $i$, you should assign the value $A_{p_i}$ to this vertex. The profit of a path between two vertices $u$ and $v$ is the sum of the values assigned to the vertices on that path (including $u$ and $v$). Let's consider only (undirected) paths that start at a leaf and end at a different leaf. Calculate the maximum possible value of the sum of profits of all such paths. Since this value could be very large, compute it modulo $10^9 + 7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. - Each of the following $Nβˆ’1$ lines contains two space-separated integers $u$ and $v$ denoting that vertices $u$ and $v$ are connected by an edge. -----Output----- For each test case, print a single line containing one integer β€” the maximum sum of profits, modulo $10^9 + 7$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 300,000$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 2 4 1 2 3 4 1 2 2 3 2 4 5 1 2 3 4 5 1 2 2 3 3 4 4 5 -----Example Output----- 24 15 -----Explanation----- Example case 1: $(1, 4, 2, 3)$ is one of the possible permutations that give the optimal answer. Then, the profits of paths between pairs of vertices $(1, 3)$, $(1, 4)$ and $(3, 4)$ are $7$, $8$ and $9$ respectively. Example case 2: Here, any permutation could be chosen.
import sys sys.setrecursionlimit(10000000) def call(node, leaf, al, p): ans = 0 for i in al[node]: if i == p: continue ans += max(1, call(i, leaf, al, node)) leaf[node] = max(1, ans) return max(1, ans) def get(node, sum, al, p, leaf, total_l): ans = leaf[node] * leaf[node] for i in al[node]: if i == p: continue get(i, sum, al, node, leaf, total_l) ans -= leaf[i] * leaf[i] ans = ans // 2 ans += leaf[node] * (total_l - leaf[node]) sum[node] = ans t = int(input()) for _ in range(t): n = int(input()) arr = map(int, input().split()) arr = sorted(arr) al = [0] * n for i in range(n): al[i] = list() for i in range(n - 1): a, b = input().split() a, b = int(a) - 1, int(b) - 1 al[a].append(b) al[b].append(a) leaf = [0] * n total_l = 0 for i in al: if len(i) <= 1: total_l += 1 call(0, leaf, al, -1) sum = [0] * n get(0, sum, al, -1, leaf, total_l) sum = sorted(sum) answer, mod = 0, int(1000000000.0 + 7) for i in range(n): answer = (answer + sum[i] * arr[i]) % mod print(answer)
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a tree with $N$ vertices (numbered $1$ through $N$) and a sequence of integers $A_1, A_2, \ldots, A_N$. You may choose an arbitrary permutation $p_1, p_2, \ldots, p_N$ of the integers $1$ through $N$. Then, for each vertex $i$, you should assign the value $A_{p_i}$ to this vertex. The profit of a path between two vertices $u$ and $v$ is the sum of the values assigned to the vertices on that path (including $u$ and $v$). Let's consider only (undirected) paths that start at a leaf and end at a different leaf. Calculate the maximum possible value of the sum of profits of all such paths. Since this value could be very large, compute it modulo $10^9 + 7$. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. - Each of the following $Nβˆ’1$ lines contains two space-separated integers $u$ and $v$ denoting that vertices $u$ and $v$ are connected by an edge. -----Output----- For each test case, print a single line containing one integer β€” the maximum sum of profits, modulo $10^9 + 7$. -----Constraints----- - $1 \le T \le 1,000$ - $1 \le N \le 300,000$ - $1 \le A_i \le 10^9$ for each valid $i$ - the sum of $N$ over all test cases does not exceed $5 \cdot 10^5$ -----Example Input----- 2 4 1 2 3 4 1 2 2 3 2 4 5 1 2 3 4 5 1 2 2 3 3 4 4 5 -----Example Output----- 24 15 -----Explanation----- Example case 1: $(1, 4, 2, 3)$ is one of the possible permutations that give the optimal answer. Then, the profits of paths between pairs of vertices $(1, 3)$, $(1, 4)$ and $(3, 4)$ are $7$, $8$ and $9$ respectively. Example case 2: Here, any permutation could be chosen.
inf = 10**18 mod = 10**9 + 7 def read_line_int(): return [int(x) for x in input().split()] def getDfsOrder(adj, src): st = [src] ret = [] n = len(adj) - 1 visited = [False] * (n + 1) visited[src] = True while len(st) > 0: u = st.pop() ret.append(u) for v in adj[u]: if not visited[v]: st.append(v) visited[v] = True return ret T = read_line_int()[0] for test in range(T): n = read_line_int()[0] a = read_line_int() adj = [[] for i in range(n + 1)] for i in range(n - 1): u, v = read_line_int() adj[u].append(v) adj[v].append(u) if n == 1: print(0) continue if n == 2: print(sum(a) % mod) continue mx_deg = -1 root = -1 for i in range(1, n + 1): if mx_deg < len(adj[i]): mx_deg = len(adj[i]) root = i dfs_order = getDfsOrder(adj, root) sz = [0] * (n + 1) par = [-1] * (n + 1) cnt_leafs = 0 for u in dfs_order[::-1]: for v in adj[u]: sz[u] += sz[v] if sz[v] == 0: par[u] = v if len(adj[u]) == 1: sz[u] += 1 cnt_leafs += 1 coef = [0] * (n + 1) for u in range(1, n + 1): s = 0 for v in adj[u]: if v == par[u]: continue coef[u] += s * sz[v] s += sz[v] coef[u] += sz[u] * (cnt_leafs - sz[u]) coef = coef[1:] coef = sorted(coef) a = sorted(a) ret = sum([(x * y % mod) for x, y in zip(coef, a)]) % mod print(ret)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN 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 EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Gildong is playing with his dog, Badugi. They're at a park that has $n$ intersections and $n-1$ bidirectional roads, each $1$ meter in length and connecting two intersections with each other. The intersections are numbered from $1$ to $n$, and for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection using some set of roads. Gildong has put one snack at every intersection of the park. Now Gildong will give Badugi a mission to eat all of the snacks. Badugi starts at the $1$-st intersection, and he will move by the following rules: Badugi looks for snacks that are as close to him as possible. Here, the distance is the length of the shortest path from Badugi's current location to the intersection with the snack. However, Badugi's sense of smell is limited to $k$ meters, so he can only find snacks that are less than or equal to $k$ meters away from himself. If he cannot find any such snack, he fails the mission. Among all the snacks that Badugi can smell from his current location, he chooses a snack that minimizes the distance he needs to travel from his current intersection. If there are multiple such snacks, Badugi will choose one arbitrarily. He repeats this process until he eats all $n$ snacks. After that, he has to find the $1$-st intersection again which also must be less than or equal to $k$ meters away from the last snack he just ate. If he manages to find it, he completes the mission. Otherwise, he fails the mission. Unfortunately, Gildong doesn't know the value of $k$. So, he wants you to find the minimum value of $k$ that makes it possible for Badugi to complete his mission, if Badugi moves optimally. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of intersections of the park. The next $n-1$ lines contain two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$) each, which means there is a road between intersection $u$ and $v$. All roads are bidirectional and distinct. It is guaranteed that: For each test case, for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection. The sum of $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer β€” the minimum possible value of $k$ such that Badugi can complete the mission. -----Examples----- Input 3 3 1 2 1 3 4 1 2 2 3 3 4 8 1 2 2 3 3 4 1 5 5 6 6 7 5 8 Output 2 3 3 -----Note----- In the first case, Badugi can complete his mission with $k=2$ by moving as follows: Initially, Badugi is at the $1$-st intersection. The closest snack is obviously at the $1$-st intersection, so he just eats it. Next, he looks for the closest snack, which can be either the one at the $2$-nd or the one at the $3$-rd intersection. Assume that he chooses the $2$-nd intersection. He moves to the $2$-nd intersection, which is $1$ meter away, and eats the snack. Now the only remaining snack is on the $3$-rd intersection, and he needs to move along $2$ paths to get to it. After eating the snack at the $3$-rd intersection, he needs to find the $1$-st intersection again, which is only $1$ meter away. As he gets back to it, he completes the mission. In the second case, the only possible sequence of moves he can make is $1$ – $2$ – $3$ – $4$ – $1$. Since the distance between the $4$-th intersection and the $1$-st intersection is $3$, $k$ needs to be at least $3$ for Badugi to complete his mission. In the third case, Badugi can make his moves as follows: $1$ – $5$ – $6$ – $7$ – $8$ – $2$ – $3$ – $4$ – $1$. It can be shown that this is the only possible sequence of moves for Badugi to complete his mission with $k=3$.
for _ in range(int(input())): n = int(input()) dic = [[] for _ in range(n + 1)] gress = [0] * (n + 1) gress[1] += 1 father = [0] * (n + 1) now = [1] s = [[] for _ in range(n + 1)] leaf = [] for _ in range(n - 1): u, v = map(int, input().split()) dic[u].append(v) dic[v].append(u) while now: node = now.pop() for child in dic[node]: if child != father[node]: gress[node] += 1 father[child] = node now.append(child) if gress[node] == 0: leaf.append(node) while leaf: node = leaf.pop() f = father[node] if not s[node]: s[f].append((1, 0)) elif len(s[node]) == 1: d, k = s[node][0] s[f].append((d + 1, k)) else: d = min(p[0] for p in s[node]) + 1 k = max(max(p[1] for p in s[node]), max(p[0] for p in s[node]) + 1) s[f].append((d, k)) gress[f] -= 1 if gress[f] == 0: leaf.append(f) node = 1 if len(s[node]) == 1: print(max(s[node][0])) else: k = max(p[1] for p in s[node]) tmp = [p[0] for p in s[node]] m = max(tmp) tmp.remove(m) print(max(max(tmp) + 1, m, k))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
Gildong is playing with his dog, Badugi. They're at a park that has $n$ intersections and $n-1$ bidirectional roads, each $1$ meter in length and connecting two intersections with each other. The intersections are numbered from $1$ to $n$, and for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection using some set of roads. Gildong has put one snack at every intersection of the park. Now Gildong will give Badugi a mission to eat all of the snacks. Badugi starts at the $1$-st intersection, and he will move by the following rules: Badugi looks for snacks that are as close to him as possible. Here, the distance is the length of the shortest path from Badugi's current location to the intersection with the snack. However, Badugi's sense of smell is limited to $k$ meters, so he can only find snacks that are less than or equal to $k$ meters away from himself. If he cannot find any such snack, he fails the mission. Among all the snacks that Badugi can smell from his current location, he chooses a snack that minimizes the distance he needs to travel from his current intersection. If there are multiple such snacks, Badugi will choose one arbitrarily. He repeats this process until he eats all $n$ snacks. After that, he has to find the $1$-st intersection again which also must be less than or equal to $k$ meters away from the last snack he just ate. If he manages to find it, he completes the mission. Otherwise, he fails the mission. Unfortunately, Gildong doesn't know the value of $k$. So, he wants you to find the minimum value of $k$ that makes it possible for Badugi to complete his mission, if Badugi moves optimally. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of intersections of the park. The next $n-1$ lines contain two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$) each, which means there is a road between intersection $u$ and $v$. All roads are bidirectional and distinct. It is guaranteed that: For each test case, for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection. The sum of $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer β€” the minimum possible value of $k$ such that Badugi can complete the mission. -----Examples----- Input 3 3 1 2 1 3 4 1 2 2 3 3 4 8 1 2 2 3 3 4 1 5 5 6 6 7 5 8 Output 2 3 3 -----Note----- In the first case, Badugi can complete his mission with $k=2$ by moving as follows: Initially, Badugi is at the $1$-st intersection. The closest snack is obviously at the $1$-st intersection, so he just eats it. Next, he looks for the closest snack, which can be either the one at the $2$-nd or the one at the $3$-rd intersection. Assume that he chooses the $2$-nd intersection. He moves to the $2$-nd intersection, which is $1$ meter away, and eats the snack. Now the only remaining snack is on the $3$-rd intersection, and he needs to move along $2$ paths to get to it. After eating the snack at the $3$-rd intersection, he needs to find the $1$-st intersection again, which is only $1$ meter away. As he gets back to it, he completes the mission. In the second case, the only possible sequence of moves he can make is $1$ – $2$ – $3$ – $4$ – $1$. Since the distance between the $4$-th intersection and the $1$-st intersection is $3$, $k$ needs to be at least $3$ for Badugi to complete his mission. In the third case, Badugi can make his moves as follows: $1$ – $5$ – $6$ – $7$ – $8$ – $2$ – $3$ – $4$ – $1$. It can be shown that this is the only possible sequence of moves for Badugi to complete his mission with $k=3$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) e = [[] for _ in range(n + 1)] for _ in range(n - 1): u, v = map(int, input().split()) e[u].append(v) e[v].append(u) dp = [n] * (n + 1) s = [1] vis = [0] * (n + 1) order = [] parent = [-1] * (n + 1) while len(s): x = s.pop() vis[x] = 1 order.append(x) c = 0 for y in e[x]: if vis[y]: continue c = 1 s.append(y) vis[y] = 1 parent[y] = x if c == 0: dp[x] = 0 order.reverse() order.pop() for y in order: x = parent[y] dp[x] = min(dp[x], dp[y] + 1) c = 0 res = 0 mx = 0 mx2 = 0 for y in e[1]: mx = max(mx, dp[y] + 1) for y in e[1]: if dp[y] + 1 != mx: mx2 = max(mx2, dp[y] + 1) for y in e[1]: c += dp[y] == mx - 1 if c > 1: res = mx + 1 else: res = max(mx, mx2 + 1) for x in range(2, n + 1): t = 0 c = len(e[x]) - 1 for y in e[x]: if y != parent[x]: t = max(t, dp[y] + 1) if c > 1: res = max(res, t + 1) print(res)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Gildong is playing with his dog, Badugi. They're at a park that has $n$ intersections and $n-1$ bidirectional roads, each $1$ meter in length and connecting two intersections with each other. The intersections are numbered from $1$ to $n$, and for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection using some set of roads. Gildong has put one snack at every intersection of the park. Now Gildong will give Badugi a mission to eat all of the snacks. Badugi starts at the $1$-st intersection, and he will move by the following rules: Badugi looks for snacks that are as close to him as possible. Here, the distance is the length of the shortest path from Badugi's current location to the intersection with the snack. However, Badugi's sense of smell is limited to $k$ meters, so he can only find snacks that are less than or equal to $k$ meters away from himself. If he cannot find any such snack, he fails the mission. Among all the snacks that Badugi can smell from his current location, he chooses a snack that minimizes the distance he needs to travel from his current intersection. If there are multiple such snacks, Badugi will choose one arbitrarily. He repeats this process until he eats all $n$ snacks. After that, he has to find the $1$-st intersection again which also must be less than or equal to $k$ meters away from the last snack he just ate. If he manages to find it, he completes the mission. Otherwise, he fails the mission. Unfortunately, Gildong doesn't know the value of $k$. So, he wants you to find the minimum value of $k$ that makes it possible for Badugi to complete his mission, if Badugi moves optimally. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the number of intersections of the park. The next $n-1$ lines contain two integers $u$ and $v$ ($1 \le u,v \le n$, $u \ne v$) each, which means there is a road between intersection $u$ and $v$. All roads are bidirectional and distinct. It is guaranteed that: For each test case, for every $a$ and $b$ ($1 \le a, b \le n$), it is possible to get to the $b$-th intersection from the $a$-th intersection. The sum of $n$ in all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print one integer β€” the minimum possible value of $k$ such that Badugi can complete the mission. -----Examples----- Input 3 3 1 2 1 3 4 1 2 2 3 3 4 8 1 2 2 3 3 4 1 5 5 6 6 7 5 8 Output 2 3 3 -----Note----- In the first case, Badugi can complete his mission with $k=2$ by moving as follows: Initially, Badugi is at the $1$-st intersection. The closest snack is obviously at the $1$-st intersection, so he just eats it. Next, he looks for the closest snack, which can be either the one at the $2$-nd or the one at the $3$-rd intersection. Assume that he chooses the $2$-nd intersection. He moves to the $2$-nd intersection, which is $1$ meter away, and eats the snack. Now the only remaining snack is on the $3$-rd intersection, and he needs to move along $2$ paths to get to it. After eating the snack at the $3$-rd intersection, he needs to find the $1$-st intersection again, which is only $1$ meter away. As he gets back to it, he completes the mission. In the second case, the only possible sequence of moves he can make is $1$ – $2$ – $3$ – $4$ – $1$. Since the distance between the $4$-th intersection and the $1$-st intersection is $3$, $k$ needs to be at least $3$ for Badugi to complete his mission. In the third case, Badugi can make his moves as follows: $1$ – $5$ – $6$ – $7$ – $8$ – $2$ – $3$ – $4$ – $1$. It can be shown that this is the only possible sequence of moves for Badugi to complete his mission with $k=3$.
import sys input = lambda: sys.stdin.readline() def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def print_list(l): print(" ".join(map(str, l))) for _ in range(N()): n = N() dic = [[] for _ in range(n + 1)] gress = [0] * (n + 1) gress[1] += 1 father = [0] * (n + 1) for _ in range(n - 1): u, v = RL() dic[u].append(v) dic[v].append(u) now = [1] s = [[] for _ in range(n + 1)] leaf = [] while now: node = now.pop() for child in dic[node]: if child != father[node]: gress[node] += 1 father[child] = node now.append(child) if gress[node] == 0: leaf.append(node) while leaf: node = leaf.pop() f = father[node] if not s[node]: s[f].append((1, 0)) elif len(s[node]) == 1: d, k = s[node][0] s[f].append((d + 1, k)) else: d = min(p[0] for p in s[node]) + 1 k = max(max(p[1] for p in s[node]), max(p[0] for p in s[node]) + 1) s[f].append((d, k)) gress[f] -= 1 if gress[f] == 0: leaf.append(f) node = 1 if len(s[node]) == 1: print(max(s[node][0])) else: k = max(p[1] for p in s[node]) tmp = [p[0] for p in s[node]] m = max(tmp) tmp.remove(m) print(max(max(tmp) + 1, m, k))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
f = lambda: map(int, input().split()) n, k = f() s, d = [], -1 for q in f(): if q < 0: k -= 1 if d > 0: s += [d] d = 0 elif d > -1: d += 1 s.sort() t = 2 * len(s) for q in s: if q > k: break k -= q t -= 2 print(-1 if k < 0 else 0 if d < 0 else 2 + t - (k >= d))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
f = lambda: map(int, input().split()) n, k = f() s, d = [], -1 for q in f(): if q < 0: k -= 1 if d > 0: s += [d] d = 0 elif d + 1: d += 1 s.sort() def g(k, d): d += len(s) << 1 for q in s: if q > k: break k -= q d -= 2 return d print(0 if d < 0 else -1 if k < 0 else g(k, 2) if k < d else min(g(k, 2), g(k - d, 1)))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
import sys inp = sys.stdin.readline def input(): return inp().strip() flush = sys.stdout.flush def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): n, k = lin() a = lin() a1 = [0] * n for i in range(n): if a[i] < 0: a1[i] = 1 sm = sum(a1) if sm > k: print(-1) else: sm = k - sm lft = [] ch = 0 for i in range(n): if a1[i]: if ch and i - ch: lft.append([ch, i - ch, 2]) ch = 0 else: ch += 1 ext = [] if ch and i + 1 - ch: ext = [ch, 1 + i - ch, 1] lft.sort() a2 = lft + ([ext] if ext else []) a2.sort() def check(a): if not a: return 0 s1 = sm ans = 0 for i, j, k in a: if s1 < i: break s1 -= i ans += k return ans if check(a2) > check(lft): lft = a2 lft = lft[::-1] while sm and lft: ch, i, asd = lft.pop() if sm < ch: continue while sm and ch and i < n: a1[i] = 1 i += 1 sm -= 1 ch -= 1 ans = 0 a1 = [0] + a1 for i in range(1, n + 1): if a1[i] != a1[i - 1]: ans += 1 print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST IF VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST VAR BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR LIST EXPR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = list(map(int, input().split())) days = list(map(int, input().split())) a = [(0) for i in range(n)] b = [(0) for i in range(n)] asum = 0 ia = 0 ib = 0 inf = n + 199 for x in days: if x < 0: a[ia] += 1 asum += 1 if b[ib] != 0: ib += 1 else: b[ib] += 1 if a[ia] != 0: ia += 1 changes = ia + 1 if a[ia] > 0 else ia changes += changes - 1 if a[ia] == 0: ia -= 1 if b[ib] == 0: ib -= 1 if asum > k: print(-1) quit() for i in range(ia, n): a[i] = inf if a[i] == 0 else a[i] for i in range(ib, n): b[i] = inf if b[i] == 0 else b[i] if days[0] >= 0: b[0] = inf lastb = -1 removedlastb = False if days[len(days) - 1] >= 0: changes += 1 lastb = b[ib] b.sort() curb = 0 seccurb = curb secasum = asum secchanges = changes secremovedlastb = removedlastb while curb <= ib and asum + b[curb] <= k: asum += b[curb] if not removedlastb: if b[curb] == lastb: changes -= 1 removedlastb = True else: changes -= 2 else: changes -= 2 curb += 1 while seccurb <= ib and secasum + b[seccurb] <= k: secasum += b[seccurb] if not secremovedlastb: if b[seccurb] == lastb: secasum -= b[seccurb] secremovedlastb = True else: secchanges -= 2 else: secchanges -= 2 seccurb += 1 print(max(min(changes, secchanges), 0))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = map(int, input().split()) temp = list(map(int, input().split())) rslt = [] list = [] cnt = 0 for i in range(n): if temp[i] < 0: rslt.append(i) cnt += 1 if cnt > k: print(-1) exit() if cnt == 0: print(0) exit() k -= cnt ans = 2 * cnt for i in range(cnt - 1): list.append(rslt[i + 1] - rslt[i] - 1) list.sort() for i in list: if k < i: break k -= i ans -= 2 if k >= n - rslt[cnt - 1] - 1: ans -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER 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 FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = map(int, input().split()) inF = False bad = 0 last = None stretches = [0] for d in map(lambda x: int(x) >= 0, input().split()): last = d if d and not inF: continue else: inF = True if d: stretches[-1] += 1 else: bad += 1 if stretches[-1] != 0: stretches.append(0) if bad > k: print(-1) elif bad == 0: print(0) elif n == k: print(1) else: k -= bad num = 1 if last: num -= 1 if stretches[-1] == 0: del stretches[-1] num_ = num stretches_ = stretches[:-1] k_ = k if len(stretches) > 0: num += len(stretches) * 2 last = stretches[-1] stretches.sort() while len(stretches) > 0 and stretches[0] <= k: num -= 2 k -= stretches[0] del stretches[0] if not last in stretches: num += 1 if len(stretches_) > 0: num_ += (len(stretches_) + 1) * 2 stretches_.sort() while len(stretches_) > 0 and stretches_[0] <= k_: num_ -= 2 k_ -= stretches_[0] del stretches_[0] print(min(num, num_)) else: print(num)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = list(map(int, input().split())) a = list([(int(x) >= 0) for x in input().split()]) for ind_positive in range(n): if not a[ind_positive]: break else: print(0) return a = a[ind_positive:] n = len(a) for ind_positive in range(n - 1, -1, -1): if not a[ind_positive]: break a = a[: ind_positive + 1] len_suf = n - ind_positive - 1 n = len(a) b = [] i = 0 while True: j = 0 while i < n and a[i]: i += 1 j += 1 if j: b.append(j) i += 1 if i >= n: break b_sort = b.copy() b_sort.sort(reverse=True) s = 0 i = 0 if n - s > k: for i in range(len(b)): s += b_sort[i] if n - s <= k: i += 1 break if n - s > k: ans2 = -1 else: ans2 = i * 2 + 2 k -= len_suf if k < 0: ans1 = -1 else: b = [] i = 0 while True: j = 0 while i < n and a[i]: i += 1 j += 1 if j: b.append(j) i += 1 if i >= n: break b_sort = b.copy() b_sort.sort(reverse=True) s = 0 i = 0 if n - s > k: for i in range(len(b)): s += b_sort[i] if n - s <= k: i += 1 break if n - s > k: ans1 = -1 else: ans1 = i * 2 + 1 if ans1 == -1: print(ans2) elif ans2 == -1: print(ans1) else: print(min(ans1, ans2))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
_, k = map(int, input().split()) val = [0] + list(map(int, input().split())) n = len(val) pls = [] cur, i, sm = 0, 0, 0 while i < n: if val[i] >= 0: cur += 1 else: if cur > 0: pls.append(cur) cur = 0 i += 1 sm = sum(p for p in pls) pls = pls[1:] if cur > 0: one = cur sm += cur else: one = 10**19 ans = 0 for i in range(1, n): if val[i] >= 0 and val[i - 1] < 0 or val[i] < 0 and val[i - 1] >= 0: ans += 1 if n - sm > k: print(-1) else: pls.sort() rem = k - (n - sm) for p in pls: if rem >= p: rem -= p ans -= 2 if rem >= one: ans -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = [int(i) for i in input().split(" ")] w = [int(i) for i in input().split(" ")] f = 0 while f < n and w[f] >= 0: f += 1 if f == n: print(0) exit() b = n - 1 while b >= 0 and w[b] >= 0: b -= 1 if f == b: if k == 0: print(-1) exit() if f + 1 == n: print(1) exit() elif k - 1 >= n - (b + 1): print(1) exit() else: print(2) exit() inv = [] ps = 0 ns = 0 for i in range(f, b): if w[i] >= 0: ps += 1 if w[i + 1] < 0: inv.append(ps) elif w[i] < 0: ns += 1 if w[i + 1] >= 0: ps = 0 ns += 1 spare = k - ns if spare < 0: print(-1) exit() if not inv: if w[n - 1] < 0: print(1) exit() elif spare >= n - (b + 1): print(1) exit() else: print(2) exit() invs = sorted(inv) if spare < invs[0]: ch = (len(invs) + 1) * 2 remainder = spare else: use, invsn = invs[0], -1 for i in range(1, len(invs)): use += invs[i] if spare < use: use -= invs[i] invsn = i break if invsn == -1: invsn = len(invs) ch = (len(invs) - invsn + 1) * 2 remainder = spare - use if remainder >= n - (b + 1): ch -= 1 print(ch)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
def ri(): return list(map(int, input().split())) n, k = ri() t = list(ri()) tt = [] mc = 0 for i in range(n): if i == 0: if t[i] < 0: tt.append(-1) mc += 1 else: tt.append(1) continue if t[i] < 0: mc += 1 if t[i - 1] >= 0: tt.append(-1) continue elif t[i - 1] >= 0: tt[-1] = tt[-1] + 1 else: tt.append(1) if mc > k: print(-1) return if tt[0] != -1: tt = tt[1:] if len(tt) == 0: print(0) return ttt = [] maxs = len(tt) k2 = k tt2 = tt[:] if tt[-1] != -1: tt = tt[:-1] for i in tt: if i > 0: ttt.append(i) k -= mc ttt.sort() for i in ttt: if k - i >= 0: k -= i maxs -= 2 else: break ttt2 = [] maxs2 = len(tt2) for i in tt2: if i > 0: ttt2.append(i) k2 -= mc check = 0 ttt2.sort() for i in ttt2: if k2 - i >= 0: k2 -= i maxs2 -= 2 if i == tt2[-1] and check == 0: maxs2 += 1 check = 1 else: break print(min(maxs, maxs2))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
def solve(k, s): w = [0] for c in s: if c < 0: if w[-1] >= 0: w.append(-1) else: w[-1] -= 1 elif w[-1] < 0: w.append(1) else: w[-1] += 1 begin, end = w[0], w[-1] if len(w) == 1 and w[0] > 0: return 0 if begin > 0: w.pop(0) if end > 0: w.pop() neg_seg = [t for t in w if t < 0] pos_seg = [t for t in w if t > 0] d = -sum(neg_seg) if d > k: return -1 changes = 2 * len(neg_seg) - (end < 0) pos_seg.sort() for seg in pos_seg: d += seg if d > k: if end > 0 and d - seg + end <= k: return changes - 1 else: return changes else: changes -= 2 if end > 0 and d + end <= k: return changes - 1 else: return changes n, k = map(int, input().split()) s = [int(x) for x in input().split()] print(solve(k, s))
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = map(int, input().split()) d = list(map(int, input().split())) p = [] count = 0 count1 = 0 for i in range(len(d)): if d[i] >= 0: count += 1 if d[i] < 0 or i == len(d) - 1: p.append(count) count = 0 count1 += 1 if d[len(d) - 1] >= 0: count1 -= 1 if count1 == 0: print(0) return a = p.pop() if d[len(d) - 1] >= 0 else 0 if len(p) > 0: p.reverse() p.pop() p.sort() count = k - len(p) - 1 if count1 > 1 else k - count1 if count < 0: print(-1) return count1 = 0 for i in range(len(p)): if count - p[i] >= 0: count1 += 1 count -= p[i] else: break ans = 2 * (len(p) + 1 - count1) if count - a >= 0: ans -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
def solve(): n, k = list(map(int, input().split())) temps = list(map(int, input().split())) summer_seqs = [] winter_seqs = [] cur_season = 1 cur_len = 0 for t in temps: if cur_season * t > 0 or t == 0 and cur_season == 1: cur_len += 1 else: if cur_season == 1: summer_seqs.append(cur_len) else: winter_seqs.append(cur_len) cur_len = 1 cur_season = -cur_season if cur_season == 1: summer_seqs.append(cur_len) else: winter_seqs.append(cur_len) summer_seqs = summer_seqs[1:] cur_len = sum(winter_seqs) if cur_len > k: return -1 if len(summer_seqs) == 0: return 1 if len(winter_seqs) != 0 else 0 changes = len(summer_seqs) + len(winter_seqs) last_sum_seq = None if temps[-1] >= 0: last_sum_seq = summer_seqs[-1] summer_seqs = summer_seqs[:-1] summer_seqs = list(sorted(summer_seqs)) for s in summer_seqs: if k - cur_len >= s: changes -= 2 cur_len += s else: break if last_sum_seq is not None: if k - cur_len >= last_sum_seq: changes -= 1 return changes def __starting_point(): print(solve()) __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NONE IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR IF VAR NONE IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = [int(i) for i in input().split()] t = [int(i) for i in input().split()] colddays = [i for i in range(n) if t[i] < 0] num_colddays = len(colddays) if num_colddays == 0: print(0) elif k >= n: print(1) elif num_colddays > k: print(-1) else: k -= num_colddays ans = 0 for i in range(1, len(colddays)): if colddays[i] - colddays[i - 1] > 1: ans += 1 ans += 1 ans *= 2 gap = [(0) for i in range(num_colddays)] for j in range(1, num_colddays): gap[j - 1] = colddays[j] - colddays[j - 1] - 1 positivegap = [j for j in gap if j > 0] positivegap = sorted(positivegap) for i in positivegap: if k >= i: k -= i ans -= 2 else: break remain = n - 1 - colddays[-1] if k >= remain: ans -= 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR 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 VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. -----Input----- The first line contains two positive integers n and k (1 ≀ n ≀ 2Β·10^5, 0 ≀ k ≀ n)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total. The second line contains a sequence of n integers t_1, t_2, ..., t_{n} ( - 20 ≀ t_{i} ≀ 20)Β β€” the average air temperature in the i-th winter day. -----Output----- Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. -----Examples----- Input 4 3 -5 20 -3 0 Output 2 Input 4 2 -5 20 -3 0 Output 4 Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Output 3 -----Note----- In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
n, k = list(map(int, input().split())) winter = [] for i, t in enumerate([int(n) for n in input().split()]): if t < 0: winter.append(i) if len(winter) > k: print(-1) else: ans = len(winter) * 2 k -= len(winter) for diff in sorted( [(winter[i] - winter[i - 1] - 1) for i in range(1, len(winter))] ): if k >= diff: k -= diff ans -= 2 print(ans - (1 if len(winter) > 0 and k >= n - winter[-1] - 1 else 0))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) pos = [] for i in range(n): vvod1, vvod2 = map(int, input().split()) pos.append([vvod2, vvod1]) dop1 = pos.copy() pos.sort() pos.reverse() k = int(input()) sto = list(map(int, input().split())) dop2 = sto.copy() sto.sort() kol = 0 suma = 0 vivod = [] for y in range(n): for x in range(k): if pos[y] != "" and sto[x] != "" and pos[y][1] <= sto[x]: vivod.append([pos[y], sto[x]]) suma += pos[y][0] kol += 1 pos[y] = "" sto[x] = "" break print(kol, suma) for i in range(kol): x = dop1.index(vivod[i][0]) y = dop2.index(vivod[i][1]) print(x + 1, y + 1) dop1[x] = "" dop2[y] = ""
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) cp = [(list(map(int, input().split())) + [i]) for i in range(n)] k = int(input()) r = list(map(int, input().split())) for i in range(k): r[i] = [r[i], i] cp.sort(key=lambda x: x[1], reverse=True) r.sort() use = [False] * k ans = [] m, s = 0, 0 for i in range(n): c, p, idx = cp[i] for j in range(k): rr, idx2 = r[j] if rr >= c and not use[j]: use[j] = True m += 1 s += p ans.append([idx + 1, idx2 + 1]) break print(m, s) for x in ans: print(*x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
from _collections import defaultdict n = int(input()) d = defaultdict(list) for i in range(1, n + 1): d[tuple(map(int, input().split()))].append(i) m = int(input()) t = defaultdict(list) for x, y in enumerate(map(int, input().split()), 1): t[y].append(x) s = 0 c = 0 l = [] for x in sorted(d.keys(), key=lambda x: (-x[1], x[0])): for j in d[x]: for i in sorted(t): if len(t[i]) > 0 and i >= x[0]: z = 0 for v in t[i]: s += x[1] c += 1 l.append([j, v]) t[i].pop(t[i].index(v)) n += 1 z = 1 break if len(t[i]) == 0: del t[i] if z == 1: break print(c, s) for i in l: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) l = [] for i in range(n): l.append(list(map(int, input().split()))) l[i] += [i + 1] k = int(input()) s = list(map(int, input().split())) for i in range(k): s[i] = [i + 1, s[i]] def fun(itm): return itm[1] l.sort(reverse=True, key=fun) s.sort(key=fun) ans = 0 ansl = [] for i in l: for j in s: if i[0] <= j[1]: ans += i[1] s.remove(j) ansl.append([i[2], j[0]]) break print(len(ansl), ans) for i in ansl: print(i[0], i[1])
ASSIGN VAR FUNC_CALL VAR 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 VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER VAR VAR FUNC_DEF RETURN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
def smaller(a, val, vis): ind = None for i in range(len(a)): if a[i] >= val and vis[i] == 0: if ind != None: if a[i] < a[ind]: ind = i else: ind = i return ind def f(p, a): for i in range(len(p)): p[i].append(i) p = sorted(p, key=lambda s: s[1], reverse=True) ans = [] s = 0 vis = [0] * len(a) for i in p: ind = smaller(a, i[0], vis) if ind != None: ans.append([i[2] + 1, ind + 1]) vis[ind] = 1 s += i[1] print(len(ans), s) for i in ans: print(*i) return "" p = [] for i in range(int(input())): a, b = map(int, input().strip().split()) p.append([a, b]) k = input() ar = list(map(int, input().strip().split())) print(f(p, ar))
FUNC_DEF ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NONE IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR NONE EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) g = [(list(map(int, input().split())) + [i]) for i in range(n)] k = int(input()) r = [(v, i) for i, v in enumerate(list(map(int, input().split())))] b = [None] * n sg = sorted(g, key=lambda x: x[1], reverse=True) sr = sorted(r) count = 0 money = 0 for v in sg: for w in sr: if w[0] < v[0]: continue b[v[2]] = w[1] count += 1 money += v[1] sr.remove(w) break print(count, money) for i in range(n): if b[i] == None: continue print(i + 1, b[i] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) people = [] for i in range(n): inp = input().split() p, c = int(inp[0]), int(inp[1]) people.append([c, p, i + 1]) people.sort() k = int(input()) z = input().split() v = [[int(z[i]), i + 1] for i in range(k)] v.sort() tot = 0 s = 0 ans = [] for i in range(n - 1, -1, -1): for j in range(len(v)): if v[j][0] >= people[i][1]: tot += people[i][0] ans.append([people[i][2], v[j][1]]) s += 1 v.remove(v[j]) break print(s, tot) for i in range(s): print(ans[i][0], ans[i][1])
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 VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) g = [] for i in range(n): g.append(list(map(int, input().split()))[::-1]) g_copy = g[:] a = int(input()) t = list(map(int, input().split())) t_copy = t[:] g.sort(reverse=True) t.sort() ans = [] s = 0 k = 0 for group in g: for table in range(len(t)): if group[1] <= t[table]: k += 1 s += group[0] ans.append([group, t[table]]) t[table] = 0 break print(k, s) for answer in ans: gr = answer[0] ta = answer[1] ig = g_copy.index(gr) it = t_copy.index(ta) print(ig + 1, end=" ") print(it + 1, end="") g_copy[ig] = -1 t_copy[it] = -1 print()
ASSIGN VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) vs = list() for i in range(n): a, b = map(int, input().strip().split()) vs.append([b, a, i + 1]) vs.sort(reverse=True) k = int(input()) tb = list() j = 1 for i in map(int, input().strip().split()): tb.append([i, j]) j += 1 tb.sort() i = 0 ans = 0 ans_ar = list() while len(tb) > 0 and i < len(vs): l = 0 r = len(tb) - 1 mid = int(r / 2) while l < r: if vs[i][1] > tb[mid][0]: l = mid + 1 else: r = mid mid = int((l + r) / 2) if vs[i][1] <= tb[r][0]: ans += vs[i][0] ans_ar.append([vs[i][2], tb[r][1]]) tb.pop(r) i += 1 print("{} {}".format(len(ans_ar), ans)) for i in range(len(ans_ar)): print("{} {}".format(ans_ar[i][0], ans_ar[i][1]))
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 FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER VAR VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
def findTable(tables, c, visited): min_diff = float("inf") index = -1 k = len(tables) for i in range(k): diff = tables[i] - c if diff >= 0: if diff < min_diff and i + 1 not in visited: min_diff = diff index = i + 1 if index != -1: visited.add(index) return index def main(): n = int(input()) visitors = [] for i in range(n): c, p = map(int, input().split()) visitors.append((p, c, i + 1)) visitors.sort(reverse=True) ans = [] money = 0 k = int(input()) tables = list(map(int, input().split())) visited = set() for visitor in visitors: p, c = visitor[0], visitor[1] index = findTable(tables, c, visited) if index != -1: visited.add(index) ans.append((visitor[2], index)) money += p print(len(ans), money) for i in ans: print(i[0], i[1]) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) class req: def __init__(self, no, size, cash): self.no = no self.size = size self.cash = cash class table: def __init__(self, no, size): self.no = no self.size = size rl = [] for i in range(n): s, c = [int(x) for x in input().split()] a = req(i + 1, s, c) rl.append(a) k = int(input()) t = [int(x) for x in input().split()] tl = [] for i in range(len(t)): a = table(i + 1, t[i]) tl.append(a) rl.sort(key=lambda x: x.cash, reverse=True) tl.sort(key=lambda x: x.size) tot = 0 seated = 0 a = [] for i in tl: s = i.size for j in rl: if j.size <= i.size: tot += j.cash seated += 1 a.append([j.no, i.no]) rl.remove(j) break a.sort(key=lambda x: x[0]) print(seated, tot) for i in a: print(i[0], i[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) P = [] for i in range(n): x, y = map(int, input().split()) P.append((y, x, i)) k = int(input()) L = list(map(int, input().split())) for i in range(k): L[i] = L[i], i L.sort() P.sort(reverse=True) ans = 0 Taken = [False] * k r = 0 Ans = [-1] * n for i in range(n): for j in range(k): if L[j][0] >= P[i][1] and Taken[j] == False: Taken[j] = True ans += P[i][0] r += 1 Ans[P[i][2]] = L[j][1] break print(r, ans) for i in range(n): if Ans[i] == -1: continue print(i + 1, Ans[i] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input("")) group = [] table = [] for i in range(n): size, money = map(int, input("").split()) group += [(money, size, i)] k = int(input("")) out = [] all_money = 0 table = list(map(int, input("").split())) for money, size, j in reversed(sorted(group)): max = 1000 uu = -1 for i, table1 in enumerate(table): if size <= table1 < max + 1: max = table1 uu = i if uu > -1: table[uu] = 0 out += [(j, uu)] all_money += money print(len(out), all_money) for i, j in out: print(i + 1, j + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
def get_key(item): return item[1] n = int(input()) requests = [] for i in range(n): c, p = map(int, input().split()) requests.append([c, p, i + 1]) k = int(input()) l = list(map(int, input().split())) capacity = [] for i in range(k): capacity.append([l[i], i + 1, 0]) capacity = sorted(capacity) requests = sorted(requests, key=get_key, reverse=True) results = [] sum = 0 for i in range(n): for j in range(k): if capacity[j][2] == 0 and requests[i][0] <= capacity[j][0]: capacity[j][2] = 1 results.append([requests[i][2], capacity[j][1]]) sum += requests[i][1] break print(len(results), sum) for i in results: print(i[0], i[1])
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) ls = [] for i in range(n): c, p = map(int, input().split()) ls.append([p, c, i + 1]) ls.sort(reverse=True) k = int(input()) table = [[0] for i in range(1001)] r = list(map(int, input().split())) for i in range(k): table[r[i]].append(i + 1) total = 0 ans = [] for i in range(n): for j in range(ls[i][1], 1001): if table[j][0] < len(table[j]) - 1: total += ls[i][0] ans.append([ls[i][2], table[j][table[j][0] + 1]]) table[j][0] += 1 break print(len(ans), total) for i in range(len(ans)): print(*ans[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) R = [] for r in range(n): c, p = list(map(int, input().split())) R.append((p, c, r + 1)) m = int(input()) T = list(map(int, input().split())) T = sorted([[c, i + 1] for i, c in enumerate(T)]) R = sorted(R, reverse=True) ans = 0 B = [] for p, c, r in R: for i in range(m): if T[i][0] >= c: B.append((r, T[i][1])) T[i][0] = -1 ans += p break print(len(B), ans) for r, t in B: print(r, t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
import sys input = sys.stdin.readline def multi_input(): return map(int, input().split()) def array_print(arr): print(" ".join(map(str, arr))) requests = [] r = int(input()) for n in range(1, r + 1): p, c = multi_input() requests.append([n, p, c]) t = int(input()) tables = list(multi_input()) tables_index = [0] * t requests.sort(key=lambda item: item[2], reverse=True) ans = [] profit = 0 for i in range(r): req_index = requests[i][0] person = requests[i][1] cost = requests[i][2] idx = -1 jgte = 1000000 for j in range(t): if tables_index[j] == 0 and tables[j] >= person and jgte >= tables[j]: jgte = tables[j] idx = j if idx != -1: profit += cost tables_index[idx] = 1 ans.append([req_index, idx + 1]) print(len(ans), profit) for i in range(len(ans)): print(ans[i][0], ans[i][1])
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) d = {} for x in range(n): grp, m = map(int, input().strip().split()) if m not in d: d[m] = [[grp, x + 1]] else: d[m].append([grp, x + 1]) k = int(input()) arr = list(map(int, input().strip().split())) sums = 0 count = 0 last = -1 lis = [] for e in sorted(d.keys(), reverse=True): for i in d[e]: diff = 100000 f = 0 for y in range(len(arr)): if arr[y] - i[0] >= 0 and i[0] <= arr[y]: if diff > arr[y] - i[0]: diff = arr[y] - i[0] last = y f = 1 if f == 1: lis.append([i[1], last + 1]) arr[last] = -1 count += 1 sums += e print(count, sums) for l in lis: print(*l)
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 FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) request, ans = [], [] for idx in range(n): request.append(list(map(int, input().split())) + [idx + 1]) k = int(input()) accept = 0 size = list(map(int, input().split())) for idx in range(k): size[idx] = [size[idx], idx + 1] size.sort() request.sort(reverse=True, key=lambda x: x[1]) total_cost = 0 for idx in range(n): if size and size[-1][0] >= request[idx][0]: l, r = 0, k - 1 - accept while l < r - 1: mid = (l + r) // 2 if size[mid][0] < request[idx][0]: l = mid + 1 else: r = mid if size[l][0] < request[idx][0]: l = r total_cost += request[idx][1] ans.append([request[idx][2], size[l][1]]) del size[l] accept += 1 print(accept, total_cost) for val in ans: print(val[0], val[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
def low_bound(mas, x): l = 0 r = len(mas) - 1 while l < r: m = (l + r) // 2 if mas[m][0] >= x: r = m else: l = m + 1 return l n = int(input().strip()) gr = [] for i in range(n): gr.append(list(map(int, input().split()))[::-1] + [i + 1]) k = int(input().strip()) tab = [[v, i + 1] for i, v in enumerate(list(map(int, input().split())))] gr.sort(reverse=True) ans = [] s = 0 tab.sort() for p, cnt, ind in gr: if len(tab) > 0 and tab[-1][0] >= cnt: s += p i = low_bound(tab, cnt) ans.append((ind, tab[i][1])) tab.remove(tab[i]) print(len(ans), s) for i in ans: print(*i) tab.sort()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n = nmbr() l = [lst() for _ in range(n)] m = nmbr() sm = 0 a = sorted(zip(lst(), range(m))) vis = {} for i in range(m): mx = 0 p = -1 for j in range(n): if j not in vis and l[j][0] <= a[i][0] and mx <= l[j][1]: mx = l[j][1] p = j if p != -1: vis[p] = a[i][1] sm += mx print(len(vis), sm) for k, v in vis.items(): print(k + 1, v + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) requests = [] i = 0 while i != n: c, p = [int(x) for x in input().split()] requests.append((p, c, i + 1)) i += 1 k = int(input()) R = input().split() r = [] for x in range(len(R)): r.append((int(R[x]), x + 1)) m = s = 0 accepted = [] maxx = 0, 0, 0 reverse_sorted_requests = list(reversed(sorted(requests))) for j in sorted(r): for i in reverse_sorted_requests: if maxx[2] == 0 and i[1] <= j[0]: maxx = i elif i[0] > maxx[0] and i[1] <= j[0]: maxx = i if maxx[2] != 0: accepted.append((maxx[2], j[1])) m += 1 s += maxx[0] reverse_sorted_requests.remove(maxx) maxx = 0, 0, 0 print(m, s) for a in accepted: print(a[0], a[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) c = [] for i in range(n): c.append(list(map(int, input().split()))) c[i].append(i + 1) r = int(input()) k = list(map(int, input().split())) def sortfn(inp): return inp[1] c.sort(key=sortfn, reverse=True) for i in range(len(k)): val = k[i] k[i] = [i + 1, val] k.sort(key=sortfn) def gettable(size): for i in range(len(k)): if k[i][1] >= size: k[i][1] = -1 table = k[i][0] return table return -1 groups = [] val = 0 for group in c: table = gettable(group[0]) if table > 0: groups.append([str(group[2]), str(table)]) val += group[1] print(str(len(groups)) + " " + str(val)) for g in groups: print(" ".join(g))
ASSIGN VAR FUNC_CALL VAR 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 VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
import sys input = sys.stdin.readline read_tuple = lambda _type: map(_type, input().split(" ")) def solve(): n = int(input()) c_p = [] for i in range(n): c_i, p_i = read_tuple(int) c_p.append((c_i, p_i, i + 1)) k = int(input()) r = [(r_i, i + 1) for i, r_i in enumerate(read_tuple(int))] c_p.sort(key=lambda x: x[1]) r.sort(key=lambda x: x[0]) m = 0 s = 0 info = [] while c_p and r: c_i, p_i, i = c_p[-1] if c_i > r[-1][0]: c_p.pop() else: idx = 0 r_j, j = r[idx] while r_j < c_i: idx += 1 r_j, j = r[idx] m += 1 s += p_i info.append((i, j)) c_p.pop() r.pop(idx) print(m, s) for i, j in sorted(info): print(i, j) solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) clients = [] for i in range(1, n + 1): clients.append(list(map(int, input().split()))) clients[-1].reverse() clients[-1] += [i] k = int(input()) r = list(map(int, input().split())) s = 0 answer = [] clients.sort(reverse=True) r = [1001] + r for i in clients: pos = 0 for j in range(k + 1): if r[j] >= i[1] and r[j] < r[pos]: pos = j if r[pos] >= i[1] and pos: answer.append(str(i[2]) + " " + str(pos)) r[pos] = 0 s += i[0] print(len(answer), s) print("\n".join(answer))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
import sys n = int(sys.stdin.readline()) request = [] for i in range(1, n + 1): people, cost = map(int, sys.stdin.readline().split()) request.append((i, people, cost)) k = int(sys.stdin.readline()) tables = [] for tmp in zip(range(1, k + 1), [int(s) for s in sys.stdin.readline().split()]): tables.append(tmp) tables.sort(key=lambda item: item[1]) total = 0 ans = [] used = [(False) for i in range(n + 1)] count = 0 for i, t in tables: max_price = 0 request_id = -1 for j, c, p in request: if p > max_price and c <= t and not used[j]: max_price = p request_id = j if request_id > 0: count += 1 used[request_id] = True ans.append((request_id, i)) total += max_price print(str(count) + " " + str(total)) for request_id, i in ans: print(str(request_id) + " " + str(i))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) a = [list(map(int, input().split())) for i in range(n)] k = int(input()) b = list(map(int, input().split())) r = max(b) for i in range(n): if a[i][0] > r: a[i].append(1) a[i] = a[i][::-1] a[i].append(i + 1) else: a[i].append(0) a[i] = a[i][::-1] a[i].append(i + 1) a.sort(reverse=True) c = [[] for i in range(1001)] for i in range(k): c[b[i]].append(i + 1) mo = 0 ans = [] for i in range(n): if not a[i][0]: for j in range(a[i][2], r + 1): if len(c[j]) > 0: mo += a[i][1] ans.append([a[i][-1], c[j][0]]) del c[j][0] break print(len(ans), mo) for i in ans: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
n = int(input()) q = [] for i in range(n): x, y = map(int, input().split()) q.append((y, x, i + 1)) m = int(input()) t = list(map(int, input().split())) t = list(enumerate(t, 1)) t = list(map(lambda x: [x[1], x[0]], t)) q.sort() t.sort() res, sum = [], 0 for i in range(n - 1, -1, -1): for j in range(m): if q[i][1] <= t[j][0]: t[j][0] = 0 sum += q[i][0] res.append((q[i][2], t[j][1])) break print(len(res), sum) for i in res: print(*i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Innovation technologies are on a victorious march around the planet. They integrate into all spheres of human activity! A restaurant called "Dijkstra's Place" has started thinking about optimizing the booking system. There are n booking requests received by now. Each request is characterized by two numbers: c_{i} and p_{i} β€” the size of the group of visitors who will come via this request and the total sum of money they will spend in the restaurant, correspondingly. We know that for each request, all c_{i} people want to sit at the same table and are going to spend the whole evening in the restaurant, from the opening moment at 18:00 to the closing moment. Unfortunately, there only are k tables in the restaurant. For each table, we know r_{i} β€” the maximum number of people who can sit at it. A table can have only people from the same group sitting at it. If you cannot find a large enough table for the whole group, then all visitors leave and naturally, pay nothing. Your task is: given the tables and the requests, decide which requests to accept and which requests to decline so that the money paid by the happy and full visitors was maximum. -----Input----- The first line of the input contains integer n (1 ≀ n ≀ 1000) β€” the number of requests from visitors. Then n lines follow. Each line contains two integers: c_{i}, p_{i} (1 ≀ c_{i}, p_{i} ≀ 1000) β€” the size of the group of visitors who will come by the i-th request and the total sum of money they will pay when they visit the restaurant, correspondingly. The next line contains integer k (1 ≀ k ≀ 1000) β€” the number of tables in the restaurant. The last line contains k space-separated integers: r_1, r_2, ..., r_{k} (1 ≀ r_{i} ≀ 1000) β€” the maximum number of people that can sit at each table. -----Output----- In the first line print two integers: m, s β€” the number of accepted requests and the total money you get from these requests, correspondingly. Then print m lines β€” each line must contain two space-separated integers: the number of the accepted request and the number of the table to seat people who come via this request. The requests and the tables are consecutively numbered starting from 1 in the order in which they are given in the input. If there are multiple optimal answers, print any of them. -----Examples----- Input 3 10 50 2 100 5 30 3 4 6 9 Output 2 130 2 1 3 2
import sys class Visitor: def __init__(self, pi, ci, i): self.ci = ci self.pi = pi self.i = i self.place = None def __repr__(self): return "{} {} {}".format(self.ci, self.pi, self.i) class Place: def __init__(self, i, max_): self.i = i self.max_ = int(max_) def __repr__(self): return "{} {}".format(self.max_, self.i) lines = sys.stdin.readlines() n = int(lines[0]) visitors = [] for i in range(1, n + 1): ci, pi = lines[i].split() visitors.append(Visitor(int(pi), int(ci), i)) places = [] for i, place in enumerate(lines[n + 2].split()): places.append(Place(i + 1, place)) visitors = sorted(visitors, key=lambda x: x.pi)[::-1] places = sorted(places, key=lambda x: x.max_) visited = [None for _ in places] total = 0 for visitor in visitors: for i, place in enumerate(places): if visitor.ci <= place.max_ and visited[i] is None: visited[i] = visitor visitor.place = place total += visitor.pi break new_visitors = list( filter(lambda x: x.place is not None, sorted(visitors, key=lambda x: x.i)) ) print("{} {}".format(len(new_visitors), total)) for visitor in new_visitors: print("{} {}".format(visitor.i, visitor.place.i))
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FUNC_DEF RETURN FUNC_CALL STRING VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NONE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR