description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys def BinSearch(): pass t = int(sys.stdin.readline()) for _ in range(t): n, s = map(int, sys.stdin.readline().split()) tmp = [tuple(map(int, sys.stdin.readline().split())) for _ in range(n)] mid = s r = s l = 0 tmp.sort() while True: tp = [] tp1 = 0 tmpSum = 0 for x in tmp: if x[0] >= mid: tp1 += 1 tmpSum += x[0] elif x[1] >= mid: tp.append(x) else: tmpSum += x[0] q = len(tp) qq = (n + 1) // 2 - tp1 for i in range(q): if i >= qq: tmpSum += tp[q - 1 - i][0] if qq > q or tmpSum + qq * mid > s: if r - l <= 1: print(r - 1) break r = mid else: if r - l <= 1: print(l) break l = mid mid = l + (r - l) // 2
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
from sys import stdin input = stdin.readline q = int(input()) for rwre in range(q): n, s = map(int, input().split()) przed = [list(map(int, input().split())) for i in range(n)] przed.sort() przed.reverse() l = 1 p = 10**9 while abs(p - l) > 0: mozna = 1 sr = (p + l + 1) // 2 duze = [przed[i] for i in range(n) if przed[i][1] >= sr] male = [przed[i] for i in range(n) if przed[i][1] < sr] if len(duze) <= n // 2: mozna = 0 else: spent = 0 dudes = 0 for i in range(n // 2 + 1): spent += max(sr, duze[i][0]) dudes = n // 2 + 1 duze = duze[n // 2 + 1 :] for du in duze: spent += du[0] for ma in male: spent += ma[0] if spent > s: mozna = 0 if mozna == 1: l = sr else: p = sr - 1 print((p + l) // 2)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys readline = sys.stdin.readline rdw = lambda: readline().split() rdwl = lambda: list(readline().split()) rdi = lambda: int(readline()) rdis = lambda: map(int, readline().split()) rdisl = lambda: list(map(int, readline().split())) rdrows = lambda cnt: [tuple(rdis()) for _ in range(cnt)] def solve(): n, s = rdis() cm = n + 1 >> 1 a = rdrows(n) a.sort() l, r = a[n >> 1][0], 10**9 + 1 res = l while l <= r: m = l + r >> 1 need = 0 cnt = 0 for i in range(n - 1, -1, -1): li, ri = a[i] if li >= m: need += li cnt += 1 elif cnt < cm and ri >= m: need += m cnt += 1 else: need += li if cnt >= cm and need <= s: res = m l = m + 1 else: r = m - 1 print(res) tests = 1 tests = rdi() for testnum in range(tests): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN 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 BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N, S = map(int, input().split()) LR = [list(map(int, input().split())) for _ in range(N)] Query.append((N, S, LR)) for N, S, LR in Query: G = [] for L, _ in LR: G.append(L) G.sort() l = G[N // 2] r = S + 1 while r - l > 1: m = (l + r) // 2 A = [] B = [] C = [] for L, R in LR: if L <= m <= R: C.append(L) elif R < m: A.append(L) else: B.append(L) C.sort() if len(A) < N // 2 + 1 <= len(A) + len(C): if ( sum(A) + sum(B) + sum(C[: N // 2 - len(A)]) + m * (len(A) + len(C) - N // 2) <= S ): l = m else: r = m else: r = m print(l)
IMPORT ASSIGN VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
from sys import stdin, stdout def fun1(n, m, l, s): cl = 0 cr = 0 cm = 0 for i in range(n): if l[i][1] < m: s -= l[i][0] cl += 1 elif l[i][0] > m: s -= l[i][0] cr += 1 else: cm += 1 if cm + cr <= 1 + n // 2: s -= m else: s -= l[i][0] if s < 0 or cr > n // 2 or cl > n // 2: return 0 return 1 def search(n, low, high, l, su): l1 = l[::-1] while low < high: mid = (low + high) // 2 if fun1(n, mid, l1, su): low = mid + 1 else: high = mid if fun1(n, low, l1, su): return low return low - 1 def main(): for _ in range(int(stdin.readline())): n, su = [int(x) for x in stdin.readline().split()] l = [[-1, -1] for i in range(n)] l2 = [(-1) for i in range(n)] for i in range(n): inp = [int(j) for j in stdin.readline().split()] l[i] = inp l2[i] = inp[0] l.sort() low = int(l[n // 2][0]) print(search(n, low, 10**9 + 1, l, su)) main()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, s = list(map(int, input().split())) human = [] x = n // 2 + 1 for _ in range(n): human.append(list(map(int, input().split()))) human.sort(reverse=True) remain = s for i in range(n): remain -= human[i][0] right = 10**9 + 1 left = 0 while right - left > 1: mid = left + (right - left) // 2 count = 0 money = remain for i in range(n): if human[i][1] < mid: continue elif human[i][0] >= mid: count += 1 continue elif money >= mid - human[i][0]: money -= mid - human[i][0] count += 1 else: break if count >= x: left = mid else: right = mid print(left)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER 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 NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n, s = map(int, input().split()) L = [0] * n R = [0] * n RL = [0] * n for i in range(n): l, r = map(int, input().split()) L[i] = l R[i] = r RL[i] = r, l L.sort() R.sort() def is_ok(x): t = 0 cnt1 = 0 cnt2 = 0 g = [] for i in range(n): r, l = RL[i] if r < x: t += l elif x <= l: cnt1 += 1 t += l else: cnt2 += 1 g.append(l) g.sort() need = (n + 1) // 2 - cnt1 for i in range(len(g) - need): t += g[i] t += x * need if t <= s: return True else: return False ok = L[n // 2] ng = R[n // 2] + 1 while ok + 1 < ng: c = (ok + ng) // 2 if is_ok(c): ok = c else: ng = c print(ok)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER 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 EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
from sys import stdin, stdout def getmaximummediansalary(lra, n, s): l = 0 h = s while l < h: m = (l + h + 1) // 2 if candistribute(m, lra, s): l = m else: h = m - 1 return l def candistribute(m, lra, s): cntl = 0 cntm = 0 cnth = 0 sum = 0 target = [] for lr in lra: if m > lr[1]: sum += lr[0] cntl += 1 elif m < lr[0]: sum += lr[0] cnth += 1 else: target.append(lr[0]) cntm += 1 mi = len(lra) // 2 if cntl + cntm < mi: return True if cntm + cnth <= mi: return False need = mi - cntl for i in range(len(target)): if i < need: sum += target[i] else: sum += m return sum <= s t = int(stdin.readline()) for i in range(t): ns = list(map(int, stdin.readline().split())) n = ns[0] s = ns[1] lra = [] for j in range(n): lra.append(list(map(int, stdin.readline().split()))) lra.sort(key=lambda x: x[0]) res = getmaximummediansalary(lra, n, s) stdout.write(str(res) + "\n")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline test = int(input()) for i in range(test): numbers = list(map(int, input().split())) people = numbers[0] salary = numbers[1] interv = [] nr = 0 for j in range(people): v = list(map(int, input().split())) left = v[0] right = v[1] nr = nr + left interv.append([left, right]) left = 0 right = 1000000001 while right > left + 1: m = (left + right) // 2 fin = [] for j in range(people): if m <= interv[j][1]: if -interv[j][0] < -m: fin.append(-m) else: fin.append(-interv[j][0]) x = len(fin) if x < people // 2 + 1: right = m continue fin.sort() suma = nr + (people // 2 + 1) * m + sum(fin[: people // 2 + 1]) if suma > salary: right = m else: left = m print(left)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.buffer.readline def check(num): count1 = 0 count2 = 0 spent = 0 left = [] for i in l: if i[0] > num: count2 += 1 spent += i[0] elif i[1] < num: count1 += 1 spent += i[0] else: left.append(i) if count2 > n // 2 + 1: return True elif count1 > n // 2: return False else: for i in left: if count2 != n // 2 + 1: spent += num count2 += 1 else: spent += i[0] count1 += 1 if count2 == n // 2 + 1 and count1 == n // 2 and spent <= s: return True return False for nt in range(int(input())): n, s = map(int, input().split()) l = [] for i in range(n): a, b = map(int, input().split()) l.append((a, b)) l.sort(reverse=True) if n == 1: print(min(l[0][1], s)) continue low = 0 high = 10**10 while low < high: mid = (low + high) // 2 if check(mid): low = mid + 1 else: high = mid - 1 if check(low): print(low) else: print(low - 1)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
from sys import stdin t = int(stdin.readline()) for _ in range(t): n, s = map(int, stdin.readline().split()) lr = [list(map(int, stdin.readline().split())) for i in range(n)] lr.sort(reverse=True) l = 0 r = s + 1 while l + 1 < r: mid = (l + r) // 2 right_cnt = 0 salary = 0 for p in lr: if p[0] > mid: right_cnt += 1 salary += p[0] elif p[0] <= mid <= p[1]: if right_cnt < (n + 1) // 2: right_cnt += 1 salary += mid else: salary += p[0] elif mid > p[1]: salary += p[0] if salary <= s and right_cnt >= (n + 1) // 2: l = mid else: r = mid print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline def judge(x): salary = [] left, right = 0, 0 l = [] for li, ri in lr: if ri < x: salary.append(li) left += 1 elif x < li: salary.append(li) right += 1 else: l.append((li, ri)) if left > (n - 1) // 2 or right > (n - 1) // 2: return False l.sort(key=lambda k: k[0]) for i in range(len(l)): if i < (n - 1) // 2 - left: salary.append(l[i][0]) else: salary.append(x) return sum(salary) <= s def binary_search(l): r = 10**10 while l <= r: mid = (l + r) // 2 if judge(mid): l = mid + 1 else: r = mid - 1 return r t = int(input()) for _ in range(t): n, s = map(int, input().split()) lr = [tuple(map(int, input().split())) for _ in range(n)] ls = [li for li, _ in lr] ls.sort() ans = binary_search(ls[n // 2]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF 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 BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep="\n") def solve(): n, s = nm() m = (n + 1) // 2 emp = [tuple(nm()) for _ in range(n)] emp.sort(reverse=True) ok = emp[m - 1][0] ng = s + 1 while ng - ok > 1: x = (ok + ng) // 2 cur = 0 t = 0 for l, r in emp: if r >= x and t < m: cur += max(x, l) t += 1 else: cur += l if t == m and cur <= s: ok = x else: ng = x print(ok) return T = ni() for _ in range(T): solve()
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP 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 VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys def highest_median(ranges, s): n = len(ranges) ranges = sorted(ranges) low = ranges[n // 2][0] high = sorted(ranges, key=lambda x: x[1])[n // 2][1] + 1 while low + 1 < high: med = low + (high - low) // 2 smaller = 0 larger = 0 total = 0 for l, h in ranges: if l > med: smaller += 1 total += l elif h < med: larger += 1 total += l if smaller > n // 2: low = med continue if larger > n // 2: high = med continue for l, h in ranges: if larger == n // 2: break if l <= med <= h: larger += 1 total += l total += med * (n - larger - smaller) if total > s: high = med else: low = med return low input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ t = int(input().strip()) for i in range(t): line = input().strip() vs = [int(v) for v in line.split(" ")] n, s = vs[0], vs[1] ranges = [] for j in range(n): line = input().strip() vs = [int(v) for v in line.split(" ")] ranges.append(vs) print(highest_median(ranges, s))
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline def solve(mid): ans = 0 cnt = 0 tmp = [] for i in range(n): l, r = info[i] if r < mid: ans += l elif mid < l: ans += l cnt += 1 else: tmp.append(l) tmp.sort(reverse=True) nokori = (n + 1) // 2 - cnt for i in tmp: if nokori > 0: ans += mid nokori -= 1 else: ans += i if ans <= s and nokori <= 0: return True else: return False q = int(input()) ans = [0] * q for qi in range(q): n, s = map(int, input().split()) info = [list(map(int, input().split())) for i in range(n)] ok = 0 ng = s + 1 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if solve(mid): ok = mid else: ng = mid ans[qi] = ok print("\n".join(map(str, ans)))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys INF = 10**9 sys.setrecursionlimit(10**8) input = sys.stdin.buffer.readline t = int(input()) for i in range(t): n, s = [int(item) for item in input().split()] lr = [] for j in range(n): l, r = [int(item) for item in input().split()] lr.append((l, r)) lft = 0 rgt = 10**9 + 1 while rgt - lft > 1: mid = (lft + rgt) // 2 minimum_cost = 0 takable = [] for l, r in lr: minimum_cost += l if mid <= r: takable.append(max(l - mid, 0) - l) if len(takable) < n // 2 + 1: rgt = mid continue takable.sort() cost = sum(takable[: n // 2 + 1]) + minimum_cost + mid * (n // 2 + 1) if cost <= s: lft = mid else: rgt = mid print(lft)
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys readline = sys.stdin.readline inf = 10**16 def calc(m, L, R): N = len(L) cl = 0 cr = 0 ss = 0 candi = [] for i in range(N): if L[i] > m: cr += 1 ss += L[i] elif R[i] < m: cl += 1 ss += L[i] else: candi.append(L[i]) cm = len(candi) if cl > N // 2: return inf k = min(cm, N // 2 - cl) candi.sort() ss += sum(candi[:k]) ss += (cm - k) * m return ss T = int(readline()) Ans = [None] * T for qu in range(T): M, LS = map(int, readline().split()) L = [None] * M R = [None] * M for i in range(M): L[i], R[i] = map(int, readline().split()) ok = 0 ng = 10**9 + 1 while abs(ok - ng) > 1: med = (ok + ng) // 2 if calc(med, L, R) <= LS: ok = med else: ng = med Ans[qu] = ok print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys input = sys.stdin.readline t = int(input()) def ok(mid, l, n, s): su = 0 cnt = 0 ll = [] for i in range(n): if l[i][1] < mid: su += l[i][0] elif l[i][0] >= mid: su += l[i][0] cnt += 1 else: ll.append(l[i]) ll.sort() ne = max(0, (n + 1) // 2 - cnt) if ne > len(ll): return False for i in range(len(ll)): if i < len(ll) - ne: su += ll[i][0] else: su += mid return su <= s while t: l = [] n, s = map(int, input().split()) for i in range(n): ll, r = map(int, input().split()) l.append((ll, r)) l.sort() lf = 0 r = 10**9 + 100 while r - lf > 1: mid = lf + (r - lf) // 2 if ok(mid, l, n, s): lf = mid else: r = mid print(lf) t -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
You are the head of a large enterprise. $n$ people work at you, and $n$ is odd (i. e. $n$ is not divisible by $2$). You have to distribute salaries to your employees. Initially, you have $s$ dollars for it, and the $i$-th employee should get a salary from $l_i$ to $r_i$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $[5, 1, 10, 17, 6]$ is $6$, the median of the sequence $[1, 2, 1]$ is $1$. It is guaranteed that you have enough money to pay the minimum salary, i.e $l_1 + l_2 + \dots + l_n \le s$. Note that you don't have to spend all your $s$ dollars on salaries. You have to answer $t$ test cases. -----Input----- The first line contains one integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The first line of each query contains two integers $n$ and $s$ ($1 \le n < 2 \cdot 10^5$, $1 \le s \le 2 \cdot 10^{14}$) — the number of employees and the amount of money you have. The value $n$ is not divisible by $2$. The following $n$ lines of each query contain the information about employees. The $i$-th line contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le 10^9$). It is guaranteed that the sum of all $n$ over all queries does not exceed $2 \cdot 10^5$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. $\sum\limits_{i=1}^{n} l_i \le s$. -----Output----- For each test case print one integer — the maximum median salary that you can obtain. -----Example----- Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Output 11 1337 6 -----Note----- In the first test case, you can distribute salaries as follows: $sal_1 = 12, sal_2 = 2, sal_3 = 11$ ($sal_i$ is the salary of the $i$-th employee). Then the median salary is $11$. In the second test case, you have to pay $1337$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$. Then the median salary is $6$.
import sys def main(): def getLowestPossibleMedian(lr, n): lr.sort(key=lambda x: x[0]) return lr[n // 2][0] def checkPossibleMedian(lr, guess, n, s): potentialUpper = [] lower = [] lowerCnts = n // 2 upperCnts = n - lowerCnts for l, r in lr: if r >= guess: potentialUpper.append([l, r]) else: lower.append([l, r]) if len(potentialUpper) < upperCnts: return False potentialUpper.sort(key=lambda x: x[0], reverse=True) for i in range(upperCnts, len(potentialUpper)): lower.append(potentialUpper[i]) assert len(lower) == lowerCnts requiredSalary = 0 for l, r in lower: requiredSalary += l for i in range(upperCnts): requiredSalary += max(guess, potentialUpper[i][0]) return requiredSalary <= s t = int(input()) allans = [] for _ in range(t): n, s = readIntArr() lr = [] for __ in range(n): lr.append(readIntArr()) m = getLowestPossibleMedian(lr, n) ans = m b = s while b > 0: while checkPossibleMedian(lr, ans + b, n, s): ans += b b //= 2 allans.append(ans) multiLineArrayPrint(allans) return input = lambda: sys.stdin.readline().rstrip("\r\n") def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 998244353 for _abc in range(1): main()
IMPORT FUNC_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
import sys def input(): return sys.stdin.readline().rstrip() class STR: def __init__(self, x, y): self.s = x self.h = y def __gt__(self, other): ss, sh = self.s, self.h os, oh = other.s, other.h return oh * ss <= os * sh n = int(input()) ans = 0 que = [] for i in range(n): S = list(input()) L = len(S) ccnt = 0 hcnt = 0 for j in range(L - 1, -1, -1): if S[j] == "h": hcnt += 1 else: ccnt += 1 ans += hcnt que.append(STR(ccnt, hcnt)) que.sort(reverse=True) ccnt, hcnt = 0, 0 for v in que: nows, nowh = v.s, v.h ans += nows * hcnt hcnt += nowh print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
get = lambda: map(int, input().split()) f = next(get()) a = [] for i in range(f): s = input() a.append(s) def key(y): t = y.count("s") if not t: return float("inf") return -(t - len(y)) / t a.sort(key=key) scnt = 0 ans = 0 s = "".join(a) for i in s: if i == "s": scnt += 1 else: ans += scnt print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR RETURN FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
def mycmp(a, b): if a[0] * b[1] > b[0] * a[1]: return -1 return 1 n = int(input()) a = [] ans = 0 for i in range(1, n + 1): str = input() totlen = len(str) s = h = 0 for j in range(0, totlen): if str[j] == "s": s = s + 1 else: h = h + 1 ans = ans + s tmp = 0 if h == 0: tmp = 1e18 else: tmp = s / h a.append((int(s), int(h), tmp)) a.sort(key=lambda x: x[2], reverse=True) sum = 0 for i in range(0, len(a)): ans = ans + sum * a[i][1] sum = sum + a[i][0] print(ans)
FUNC_DEF IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) s = [input() for i in range(n)] s = [(x, x.count("s"), x.count("h")) for x in s] s.sort(key=lambda x: x[2] / float(x[1] + 1e-06)) s = "".join(x[0] for x in s) pre, ans = 0, 0 for x in s: if x == "s": pre += 1 else: ans += pre print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
import sys f = sys.stdin ans = 0 od = 0 n = int(f.readline()) s = [f.readline().strip() for _ in range(n)] s = [(x, x.count("s"), x.count("h")) for x in s] s = sorted(s, key=lambda x: x[2] / float(x[1] + 1e-07)) s = "".join(x[0] for x in s) for x in s: if x == "s": od += 1 else: ans += od print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR NUMBER VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) l = [] for i in range(n): l.append(input()) l.sort(key=lambda s: -(s.count("s") / (s.count("h") + 1e-07))) s = "".join(l) count = 0 l = [0] * len(s) for i in range(len(s)): if s[i] == "s": count += 1 l[i] = count count = 0 for i in range(1, len(s)): if s[i] == "h": count += l[i - 1] print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) li = [] for i in range(n): inp = input() li.append([inp.count("h") / len(inp), inp]) li.sort() ans = "" for i in range(n): ans += li[i][1] count = 0 pri = 0 for i in range(1, len(ans) + 1): if ans[-i] == "h": count += 1 else: pri += count print(pri)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) a = [] for i in range(n): b = input() a.append([b.count("h") / len(b), b]) a.sort() b = "" for i in range(n): b = b + a[i][1] c = 0 s = 0 for i in range(len(b)): if b[i] == "s": c = c + 1 if b[i] == "h": s = s + c print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
def key(x): try: return x.count("h") / x.count("s") except ZeroDivisionError: return 10**9 n = int(input()) t = "".join(sorted((input() for _ in range(n)), key=key)) res, cnt = 0, 0 for ti in t: if ti == "s": cnt += 1 if ti == "h": res += cnt print(res)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) ts = [] res = 0 for _ in range(n): i = input().strip() m_res = 0 s_count = 0 for c in i: if c == "s": s_count += 1 else: m_res += s_count res += m_res ts.append((i.count("s"), i.count("h"))) ts = sorted( ts, key=lambda k: float(k[0]) / float(k[1]) if k[1] else float("inf"), reverse=True ) ss = [s for s, h in ts] hs2 = [h for s, h in ts] hs = [(0) for _ in ts] for i in range(n - 2, -1, -1): hs[i] = hs2[i + 1] + hs[i + 1] for i in range(n): res += ss[i] * hs[i] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) a = [] for i in range(n): k = input() s, t = 0, 0 for j in k: if j == "s": s += 1 else: t += 1 if t > 0: a.append([k, s / t]) else: a.append([k, 100000]) a.sort(key=lambda x: x[1]) a = list(reversed(a)) l = a[0][0][0] x = 0 m = [] for i in a: for j in i[0]: if j != l: m.append(x) x = 1 l = j else: x += 1 m.append(x) if a[0][0][0] == "h": m = m[1:] p, q = [], [] e = 0 z = len(m) for i in range(z): if i % 2 == 0: p.append(m[i]) m = list(reversed(m)) if z % 2 == 1: m = m[1:] z -= 1 for i in range(z): if i % 2 == 0: e += m[i] q.append(e) q = list(reversed(q)) ans = 0 for i in range(z // 2): ans += p[i] * q[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) def insert(s, t, ret, string, c, scount, tcount): maxval = s * tcount val = maxval maxpos = 0 for i in range(len(string)): si, ti, ci = string[i] val = val + si * t * ci - s * ti * ci if maxval < val: maxval = val maxpos = i + 1 string = string[:maxpos] + [(s, t, c)] + string[maxpos:] return ret + maxval * c, string def count2(m, memo): if m in memo: return memo[m] s = 0 ret = 0 for c in m: if c == "s": s += 1 else: ret += s memo[m] = ret, s return ret, s memo = {} ret = 0 d = {} for _ in range(n): m = input() c, s = count2(m, memo) ret += c t = len(m) - s if (s, t) not in d: d[s, t] = 0 ret += s * t * d[s, t] d[s, t] += 1 string = [] scount = 0 tcount = 0 for s, t in d: c = d[s, t] ret, string = insert(s, t, ret, string, c, scount, tcount) scount += s * c tcount += t * c print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
class Node(object): def __init__(self, s, h, sh): self.s = s self.h = h self.sh = sh def __lt__(self, other): return self.s * other.h > other.s * self.h M = int(100000.0 + 5) c = [] n = int(input()) for i in range(n): ss = input() sv, hv, shv = 0, 0, 0 for e in ss: if e == "s": sv += 1 else: shv += sv hv += 1 c.append(Node(sv, hv, shv)) c = sorted(c) pre_s, ans = 0, 0 for e in c: ans += e.sh + pre_s * e.h pre_s += e.s print(ans)
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) s = [] a = [] for i in range(n): s.append(input()) p = 0 q = 0 for j in s[i]: if j == "s": p += 1 elif j == "h": q += 1 val = (q - p) / (q + p) a.append([val, i]) a.sort() fin = "" for i in range(n): fin += s[a[i][1]] p = 0 q = 0 ins = 0 for i in fin: if i == "s": q += 1 elif i == "h": p += 1 ins += q print(ins)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
def key(x): s = x.count("s") return s / len(x) def sh_produce(x): x = str(x) sh = [(0) for j in range(len(x))] if x[-1] == "h": sh[-1] = 1 for i in range(len(x) - 2, -1, -1): if x[i] == "h": sh[i] = sh[i + 1] + 1 else: sh[i] = sh[i + 1] sh_num = 0 for i in range(len(x)): if x[i] == "s": sh_num += sh[i] return sh_num n = int(input()) input_list = [] for i in range(n): tmp = input() input_list.append(tmp) input_list = sorted(input_list, key=key, reverse=True) res = "".join(input_list) print(sh_produce(res))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) mas = [] all_h = 0 for i in range(n): line = input() s = line.count("s") h = line.count("h") all_h += h if h == 0: k = 100000 else: k = s / h mas.append((k, line)) sor = sorted(mas, key=lambda val: val[0], reverse=True) str = "" for item in sor: str += item[1] count = 0 count_h = all_h for i in range(len(str)): if str[i] == "h": count_h -= 1 else: count += count_h print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) a = [] for i in range(n): s = input() l = 0 for c in s: if c == "s": l += 1 a.append((s, l / len(s))) a.sort(key=lambda x: x[1], reverse=True) ns = 0 ans = 0 for st, _ in a: for c in st: if c == "s": ns += 1 else: ans += ns print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
def count_s(s): ret = 0 for k in s: if k == "s": ret += 1 return ret def count_sh(s): ret = 0 ls = [] for k in s: if k == "s": ls.append(0) else: ls.append(1) ls.reverse() for i in range(1, len(ls)): ls[i] += ls[i - 1] ls.reverse() for i in range(0, len(s) - 1): if s[i] == "s": ret += ls[i] return ret n = int(input()) ls = [] ans = 0 for _ in range(n): s = input() cnt = count_s(s) ans += count_sh(s) if cnt == len(s): ls.append([s, 100000000000, cnt, 0]) else: ls.append([s, cnt * 1.0 / (len(s) - cnt), cnt, len(s) - cnt]) ls.sort(key=lambda x: x[1], reverse=True) ls.reverse() for i in range(1, len(ls)): ls[i][3] += ls[i - 1][3] ls.reverse() for i in range(0, len(ls) - 1): ans += ls[i][2] * ls[i + 1][3] print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Pushok the dog has been chasing Imp for a few hours already. $48$ Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and $t_{i} = s$ and $t_{j} = h$. The robot is off at the moment. Imp knows that it has a sequence of strings t_{i} in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of strings in robot's memory. Next n lines contain the strings t_1, t_2, ..., t_{n}, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 10^5. -----Output----- Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. -----Examples----- Input 4 ssh hs s hhhs Output 18 Input 2 h s Output 1 -----Note----- The optimal concatenation in the first sample is ssshhshhhs.
n = int(input()) a = [] for i in range(0, n): a.append(input()) noise = 0 def calc_noise(str): result = 0 struct = [] struct.append(0) c = "s" j = 0 str.lstrip("h") for i in range(0, len(str)): if str[i] == c: struct[j] += 1 else: c = str[i] struct.append(1) j += 1 s = 0 for i in range(0, len(struct)): if i % 2 != 0: result += s * struct[i] else: s += struct[i] return result v = [] for i in range(0, n): cs = a[i].count("s") ch = len(a[i]) - cs sound = calc_noise(a[i]) if cs == 0: v.append([i, -100000, 0]) elif ch == 0: v.append([i, 100000, 0]) else: v.append([i, cs / ch, sound]) v.sort(key=lambda x: x[2], reverse=1) v.sort(key=lambda x: x[1], reverse=1) str = "" for i in range(0, n): str += a[v[i][0]] noise = calc_noise(str) print(noise)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
import sys n, m, c = map(int, input().split()) l = [(0) for _ in range(n)] for j in range(m): p = int(input()) if p > c // 2: i = n - 1 while i > 0 and p <= l[i]: i -= 1 l[i] = p else: i = 0 while l[i] != 0 and i < n - 1 and p >= l[i]: i += 1 l[i] = p print(i + 1) sys.stdout.flush() if 0 not in l and sorted(l) == l: break
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF NUMBER VAR FUNC_CALL VAR VAR VAR
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
import sys n, m, c = map(int, input().split()) free = n nums = [0] * n for i in range(m): pi = int(input()) if pi > c // 2: current = n - 1 while nums[current] > 0 and nums[current] >= pi and current >= 0: current -= 1 if current == -1: current = 0 if nums[current] == 0: free -= 1 nums[current] = pi print(current + 1) else: current = 0 while current < n and nums[current] > 0 and nums[current] <= pi: current += 1 if current == n: current = n - 1 if nums[current] == 0: free -= 1 nums[current] = pi print(current + 1) if free == 0: break
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
def main(): n, m, c = map(int, input().split()) middle = (c + 1) // 2 * 2 + 1 xs = [middle for _ in range(n)] def value(a, b): if a > middle: return b - a elif a < middle: return a - b return c for _ in range(m): sheet = int(input().strip()) * 2 best_i = -1 best_value = -1 for i in range(n): if (i == 0 or xs[i - 1] <= sheet) and (i == n - 1 or sheet <= xs[i + 1]): tmp_value = value(xs[i], sheet) if tmp_value > best_value: best_value = tmp_value best_i = i xs[best_i] = sheet print(best_i + 1, flush=True) if not any(x == middle for x in xs): break main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
import sys n, m, c = [int(x) for x in input().split()] l = 0 r = 0 smalldata = [c + 1] * n largedata = [-1] * n small = c // 2 large = c - small for i in range(m): num = int(input()) if num <= small: ind = 0 while smalldata[ind] <= num: ind += 1 smalldata[ind] = num l = 1 while l < n and smalldata[l] != c + 1 and smalldata[l - 1] <= smalldata[l]: l += 1 else: ind = n - 1 while largedata[ind] >= num: ind -= 1 largedata[ind] = num r = 1 while r < n and largedata[-r - 1] != -1 and largedata[-r] >= largedata[-r - 1]: r += 1 print(ind + 1) sys.stdout.flush() if l + r >= n: break
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
n, m, c = map(int, input().split()) a = [0] * n while a.count(0): x = int(input()) if x > c // 2: i = n - 1 while a[i] >= x: i -= 1 a[i] = x else: i = 0 while a[i] and a[i] <= x: i += 1 a[i] = x print(i + 1)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
def get_int(string, n): i = j = k = 0 for s in string: k += 1 for s in string: if i == n - 1: break if s == " ": i += 1 j += 1 i = 0 while j < k: if string[j] == " ": break i = 10 * i + int(string[j]) j += 1 return i def check_order(ls, n): for i in ls: if i == None: return 0 for i in range(0, n - 1): if ls[i] > ls[i + 1]: return -1 return 1 def forward(p, ls, n): for i in range(0, n): if ls[i] == None: ls[i] = p print(i + 1) return 0 if ls[i] > p: ls[i] = p print(i + 1) return 0 def reverse(p, ls, n): for i in range(0, n): if ls[n - 1 - i] == None: ls[n - 1 - i] = p print(n - i) return 0 elif ls[n - 1 - i] < p: ls[n - i - 1] = p print(n - i) return 0 x = input() n = get_int(x, 1) m = get_int(x, 2) c = get_int(x, 3) ls = [] for i in range(0, n): ls += [None] for i in range(0, m): p = int(input()) if p > c / 2: reverse(p, ls, n) else: forward(p, ls, n) if check_order(ls, n) == 1: break
FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR VAR IF VAR NONE RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NONE ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
import sys def output(n): print(n) sys.stdout.flush() n, m, c = map(int, input().split()) arr = [(0) for _ in range(n)] l = -1 r = n cnt = 0 while cnt < n: p = int(input()) if p <= (c + 1) // 2: for i in range(0, r): if arr[i] == 0: l += 1 output(i + 1) arr[i] = p break elif p < arr[i]: output(i + 1) arr[i] = p break else: for i in range(n - 1, l, -1): if arr[i] == 0: r -= 1 output(i + 1) arr[i] = p break elif arr[i] < p: output(i + 1) arr[i] = p break if r - l == 1: break
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER
This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. <image> Initially, Ithea puts n clear sheets of paper in a line. They are numbered from 1 to n from left to right. This game will go on for m rounds. In each round, Ithea will give Chtholly an integer between 1 and c, and Chtholly needs to choose one of the sheets to write down this number (if there is already a number before, she will erase the original one and replace it with the new one). Chtholly wins if, at any time, all the sheets are filled with a number and the n numbers are in non-decreasing order looking from left to right from sheet 1 to sheet n, and if after m rounds she still doesn't win, she loses the game. Chtholly really wants to win the game as she wants to cook something for Willem. But she doesn't know how to win the game. So Chtholly finds you, and your task is to write a program to receive numbers that Ithea gives Chtholly and help her make the decision on which sheet of paper write this number. Input The first line contains 3 integers n, m and c (<image>, <image> means <image> rounded up) — the number of sheets, the number of rounds and the largest possible number Ithea can give to Chtholly respectively. The remaining parts of input are given throughout the interaction process. Interaction In each round, your program needs to read one line containing a single integer pi (1 ≤ pi ≤ c), indicating the number given to Chtholly. Your program should then output a line containing an integer between 1 and n, indicating the number of sheet to write down this number in. After outputting each line, don't forget to flush the output. For example: * fflush(stdout) in C/C++; * System.out.flush() in Java; * sys.stdout.flush() in Python; * flush(output) in Pascal; * See the documentation for other languages. If Chtholly wins at the end of a round, no more input will become available and your program should terminate normally. It can be shown that under the constraints, it's always possible for Chtholly to win the game. Example Input 2 4 4 2 1 3 Output 1 2 2 Note In the example, Chtholly initially knew there were 2 sheets, 4 rounds and each number was between 1 and 4. She then received a 2 and decided to write it in the 1st sheet. Then she received a 1 and wrote it in the 2nd sheet. At last, she received a 3 and replaced 1 with 3 in the 2nd sheet. At this time all the sheets were filled with a number and they were non-decreasing, so she won the game. Note that it is required that your program terminate immediately after Chtholly wins and do not read numbers from the input for the remaining rounds. If not, undefined behaviour may arise and it won't be sure whether your program will be accepted or rejected. Also because of this, please be careful when hacking others' codes. In the sample, Chtholly won the game after the 3rd round, so it is required that your program doesn't read the number of the remaining 4th round. The input format for hacking: * The first line contains 3 integers n, m and c; * The following m lines each contains an integer between 1 and c, indicating the number given to Chtholly in each round.
import sys n, m, c = map(int, input().split()) a = [] for i in range(n + 1): a.append(0) cnt = 0 while cnt < n: i = 0 x = int(input()) if x * 2 <= c: i = 1 while a[i] and a[i] <= x: i += 1 else: i = n while a[i] and a[i] >= x: i -= 1 if a[i] == 0: cnt += 1 a[i] = x print(i) sys.stdout.flush()
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) sml = 0 big = m - 1 curr = (m - 1) // 2 while sml != big: bug = 0 good = True for i in range(n): if a[i] + curr < m + bug: if a[i] + curr < bug: good = False break bug = max(bug, a[i]) if good: big = max(curr, sml) curr = (big + sml) // 2 else: sml = min(curr + 1, big) curr = (sml + big) // 2 print(curr)
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 VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys class Main: def __init__(self): self.buff = None self.index = 0 def next(self): if self.buff is None or self.index == len(self.buff): self.buff = self.next_line() self.index = 0 val = self.buff[self.index] self.index += 1 return val def next_line(self): return sys.stdin.readline().split() def next_ints(self): return [int(x) for x in sys.stdin.readline().split()] def next_int(self): return int(self.next()) def solve(self): n, m = self.next_ints() x = self.next_ints() low = 0 high = m while high - low > 1: mid = (high + low) // 2 if self.test(mid, x, m): high = mid else: low = mid print(low if self.test(low, x, m) else high) def test(self, st, xs, m): xx = 0 for x in xs: fr = x to = x + st if to >= m: to -= m if to >= fr: if xx > to: return False xx = max(xx, fr) elif to < xx < fr: xx = fr return True Main().solve()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER EXPR FUNC_CALL FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) A = list(map(int, input().split())) MIN = 0 MAX = m while MIN != MAX: x = (MIN + MAX) // 2 M = 0 for a in A: if a <= M and a + x >= M: continue elif a > M and a + x >= m and (a + x) % m >= M: continue elif a > M: M = a else: MIN = x + 1 break else: MAX = x print(MIN)
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 NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
from sys import stdin ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) si = lambda: input() msi = lambda: map(int, stdin.readline().split()) lsi = lambda: list(msi()) n, m = li() a = li() l, r = 0, m while l < r: M = (l + r) // 2 ai1 = 0 for ai in a: if (m - ai + ai1) % m > M: if ai < ai1: l = M + 1 break ai1 = ai else: r = M print(l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) l = 0 r = M m = (l + r) // 2 while l < r: ok = True pre = 0 for a in A: p = [] if a <= pre <= a + m: continue elif pre < a: if a + m - M >= pre: continue else: pre = a else: ok = False if ok: if r == m: break r = m m = (l + r) // 2 else: if l == m: break l = m m = (l + r) // 2 print(r) main()
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 VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST IF VAR VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def inc_by_mod(arr, m): def is_opt(count): prev = 0 for elem in arr: if prev < elem: if m - (elem - prev) > count: prev = elem elif prev - elem > count: return False return True count = 0 lo, hi = 0, m - 1 while lo < hi: mid = lo + (hi - lo) // 2 if is_opt(mid): hi = mid else: lo = mid + 1 return lo def main(): n, m = map(int, input().split()) arr = list(map(int, input().split())) res = inc_by_mod(arr, m) print(res) main()
FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
from sys import stdin n, m = map(int, stdin.readline().strip().split()) s = list(map(int, stdin.readline().strip().split())) high = m + 1 low = 0 while high - low > 1: mx = 0 flag = True mid = (low + high) // 2 for i in range(n): if s[i] >= mx: if s[i] + mid >= m and (s[i] + mid) % m >= mx: continue else: mx = s[i] elif s[i] + mid >= mx: continue else: flag = False break if flag: high = mid else: low = mid for j in range(low, low + 2): mx = 0 flag = True mid = (j + j + 1) // 2 for i in range(n): if s[i] >= mx: if s[i] + mid >= m and (s[i] + mid) % m >= mx: continue else: mx = s[i] elif s[i] + mid >= mx: continue else: flag = False break if flag: print(j) break
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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = [int(i) for i in input().split()] array = [int(i) for i in input().split()] def bins(a, b): if a == b: return a mid = (a + b) // 2 if good(mid): return bins(a, mid) else: return bins(mid + 1, b) def good(k): arr = array[:] if arr[0] + k >= m: arr[0] = 0 for i in range(1, n): if arr[i] < arr[i - 1]: if arr[i - 1] - arr[i] > k: return False else: arr[i] = arr[i - 1] elif arr[i] + k - m >= arr[i - 1]: arr[i] = arr[i - 1] return True print(bins(0, m - 1))
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 FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = tuple(map(int, input().split())) l, r = -1, m - 1 while l + 1 < r: M = (l + r) // 2 ai1 = 0 for ai in a: if (m - ai + ai1) % m > M: if ai < ai1: l = M break ai1 = ai else: r = M 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 VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) arr = list(map(int, input().split())) l, r = 0, m - 1 while l < r: mid = (l + r) // 2 p = 0 f = True for x in arr: step = (m - x + p) % m if step > mid: if x < p: f = False break p = x if f is True: r = mid else: l = mid + 1 print(l)
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 VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) def Check(x): y = 0 for i in range(n): if a[i] <= y and a[i] + x >= y: continue elif a[i] + x >= m and a[i] + x - m >= y: continue elif a[i] + x < y: return False else: y = a[i] return True ng = -1 ok = 10**9 mid = 0 while abs(ok - ng) > 1: mid = (ok + ng) // 2 if Check(mid): ok = mid else: ng = mid print(ok)
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 BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def f(x): curr = 0 for i in range(n): if curr >= m: return 0 if x < m - arr[i]: if arr[i] + x < curr: return 0 curr = max(curr, arr[i]) else: start = (x + arr[i]) % m if curr > start: curr = max(curr, arr[i]) return 1 def binary_search(l, r): mid = (l + r) // 2 if l == r: return mid if f(mid): if f(mid - 1) == 0: return mid return binary_search(l, mid - 1) return binary_search(mid + 1, r) l = input().split() n = int(l[0]) m = int(l[1]) l = input().split() arr = [int(i) for i in l] print(binary_search(0, m - 1))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR IF FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) l, r = 0, m ans = 0 while l <= r: mid = l + r >> 1 prev, bad = 0, False for i in a: if (prev - i + m) % m <= mid: continue elif i >= prev: prev = i elif i < prev: bad = True break if bad: l = mid + 1 else: r = mid - 1 ans = mid 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 VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] left = 0 right = m while right - left > 1: x = (right + left) // 2 b = a[:] prev = 0 for i in range(n): if b[i] < prev: if prev - b[i] <= x: b[i] = prev else: left = x break elif m - b[i] + prev <= x: b[i] = prev prev = b[i] else: right = x if sorted(a) == a: print(0) else: print(right)
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 NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def check(times, nums, m): current = 0 if nums[0] + times >= m else nums[0] for i in range(1, len(nums)): if nums[i] == current: continue if nums[i] < current and current - nums[i] > times: return False if nums[i] > current and m - nums[i] + current > times: current = nums[i] return True def main(): n, m = [int(x) for x in input().split()] nums = [int(x) for x in input().split()] if check(0, nums, m): return 0 l = 0 r = m while l + 1 < r: mid = (l + r) // 2 if check(mid, nums, m): r = mid else: l = mid return r print(main())
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF 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 IF FUNC_CALL VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
from sys import stdin def input(): return stdin.readline().strip() def solve(beg, end, a, MOD): if beg + 1 == end: return end k = (beg + end) // 2 conditions = [] prev = 0 flag = True for i in range(n): if k + a[i] < MOD: cond = [(a[i], a[i] + k)] elif k + a[i] >= MOD: cond = [(a[i], MOD - 1), (0, (a[i] + k) % MOD)] if len(cond) == 2: if prev >= cond[0][0] and prev <= cond[0][1]: prev = prev elif prev >= cond[1][0] and prev <= cond[1][1]: prev = prev elif prev <= min(cond[0][0], cond[1][0]): prev = min(cond[0][0], cond[1][0]) elif prev <= max(cond[0][0], cond[1][0]): prev = max(cond[0][0], cond[1][0]) else: flag = False break elif prev <= cond[0][0]: prev = cond[0][0] elif prev >= cond[0][0] and prev <= cond[0][1]: prev = prev else: flag = False break if not flag: return solve(k, end, a, MOD) else: if beg == k: return k return solve(beg, k, a, MOD) n, MOD = map(int, input().split()) a = list(map(int, input().split())) print(solve(-1, MOD, a, MOD))
FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def isenough(m, a, mid): b = 0 k = 0 for i in a: if b < i: if m - i + b > mid: b = i if b > i: if i + mid < b: return False return True n, m = map(int, input().split()) a = list(map(int, input().split())) low = -1 high = m while low + 1 < high: mid = low + (high - low) // 2 if isenough(m, a, mid) == False: low = mid else: high = mid print(high)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR IF BIN_OP 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 NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, k = map(int, input().split()) A = list(map(int, input().split())) l = 0 r = k + 1 ans = k + 1 for _ in range(20): B = [0] * n m = (l + r) // 2 min1 = 0 fl = 0 for i in range(n): if A[i] > min1: if k - A[i] + min1 <= m: B[i] = min1 else: min1 = A[i] B[i] = A[i] elif min1 - A[i] > m: fl = 1 break else: B[i] = min1 if fl == 0: for i in range(n - 1): if B[i] > B[i + 1]: break fl = 1 if fl == 0: r = m ans = min(ans, m) else: l = m else: l = m 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 VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = list(map(int, input().split())) a = [int(i) for i in input().split()] L = -1 R = m while L < R - 1: mid = (L + R) // 2 last = 0 isok = True for el in a: if last == el: continue if last > el: if el + mid >= last: continue else: isok = False break elif el + mid - m >= last: continue else: last = el if isok: R = mid else: L = mid print(R)
ASSIGN VAR VAR FUNC_CALL 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 VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys def check(arr, n, m): k = len(arr) low, high = [], [] for i in range(k): num = arr[i] if num + n < m: low.append([num, num + n]) high.append([num, num + n]) else: low.append([0, (num + n) % m]) high.append([num, m - 1]) lim = low[0][0] k = len(low) for i in range(1, k): if low[i][0] <= lim <= low[i][1]: continue if lim < low[i][0]: lim = low[i][0] continue if high[i][0] <= lim <= high[i][1]: continue if lim < high[i][0]: lim = high[i][0] continue return False return True n, m = map(int, sys.stdin.readline().split()) arr = list(map(int, sys.stdin.readline().split())) low, high = 0, m ans = 1000000 while low <= high: mid = (low + high) // 2 if check(arr, mid, m): ans = min(ans, mid) high = mid - 1 else: low = mid + 1 print(ans)
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST NUMBER BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER 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 NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR 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
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def modulo(a, maxi, n, m): prev = 0 for i in range(n): if a[i] > prev: t = m - (a[i] - prev) if t > maxi: prev = a[i] else: t = prev - a[i] if t > maxi: return False return True n, m = map(int, input().split()) arr = list(map(int, input().split())) l = 0 r = m while l <= r: mid = (l + r) // 2 if modulo(arr, mid, n, m): ans = mid r = mid - 1 else: l = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR 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 NUMBER ASSIGN 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 ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def checker(ij): count = 0 for i in range(len(l)): if (count - l[i]) % m >= ij: if l[i] > count: count = l[i] else: return False return True n, m = input().split() n, m = [int(n), int(m)] l = [int(i) for i in input().split()] beg = -1 last = m while beg < last - 1: mid = (beg + last) // 2 counter = checker(mid) if counter: last = mid else: beg = mid print(beg)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def chek(mid): v = 0 for i in range(n): if (v - a[i]) % m > mid: if a[i] > v: v = a[i] else: return False return True n, m = list(map(int, input().split())) l = -1 r = m a = list(map(int, input().split())) chek(3) while l < r - 1: mid = (l + r) // 2 if chek(mid): r = mid else: l = mid print(r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) lo = 0 hi = m mid = (lo + hi) // 2 while lo < hi: minupto = 0 anw = 0 for i in range(n): if a[i] < minupto: anw = max(minupto - a[i], anw) elif a[i] + mid >= m and (a[i] + mid) % m >= minupto: pass else: minupto = a[i] if anw > mid: lo = mid + 1 else: hi = mid mid = (lo + hi) // 2 print(mid)
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 VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = list(map(int, input().split())) ass = list(map(int, input().split())) def done(a): return all(x < y for x, y in zip(a[:-1], a[1:])) maxops = m - 1 minops = 0 while maxops > minops: cops = (maxops + minops) // 2 base = 0 ok = True for a in ass: if base > a and base - a > cops: ok = False break elif (base - a) % m > cops: base = a if ok: maxops = cops else: minops = cops + 1 print(maxops)
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 RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = input().split() n, m = int(n), int(m) a = list(map(int, input().split())) l, r = 0, m - 1 def isOptimal(mid): prev = 0 check = True for i in range(n): if prev < a[i]: if m - a[i] + prev > mid: prev = a[i] elif prev - a[i] > mid: check = False return check while l <= r: check = True mid = (l + r) // 2 prev = a[0] if isOptimal(mid): r = mid - 1 else: l = mid + 1 check = isOptimal(l) print(l * check + r * (not check))
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
N, M = map(int, input().split()) A = list(map(int, input().split())) def check(k): pre = 0 for a in A: if a == pre: continue elif a < pre: if a + k < pre: return False else: if a + k >= M and (a + k) % M >= pre: continue pre = a return True ok = M ng = -1 while abs(ok - ng) > 1: med = (ok + ng) // 2 if check(med): ok = med else: ng = med print(ok)
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 VAR IF VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) l = list(map(int, input().split())) def enough(mid): prev = l[0] if m - l[0] <= mid: prev = 0 for i in range(1, len(l)): if prev > l[i]: if prev - l[i] > mid: return False elif m - l[i] + prev > mid: prev = l[i] return True def bs(): l, r = 0, m - 1 while l < r: mid = (l + r) // 2 if enough(mid): r = mid else: l = mid + 1 return r print(bs())
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 VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP 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 RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = list(map(int, input().split())) ns = list(map(int, input().split())) def check(x): if ns[0] + x >= m: minn = 0 else: minn = ns[0] for i in range(1, n): c = ns[i] if minn >= c: if c + x >= minn: continue else: return False elif c + x < m: minn = c continue elif (c + x) % m >= minn: continue else: minn = c return True p = True for i in range(n - 1): if ns[i + 1] < ns[i]: p = False break if p: print(0) quit() l, r = 0, m - 1 while r - l > 1: mm = (r + l) // 2 if check(mm): r = mm else: l = mm print(r)
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 IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split(" ")) A = list(map(int, input().split(" "))) def can(h): B = [] last = 0 for i in range(0, n): b = A[i] if b <= last: if last - b <= h: continue else: return False else: if m - b + last <= h: continue last = b B.append(b) return True left = 0 right = m while left < right: middle = (left + right) // 2 if can(middle): right = middle else: left = middle + 1 print(left)
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 FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN 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
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) def check(a, x, m): Max = m flg = True for val in a[::-1]: if val <= Max: Max = min(Max, val + x) elif val + x < m: flg = False break else: Max = min(Max, (val + x) % m) return flg l = -1 u = m while u - l > 1: md = (u + l) // 2 if check(a, md, m) == True: u = md else: l = md print(u)
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 VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def ok(a, arr): prev = 0 for i in range(n): if prev <= arr[i]: s = m + prev - arr[i] if s > a: prev = arr[i] else: s = prev - arr[i] if s > a: return False return True n, m = map(int, input().split(" ")) arr = [int(i) for i in input().split(" ")] l = -1 r = m while r - l > 1: mid = int((r + l) / 2) if ok(mid, arr): r = mid else: l = mid print(r)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def check(mid): local_a = [0] for ai in a: local_a.append(ai) for i in range(1, n + 1): if local_a[i] < local_a[i - 1]: if local_a[i] + mid >= local_a[i - 1]: local_a[i] = local_a[i - 1] elif local_a[i] + mid >= m + local_a[i - 1]: local_a[i] = local_a[i - 1] if local_a[i] < local_a[i - 1]: return False return True n, m = map(int, input().split()) a = list(map(int, input().split())) low, high = 0, m while low != high: mid = low + high >> 1 if check(mid): high = mid else: low = mid + 1 print(low)
FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER 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 NUMBER 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
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys def input(): return sys.stdin.readline().rstrip() def slv(): n, m = map(int, input().split()) a = list(map(int, input().split())) def minimum(K, v, lower_bound): if v >= lower_bound: if m + lower_bound - v <= K: return lower_bound else: return v elif lower_bound - v >= K: return v + K else: return lower_bound def possible(K): if K < 0: return False V = 0 for v in a: nextV = minimum(K, v, V) if V <= nextV: V = nextV else: return False return True l = -1 r = m * m + 10 while r - l > 1: med = (r + l) // 2 if possible(med): r = med else: l = med print(r) return def main(): t = 1 for i in range(t): slv() return main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL 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 FUNC_DEF IF VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR RETURN VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def check(mid): p = 0 for i in arr: if i < p and p - i > mid: return False if i > p and mid < p - i + m: p = i return True n, m = map(int, input().split()) arr = list(map(int, input().split())) i, j = 0, m ans = m while i <= j: mid = i + (j - i) // 2 if check(mid): ans = min(ans, mid) j = mid - 1 else: i = mid + 1 print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR 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 NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL 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
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = [int(i) for i in input().split()] array = [int(i) for i in input().split()] a = 0 b = m - 1 while a < b: mid = (a + b) // 2 ret = True q = mid - m last = array[0] if last + q >= 0: last = 0 for i in range(1, n): d = last - array[i] if d > mid: ret = False break if d <= 0 and d > q: last = array[i] if ret: b = mid else: a = mid + 1 print(a)
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 NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys N, M = map(int, input().split(" ")) A = list(map(int, sys.stdin.readline().split(" "))) def check(): last = 0 for i in range(N): if A[i] < last: if last - A[i] > m: break elif A[i] > last: if A[i] + m < M or (A[i] + m) % M < last: last = A[i] else: return True return False l = -1 r = M - 1 while l + 1 < r: m = (l + r) // 2 if check(): r = m else: l = m print(r)
IMPORT 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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
def check(n, m, s, v): mi = 0 for i in range(n): if v[i] + s < mi: return False if v[i] > mi: if (v[i] + s) % m >= mi: if v[i] <= (v[i] + s) % m: mi = v[i] else: mi = v[i] return True def __starting_point(): n, m = list(map(int, input().split())) v = list(map(int, input().split())) l = 0 r = m while l < r: s = (l + r) // 2 if check(n, m, s, v): r = s else: l = s + 1 print(l) __starting_point()
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER 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 NUMBER ASSIGN 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) def isok(b, k): if b[0] + k >= m: b[0] = 0 for i in range(len(a) - 1): if b[i + 1] > b[i]: if b[i + 1] + k >= m: if (b[i + 1] + k) % m >= b[i]: b[i + 1] = b[i] elif (b[i + 1] + k) % m >= b[i]: b[i + 1] = b[i] elif b[i + 1] + k >= b[i]: b[i + 1] = b[i] else: return False return True left = -1 right = m while right - left > 1: mid = left + (right - left) // 2 if isok(a[:], mid): right = mid else: left = mid print(right)
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 IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
n, m = map(int, input().split()) a = list(map(int, input().split())) l, r, ans = 0, m, m while l <= r: mid = (l + r) // 2 cur = 0 if a[0] + mid >= m else a[0] i = 1 while i < n: if a[i] + mid < cur: break if not (a[i] < cur or a[i] + mid >= m and (a[i] + mid) % m >= cur): cur = a[i] i += 1 if i == n: ans = min(ans, mid) if l == r: break r = mid else: if l == r: break 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 VAR NUMBER VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Zitz has an array of integers, each integer is between $0$ and $m-1$ inclusive. The integers are $a_1, a_2, \ldots, a_n$. In one operation Zitz can choose an integer $k$ and $k$ indices $i_1, i_2, \ldots, i_k$ such that $1 \leq i_1 < i_2 < \ldots < i_k \leq n$. He should then change $a_{i_j}$ to $((a_{i_j}+1) \bmod m)$ for each chosen integer $i_j$. The integer $m$ is fixed for all operations and indices. Here $x \bmod y$ denotes the remainder of the division of $x$ by $y$. Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 300\,000$) — the number of integers in the array and the parameter $m$. The next line contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < m$) — the given array. -----Output----- Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print $0$. It is easy to see that with enough operations Zitz can always make his array non-decreasing. -----Examples----- Input 5 3 0 0 0 1 2 Output 0 Input 5 7 0 6 1 3 2 Output 1 -----Note----- In the first example, the array is already non-decreasing, so the answer is $0$. In the second example, you can choose $k=2$, $i_1 = 2$, $i_2 = 5$, the array becomes $[0,0,1,3,3]$. It is non-decreasing, so the answer is $1$.
N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] def chk(k): ret = 0 for i in range(N): a, b = A[i], (A[i] + k) % M if a <= b < ret: return 0 if ret <= a <= b or b < ret <= a: ret = a return 1 l = -1 r = M while r - l > 1: m = (l + r) // 2 if chk(m): r = m else: l = m print(r)
ASSIGN VAR VAR FUNC_CALL 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 FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others. Input The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k. The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order. Output First line contains integer m — the smallest number of segments. Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right. Examples Input 3 2 0 5 -3 2 3 8 Output 2 0 2 3 5 Input 3 2 0 5 -3 3 3 8 Output 1 0 5
import sys n, k = map(int, sys.stdin.buffer.readline().decode("utf-8").split()) imos = dict() for li, ri in (map(int, line.decode("utf-8").split()) for line in sys.stdin.buffer): imos[li * 2] = imos[li * 2] + 1 if li * 2 in imos else 1 imos[ri * 2 + 1] = imos[ri * 2 + 1] - 1 if ri * 2 + 1 in imos else -1 acc = 0 ans = [] append = ans.append minf = -(10**9 * 2 + 1) left = minf for x in sorted(imos.keys()): acc += imos[x] if left != minf and acc < k: append(str(left >> 1) + " " + str(x >> 1)) left = minf elif left == minf and acc >= k: left = x sys.stdout.buffer.write((str(len(ans)) + "\n" + "\n".join(ans)).encode("utf-8"))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL STRING VAR STRING
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
nk = input().split(" ") n, k = int(nk[0]), int(nk[1]) a = list(map(int, input().split(" "))) a.sort() start = (n - 1) // 2 prev = 1 max_med = a[start] for i in range(start + 1, n): required = (a[i] - a[i - 1]) * prev if k - required >= 0: k -= required prev += 1 max_med = a[i] else: possible = k // prev max_med = a[i - 1] + possible k = 0 break if k != 0: max_med += k // prev print(max_med)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() medianIndex = n // 2 a = a[medianIndex:] left = 1 right = 10**10 while left < right: mid = (left + right + 1) // 2 count = 0 for i in range(len(a)): count += max(0, mid - a[i]) if count > k: right = mid - 1 else: left = mid print(left)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) lst = sorted(list(map(int, input().split()))) middle = lst[n // 2] if len(lst) == 1: print(middle + k) exit() r = n // 2 + 1 while k > 0: while r < n and lst[r] == middle: r += 1 if r == n: middle += k // (r - n // 2) k = 0 else: put = lst[r] - middle if put * (r - n // 2) >= k: middle += k // (r - n // 2) k = 0 else: middle += put k -= put * (r - n // 2) print(middle)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
def main(): n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) i = n // 2 count = k ans = a[n // 2] while i < n - 1 and count - (a[i + 1] - a[i]) * (i - n // 2 + 1) >= 0: count -= (a[i + 1] - a[i]) * (i - n // 2 + 1) ans = a[i + 1] i += 1 print(ans + count // (i - n // 2 + 1)) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() idex = n // 2 last = -1 avilable = -1 for i in range(idex, n - 1): need = a[i + 1] - a[i] if need: real = (i + 1 - idex) * need if k >= real: a[i] += need k -= real else: avilable = k // (i + 1 - idex) last = i break if last == -1: avilable = k // (n - idex) print(a[n - 1] + avilable) else: print(a[last] + avilable)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) a = [int(x) for x in input().split()] a.sort() mid = int((n - 1) / 2) jsj = 1 for i in range(mid + 1, n): if a[i] == a[i - 1]: jsj += 1 else: if k <= jsj * (a[i] - a[i - 1]): ans = a[i - 1] + int(k / jsj) k -= jsj * (a[i] - a[i - 1]) break k -= jsj * (a[i] - a[i - 1]) jsj += 1 if k > 0: ans = a[n - 1] + int(k / jsj) print(ans)
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 ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
_, k = map(int, input().split()) a = list(map(int, input().split())) a.sort() n = len(a) // 2 med = a[n] klast = 1 while k > 0 and n + 1 < len(a): k1 = (a[n + 1] - a[n]) * klast if k >= k1: k -= k1 med = a[n + 1] else: break n += 1 klast += 1 if k > 0: med += k // klast print(med)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
fst = list(map(int, input().split())) extra = fst[1] numbers = sorted(list(map(int, input().split()))) i = len(numbers) // 2 + 1 possibleans = numbers[len(numbers) // 2] + extra while i < len(numbers): if possibleans <= numbers[i]: break extra += numbers[i] - numbers[len(numbers) // 2] possibleans = numbers[len(numbers) // 2] + extra // (i + 1 - len(numbers) // 2) i += 1 print(min(possibleans, numbers[len(numbers) // 2] + fst[1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
from sys import stdin input = stdin.readline n, k = map(int, input().split()) l = list(map(int, input().split())) l.sort() ans = l[n // 2] x = l[n // 2] md = x t = 0 for i in range(n // 2, n): t += 1 if l[i] != md or i == n - 1: if k >= (t - 1) * (l[i] - md): ans += l[i] - md k -= (t - 1) * (l[i] - md) md = l[i] else: ans += k // (t - 1) break if l[i] == md and i == n - 1: ans += k // t print(ans)
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 EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
x, y = map(int, input().split()) s = sorted(list(map(int, input().split()))) def ck(x, y, s): res = 0 ln = len(s) for n in range(ln // 2, ln): res += max(0, x - s[n]) return res <= y res = 0 hi = 10**10 lo = 0 while hi >= lo: mid = (hi + lo) // 2 if ck(mid, y, s): res = mid lo = mid + 1 else: hi = mid - 1 print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, k = map(int, input().split()) lst = list(map(int, input().split())) lst.sort() min_index = (n + 1) // 2 - 1 Min = lst[min_index] Max = Min + k flag = 0 while Min <= Max: mid = (Min + Max) // 2 total = 0 for i in range(min_index, n): temp = mid - lst[i] if temp > 0: total += temp if total == k: print(mid) flag = 1 break if total < k: Min = mid + 1 else: Max = mid - 1 if flag == 0: print(Max)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of $n$ integers, where $n$ is odd. You can make the following operation with it: Choose one of the elements of the array (for example $a_i$) and increase it by $1$ (that is, replace it with $a_i + 1$). You want to make the median of the array the largest possible using at most $k$ operations. The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $[1, 5, 2, 3, 5]$ is $3$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$, $n$ is odd, $1 \le k \le 10^9$) — the number of elements in the array and the largest number of operations you can make. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$). -----Output----- Print a single integer — the maximum possible median after the operations. -----Examples----- Input 3 2 1 3 5 Output 5 Input 5 5 1 2 1 1 1 Output 3 Input 7 7 4 1 2 4 3 4 4 Output 5 -----Note----- In the first example, you can increase the second element twice. Than array will be $[1, 5, 5]$ and it's median is $5$. In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $3$. In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $[5, 1, 2, 5, 3, 5, 5]$ and the median will be $5$.
n, m = [int(x) for x in input().split()] a = sorted([int(x) for x in input().split()])[n // 2 :] l = 0 r = 10**10 while r - l > 1: mid = (l + r) // 2 arr = 0 for item in a: arr += max(0, mid - item) if arr > m: r = mid else: l = mid print(l)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR