description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
A string is called happy if it does not have any of the strings 'aaa', 'bbb' or 'ccc' as a substring. Given three integers a, b and c, return any string s, which satisfies following conditions: s is happy and longest possible. s contains at most a occurrences of the letter 'a', at most b occurrences of the letter 'b' and at most c occurrences of the letter 'c'. s will only contain 'a', 'b' and 'c' letters. If there is no such string s return the empty string "".   Example 1: Input: a = 1, b = 1, c = 7 Output: "ccaccbcc" Explanation: "ccbccacc" would also be a correct answer. Example 2: Input: a = 2, b = 2, c = 1 Output: "aabbc" Example 3: Input: a = 7, b = 1, c = 0 Output: "aabaa" Explanation: It's the only correct answer in this case.   Constraints: 0 <= a, b, c <= 100 a + b + c > 0
class Solution: def longestDiverseString( self, a: int, b: int, c: int, charA="a", charB="b", charC="c" ) -> str: if a < b: return self.longestDiverseString(b, a, c, charB, charA, charC) if b < c: return self.longestDiverseString(a, c, b, charA, charC, charB) if b == 0: return min(a, 2) * charA use_a = min(2, a) use_b = 1 if a - use_a >= b else 0 return ( charA * use_a + charB * use_b + self.longestDiverseString(a - use_a, b - use_b, c, charA, charB, charC) )
CLASS_DEF FUNC_DEF VAR VAR VAR STRING STRING STRING IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR
A string is called happy if it does not have any of the strings 'aaa', 'bbb' or 'ccc' as a substring. Given three integers a, b and c, return any string s, which satisfies following conditions: s is happy and longest possible. s contains at most a occurrences of the letter 'a', at most b occurrences of the letter 'b' and at most c occurrences of the letter 'c'. s will only contain 'a', 'b' and 'c' letters. If there is no such string s return the empty string "".   Example 1: Input: a = 1, b = 1, c = 7 Output: "ccaccbcc" Explanation: "ccbccacc" would also be a correct answer. Example 2: Input: a = 2, b = 2, c = 1 Output: "aabbc" Example 3: Input: a = 7, b = 1, c = 0 Output: "aabaa" Explanation: It's the only correct answer in this case.   Constraints: 0 <= a, b, c <= 100 a + b + c > 0
class Solution: def longestDiverseString(self, a: int, b: int, c: int) -> str: d = { "a": min(a, 2 * (b + c + 1)), "b": min(b, 2 * (a + c + 1)), "c": min(c, 2 * (b + a + 1)), } n = sum(d.values()) res = [] for _ in range(n): cand = set(["a", "b", "c"]) if len(res) > 1 and res[-1] == res[-2]: cand.remove(res[-1]) tmp = max(cand, key=lambda x: d[x]) res.append(tmp) d[tmp] -= 1 return "".join(res)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT STRING STRING STRING FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST STRING STRING STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR VAR
You are given a string S, each of whose characters is either '0', '1', or '?'. The *badness* of a binary string is defined to be the (absolute) difference between the number of 1s and the number of 0s present in it. For example, 001 has badness |1-2| = 1 and 11111 has badness |5-0| = 5. Your task is to replace every '?' present in S with either '0' or '1', such that the badness of the resulting binary string is minimized. If there are multiple solutions, you may print any of them. ------ Input Format ------ - The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows. - Each test case contains two lines. - The first line of each test case contains an integer N, the number of characters of the string S. - The second line of each test case contains the string S, whose characters are either '0', '1', or '?'. ------ Output Format ------ For each test case, print a single line containing the binary string you obtained by replacing each '?' with either '0' or '1' such that its badness is minimum. If there are multiple possible strings that minimize badness, you may print any of them. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$. ----- Sample Input 1 ------ 4 4 ?101 4 ??10 6 ???111 5 1?0?1 ----- Sample Output 1 ------ 0101 1010 000111 11001 ----- explanation 1 ------ Test Case $1$. There are only two possible strings that can be constructed - $1101$ and $0101$. $1101$ has badness $2$, and $0101$ has badness $0$. Thus, the only possible answer is $0101$. Test Case $2$. There are four possible strings that can be made, namely $\{0010, 0110, 1010, 1110\}$. Their respective badness values are $\{2, 0, 0, 2\}$. The minimum value here is $0$, attained by both $0110$ and $1010$ - and so either of these strings is a valid answer. Test Case $3$. There are eight possible strings, of which $000111$ is the only one with minimum badness (that being $0$). Test Case $4$. There are four possible strings, out of which three of them ($10001$, $10011$, $11001$) have badness $1$, which is minimum. All three of them are possible answers.
def integer_list(): return list(map(int, input().split())) def string_list(): return list(map(str, input().split())) def hetro_list(): return list(input().split()) def main(): ones = s.count("1") zeroes = s.count("0") rand = s.count("?") diff = abs(ones - zeroes) added_ones = 0 added_zeroes = 0 if ones < zeroes: added_ones = diff rand -= diff else: added_zeroes = diff rand -= diff if rand > 0: added_zeroes += rand // 2 added_ones += rand - rand // 2 ans = "" for ele in s: if ele == "?": if added_ones > 0: ans = ans + "1" added_ones -= 1 else: ans = ans + "0" else: ans = ans + ele print(ans) t = int(input()) for _ in range(t): n = int(input()) s = input() main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR BIN_OP VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 1, 0, -1): if A[i - 1] > A[i]: break if i == 1 and A[i - 1] <= A[i]: return A j = max([x for x in range(i, len(A)) if A[x] < A[i - 1]], key=lambda x: A[x]) A[i - 1], A[j] = A[j], A[i - 1] return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: mid = -1 for i in range(len(A) - 2, -1, -1): if A[i] > A[i + 1]: mid = i break if mid == -1: return A max_mid = -1 for i in range(mid + 1, len(A)): if A[mid] > A[i]: if A[i] > max_mid: index = i max_mid = A[i] A[mid], A[index] = A[index], A[mid] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if A == []: return [] last = float("inf") flag = False for i in range(len(A) - 1, -1, -1): if A[i] > last: flag = True break last = A[i] if not flag: return A else: maxx = -float("inf") champ = None for j in range(i + 1, len(A)): if A[j] > maxx and A[j] < A[i]: champ = j maxx = A[j] A[i], A[champ] = A[champ], A[i] return A
CLASS_DEF FUNC_DEF VAR VAR IF VAR LIST RETURN LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: idx = -1 for i in range(len(A) - 1): if A[i] > A[i + 1]: idx = i if idx == -1: return A for i in range(len(A) - 1, idx, -1): if A[i] < A[idx] and A[i] != A[i - 1]: A[idx], A[i] = A[i], A[idx] break return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: i = len(A) - 2 while i >= 0 and A[i] <= A[i + 1]: i -= 1 if i >= 0: max_ = i + 1 for j in range(max_ + 1, len(A)): if A[max_] < A[j] and A[j] < A[i]: max_ = j A[max_], A[i] = A[i], A[max_] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: def findLarger(): prev = A[-1] for i in range(len(A) - 1, -1, -1): if A[i] > prev: return i prev = A[i] return -1 if not A: return A larger = findLarger() if larger == -1: return A sl = larger + 1 for i in range(larger, len(A)): if A[i] < A[larger]: if A[sl] < A[i]: sl = i print((larger, sl)) A[sl], A[larger] = A[larger], A[sl] return A
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR RETURN NUMBER IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: prev_vals = [(float("inf"), None)] for i in range(len(A) - 1, -1, -1): num = A[i] if num > prev_vals[-1][0]: max_prev = 0 ind = None while num > prev_vals[-1][0]: prev_n, prev_ind = prev_vals.pop() if prev_n > max_prev: max_prev = prev_n ind = prev_ind A[i], A[ind] = max_prev, num return A prev_vals.append((num, i)) return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FUNC_CALL VAR STRING NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: n = len(A) left = n - 1 while left >= 1 and A[left - 1] <= A[left]: left -= 1 if left == 0: return A right = left left = left - 1 for i in range(right, n): if A[i] > A[right] and A[i] < A[left]: right = i A[left], A[right] = A[right], A[left] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: max_index = -1 for i in range(1, len(A)): if A[i - 1] > A[i]: max_index = max(max_index, i - 1) if max_index != -1: for i in range(max_index + 1, len(A)): if A[i] < A[max_index]: j = i A[j], A[max_index] = A[max_index], A[j] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: largest = len(A) - 2 while largest >= 0 and A[largest] <= A[largest + 1]: largest -= 1 if largest == -1: return A smallest = len(A) - 1 while A[largest] <= A[smallest]: smallest -= 1 f = smallest while A[smallest] == A[f]: f -= 1 A[f + 1], A[largest] = A[largest], A[f + 1] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: res = [val for val in A] length = len(A) start = 0 haveInvertedPos = False for i in range(length - 1): if A[i] <= A[i + 1]: pass else: start = i haveInvertedPos = True if not haveInvertedPos: return res target = start + 1 temp = A[target] for i in range(start + 1, length): if A[i] < A[start] and A[i] > temp: temp = A[i] target = i res[start], res[target] = res[target], res[start] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if len(A) == 1: return A for i in range(len(A) - 1, 0, -1): if A[i - 1] > A[i]: m, mi = A[i], i for j in range(i, len(A)): if A[j] < A[i - 1] and A[j] > m: mi = j m = A[j] A[i - 1], A[mi] = A[mi], A[i - 1] break return A
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: stack = [] for i in range(len(A) - 1, -1, -1): if stack != [] and stack[-1] < A[i]: break stack.append(A[i]) if len(stack) == len(A): return A stack = stack[::-1] low, high = 0, len(stack) - 1 while low < high - 1: mid = (low + high) // 2 if stack[mid] >= A[i]: high = mid - 1 else: low = mid if stack[high] >= A[i]: high -= 1 l = high - 1 while l >= 0 and stack[l] == stack[high]: l -= 1 l += 1 temp = A[i] A[i] = A[i + l + 1] A[i + l + 1] = temp return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: n = len(A) i, mn, mx = n - 2, n - 1, n - 1 while i >= 0: if A[i] > A[mn]: mx = i if A[i] < A[mn]: mn = i if mn != mx and mx < mn: break i -= 1 if i == -1: return A print((mx, mn)) mxR = mx + 1 for j in range(mx + 1, n): if A[j] < A[mx] and A[j] > A[mxR]: mxR = j A[mx], A[mxR] = A[mxR], A[mx] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: m = 10000 idx = defaultdict(int) l = [] for i in range(len(A) - 1, -1, -1): if A[i] > m: l.sort() j = idx[l[bisect_left(l, A[i]) - 1]] A[i], A[j] = A[j], A[i] return A m = min(m, A[i]) idx[A[i]] = i l.append(A[i]) return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: n = len(A) if n == 1: return A tidx = -1 for i in range(n - 2, -1, -1): if A[i] > A[i + 1]: tidx = i break if tidx < 0: return A sidx = -1 for j in range(n - 1, tidx, -1): if A[j] == A[j - 1]: continue if A[j] < A[tidx]: sidx = j break A[tidx], A[sidx] = A[sidx], A[tidx] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: n = len(A) for i in range(n - 2, -1, -1): if A[i] > A[i + 1]: pm = None for j in range(n - 1, i, -1): if A[j] >= A[i]: continue if pm is None: pm = j elif A[j] == A[pm]: pm = j else: break A[i], A[pm] = A[pm], A[i] return A return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in reversed(range(1, len(A))): if A[i - 1] > A[i]: maxLessInd = i maxLessVal = A[i] for j in range(i + 1, len(A)): if A[j] > maxLessVal and A[j] < A[i - 1]: maxLessVal = A[j] maxLessInd = j A[i - 1], A[maxLessInd] = A[maxLessInd], A[i - 1] return A return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if not A: return A leftIndex = -1 for i in range(len(A) - 1, 0, -1): if A[i - 1] > A[i]: leftIndex = i - 1 break if leftIndex == -1: return A rightIndex = -1 for i in range(len(A) - 1, leftIndex, -1): if A[i] >= A[leftIndex]: continue elif rightIndex == -1: rightIndex = i elif A[i] == A[rightIndex]: rightIndex = i A[leftIndex], A[rightIndex] = A[rightIndex], A[leftIndex] return A
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: smallestSeenSoFar = A[-1] swapOutIndex = -1 for i in range(len(A) - 2, -1, -1): if A[i] > smallestSeenSoFar: swapOutIndex = i break smallestSeenSoFar = min(smallestSeenSoFar, A[i]) if swapOutIndex == -1: return A swapInIndex = len(A) - 1 largestSeenSmallerThanSwapout = 0 for i in range(len(A) - 1, swapOutIndex, -1): if A[i] < A[swapOutIndex] and A[i] >= largestSeenSmallerThanSwapout: swapInIndex = i largestSeenSmallerThanSwapout = A[i] A[swapOutIndex], A[swapInIndex] = A[swapInIndex], A[swapOutIndex] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 2, -1, -1): for x in range(len(A) - 1, i, -1): if A[x] == A[x - 1]: continue if A[i] > A[x]: A[i], A[x] = A[x], A[i] return A return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: i = len(A) - 2 while i >= 0 and A[i + 1] >= A[i]: i -= 1 if i == -1: return A k = i + 1 j = i + 1 while j < len(A): if A[k] < A[j] < A[i]: k = j j += 1 A[i], A[k] = A[k], A[i] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: already_sorted = True swap_left = None for i in range(len(A) - 1, 0, -1): if A[i] < A[i - 1]: swap_left = i - 1, A[i - 1] already_sorted = False break if already_sorted: return A swap_right = None for i in range(swap_left[0] + 1, len(A)): if A[i] < swap_left[1]: if swap_right and A[i] <= swap_right[1]: continue else: swap_right = i, A[i] A[swap_right[0]], A[swap_left[0]] = swap_left[1], swap_right[1] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: l = len(A) - 2 while l >= 0: if A[l + 1] < A[l]: break l -= 1 max_so_far = 0 max_idx = l r = l + 1 while r < len(A): if A[r] > max_so_far and A[r] < A[l]: max_so_far = A[r] max_idx = r r += 1 if 0 <= l <= max_idx < len(A): A[l], A[max_idx] = A[max_idx], A[l] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 2, -1, -1): if A[i] > A[i + 1]: for j in range(i + 1, len(A)): if A[j] > A[i] or j == len(A) - 1: k = j - 1 if A[j] > A[i] else j while k > i and A[k] == A[i]: k -= 1 while k - 1 > i and A[k - 1] == A[k]: k -= 1 A[i], A[k] = A[k], A[i] break break return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 2, -1, -1): if A[i] > A[i + 1]: break if A[i] <= A[i + 1]: return A max_j = 0 j_ind = i for j in range(i, len(A)): if A[i] > A[j]: max_j = max(max_j, A[j]) if max_j == A[j]: j_ind = j tmp = A[i] A[i] = A[j_ind] A[j_ind] = tmp return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: a_length = len(A) largest_left_index = 0 largest_left = A[0] for i in range(2, a_length + 1): i *= -1 if A[i] > A[i + 1]: largest_left_index = i largest_left = A[i] break largest_right_index = 0 largest_right = 0 for i in range(a_length + largest_left_index + 1, a_length): if A[i] > largest_right and A[i] < largest_left: largest_right_index = i largest_right = A[i] A[largest_left_index], A[largest_right_index] = ( A[largest_right_index], A[largest_left_index], ) return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if A == sorted(A): return A idx = None for i in range(len(A) - 1, 0, -1): if A[i - 1] > A[i]: idx = i - 1 break mx = float("-inf") for i in range(idx + 1, len(A)): if mx < A[i] < A[idx]: mx = A[i] midx = i A[midx], A[idx] = A[idx], A[midx] return A
CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if sorted(A) == A: return A n = len(A) s = A[:] for i in range(n - 2, -1, -1): s[i] = min(s[i], s[i + 1]) if A[i] > s[i + 1]: j = n - 1 while A[j] >= A[i]: j -= 1 A[i], A[j] = A[j], A[i] return A
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 2, -1, -1): if A[i] > A[i + 1]: break else: return A stack = [] for j in range(i + 1, len(A)): if A[j] < A[i]: while stack and A[stack[-1]] < A[j]: stack.pop() stack.append(j) A[i], A[stack[0]] = A[stack[0]], A[i] return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: for i in range(len(A) - 2, -1, -1): ans = -1, -1 for j in range(i + 1, len(A)): if A[j] < A[i]: ans = max(ans, (A[j], -j)) if ans[0] != -1: A[i], A[-ans[1]] = A[-ans[1]], A[i] return A return A
CLASS_DEF FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: stack = [0] larger_index = [-1] * len(A) for i in range(1, len(A)): while stack and A[i] >= A[stack[-1]]: stack.pop() if stack: larger_index[i] = stack[-1] stack.append(i) print(larger_index) idx, swap = len(larger_index), -1 for i in range(len(larger_index) - 1, -1, -1): if larger_index[i] == -1: continue if larger_index[i] > swap or larger_index[i] == swap and A[i] >= A[idx]: val = A[larger_index[i]] idx = i swap = larger_index[i] if swap != -1: A[swap], A[idx] = A[idx], A[swap] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: n = len(A) if n <= 1: return A left = n - 2 while left >= 0 and A[left] <= A[left + 1]: left -= 1 if left < 0: return A right = n - 1 while A[right] >= A[left]: right -= 1 while A[right] == A[right - 1]: right -= 1 A[left], A[right] = A[right], A[left] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: if len(A) == 1: return A prev = A[0] swapping = 0, -1 to_swap = 0, -1 for ind in range(1, len(A)): num = A[ind] if num < prev: swapping = num, ind to_swap = A[ind - 1], ind - 1 elif num > swapping[0] and num < to_swap[0]: swapping = num, ind prev = num if swapping[1] != -1: value = A[swapping[1]] A[swapping[1]] = A[to_swap[1]] A[to_swap[1]] = value return A
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: l = -1 for i in reversed(range(len(A) - 1)): if A[i] > A[i + 1]: l = i break if l >= 0: r = l + 1 s = A[r] for i in range(l + 2, len(A)): if A[i] < A[l] and A[i] >= s: r = i A[l], A[r] = A[r], A[l] return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.   Example 1: Input: [3,2,1] Output: [3,1,2] Explanation: Swapping 2 and 1. Example 2: Input: [1,1,5] Output: [1,1,5] Explanation: This is already the smallest permutation. Example 3: Input: [1,9,4,6,7] Output: [1,7,4,6,9] Explanation: Swapping 9 and 7. Example 4: Input: [3,1,1,3] Output: [1,3,1,3] Explanation: Swapping 1 and 3.   Note: 1 <= A.length <= 10000 1 <= A[i] <= 10000
class Solution: def prevPermOpt1(self, A: List[int]) -> List[int]: stack = [] find = -1 for i in range(1, len(A)): if A[i] < A[i - 1]: find = i - 1 break if find == -1: return A j = find ind = [] 3210 while j < len(A): if len(stack) == 0: stack.append(j) j += 1 continue if A[j] < A[stack[-1]]: if len(stack) == 2: a = stack[-1] stack = [a, j] else: stack.append(j) j += 1 continue else: if len(stack) == 2: ind = [stack[0], stack[1]] stack.pop() if len(stack) == 2: A[stack[0]], A[stack[1]] = A[stack[1]], A[stack[0]] return A if len(ind) == 2: A[ind[0]], A[ind[1]] = A[ind[1]], A[ind[0]] return A return A if len(stack) == 0 and len(ind) == 0: return A
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def __init__(self): pass def findMinMoves(self, machines): N = len(machines) if N == 0: return 0 val_sum = sum(machines) if val_sum % N != 0: return -1 val_each = val_sum // N go_left, go_right, net_change, max_step = 0, 0, 0, 0 for i in range(N): go_left = -go_right go_right = go_right + machines[i] - val_each net_change = go_left + go_right step = max(abs(go_left), abs(go_right), net_change) max_step = max(max_step, step) return max_step
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): sum_before = [0] i = 0 for m in machines: sum_before.append(m + sum_before[i]) i += 1 if sum_before[len(machines)] % len(machines) != 0: return -1 average = int(sum_before[len(machines)] / len(machines)) result = 0 i = 0 for m in machines: left_require = average * i - sum_before[i] right_require = average * (len(machines) - i - 1) - ( sum_before[len(machines)] - sum_before[i + 1] ) if left_require > 0 and right_require > 0: max_tran = left_require + right_require else: max_tran = max(abs(left_require), abs(right_require)) result = max(result, max_tran) i += 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): total = sum(machines) length = len(machines) if total % length != 0: return -1 avg = int(total / length) cum = 0 target = 0 result = 0 for m in machines: cum += m target += avg max_share = m - avg result = max(result, abs(target - cum), max_share) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): s = sum(machines) l = len(machines) if s % l: return -1 a, c, ans = int(s / l), 0, 0 for x in machines: y = x - a c += y ans = max(ans, y, abs(c)) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): if sum(machines) % len(machines) != 0: return -1 mean = sum(machines) // len(machines) cum, step = 0, 0 for x in machines: cum += x - mean step = max(step, abs(cum), x - mean) return step
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): n = len(machines) if sum(machines) % n != 0: return -1 s = sum(machines) k = s // n presum = 0 right = 0 max_net = 0 for i in range(n): presum += machines[i] left = -right right = presum - k * (i + 1) net = left + right max_net = max(max_net, abs(left), abs(right), net) return max_net
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty. For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time . Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1. Example1 Input: [1,0,5] Output: 3 Explanation: 1st move: 1 0 1 1 4 2nd move: 1 2 1 3 3rd move: 2 1 2 2 2 Example2 Input: [0,3,0] Output: 2 Explanation: 1st move: 0 1 2 0 2nd move: 1 2 --> 0 => 1 1 1 Example3 Input: [0,2,0] Output: -1 Explanation: It's impossible to make all the three washing machines have the same number of dresses. Note: The range of n is [1, 10000]. The range of dresses number in a super washing machine is [0, 1e5].
class Solution: def findMinMoves(self, machines): N = len(machines) if sum(machines) % N != 0: return -1 avg = sum(machines) // N result = 0 toNext = 0 for i in range(N): fromPrev = toNext toNext = fromPrev + machines[i] - avg if fromPrev < 0 and toNext > 0: result = max(result, -fromPrev + toNext) else: result = max(result, abs(fromPrev), abs(toNext)) return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR
JJ has a binary string S of length N. JJ can perform the following operation on S: Select an i such that 1 ≤ i ≤ N, and flip S_{i} (i.e. change 0 to 1 and 1 to 0) JJ wants to minimize the number of inversions in S by performing the above operation at most K times. Can you help JJ do so? Recall that a pair of indices (i, j) in S is called an inversion if i < j and S_{i} > S_{j}. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the maximum number of times JJ can perform the given operation. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output the minimum number of inversions possible after performing the given operation at most K times. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 8 3 00010111 5 1 10100 6 2 111000 ----- Sample Output 1 ------ 0 2 3 ----- explanation 1 ------ Test case 1: We are allowed to perform at most $3$ operations. We can perform the following operation on $S$: $0001\underline{0}111 \rightarrow 00011111$ which has $0$ inversions. Therefore $0$ is the answer. Test case 2: We can perform the following operation on $S$: $\underline{1}0100 \rightarrow 00100$ which has $2$ inversions. It can be proven that this is the minimum number of inversions possible. Test case 3: We can perform the following operations on $S$: $1110\underline{0}0 \rightarrow 11101\underline{0} \rightarrow 111011$ which has $3$ inversions. It can be proven that this is the minimum number of inversions possible.
for _ in range(int(input())): N, K = map(int, input().split()) S = list(input()) l, r = 0, N - 1 while l < r and S[l] == "0": l += 1 while l < r and S[r] == "1": r -= 1 A = [] z = 0 s = 0 for i in range(r, l - 1, -1): if S[i] == "0": z += 1 else: A.append((i, z)) s += z A = A[::-1][:K] o = r - l + 1 - z if K >= o or K >= z: print(0) continue for i in A: s -= i[1] o -= 1 ans = s t = 0 while K: if S[r] == "0": i, k = A.pop() s += k - t o += 1 s -= o K -= 1 t += 1 else: o -= 1 ans = min(ans, s) r -= 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
JJ has a binary string S of length N. JJ can perform the following operation on S: Select an i such that 1 ≤ i ≤ N, and flip S_{i} (i.e. change 0 to 1 and 1 to 0) JJ wants to minimize the number of inversions in S by performing the above operation at most K times. Can you help JJ do so? Recall that a pair of indices (i, j) in S is called an inversion if i < j and S_{i} > S_{j}. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the maximum number of times JJ can perform the given operation. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output the minimum number of inversions possible after performing the given operation at most K times. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 8 3 00010111 5 1 10100 6 2 111000 ----- Sample Output 1 ------ 0 2 3 ----- explanation 1 ------ Test case 1: We are allowed to perform at most $3$ operations. We can perform the following operation on $S$: $0001\underline{0}111 \rightarrow 00011111$ which has $0$ inversions. Therefore $0$ is the answer. Test case 2: We can perform the following operation on $S$: $\underline{1}0100 \rightarrow 00100$ which has $2$ inversions. It can be proven that this is the minimum number of inversions possible. Test case 3: We can perform the following operations on $S$: $1110\underline{0}0 \rightarrow 11101\underline{0} \rightarrow 111011$ which has $3$ inversions. It can be proven that this is the minimum number of inversions possible.
t = int(input()) for _ in range(t): line = input() nk = list(map(int, line.split())) n = nk[0] k = nk[1] s = list(input()) i = 0 Zero_values = [] curr_value = 0 for item in s: if item == "0": Zero_values.append(curr_value) else: curr_value += 1 Ones_values = [] curr_value = 0 for i in range(n - 1, -1, -1): if s[i] == "1": Ones_values.append(curr_value) else: curr_value += 1 Zero_values.sort(reverse=True) Ones_values.sort(reverse=True) if len(Zero_values) <= k or len(Ones_values) <= k: print(0) else: final_values = [sum(Zero_values[0:k])] pure_sums = [final_values[0]] for i in range(1, k + 1): new_value = pure_sums[i - 1] - Zero_values[k - i] + Ones_values[i - 1] pure_sums.append(new_value) new_value -= i * (k - i) final_values.append(new_value) print(sum(Zero_values) - max(final_values))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
JJ has a binary string S of length N. JJ can perform the following operation on S: Select an i such that 1 ≤ i ≤ N, and flip S_{i} (i.e. change 0 to 1 and 1 to 0) JJ wants to minimize the number of inversions in S by performing the above operation at most K times. Can you help JJ do so? Recall that a pair of indices (i, j) in S is called an inversion if i < j and S_{i} > S_{j}. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the maximum number of times JJ can perform the given operation. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output the minimum number of inversions possible after performing the given operation at most K times. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 8 3 00010111 5 1 10100 6 2 111000 ----- Sample Output 1 ------ 0 2 3 ----- explanation 1 ------ Test case 1: We are allowed to perform at most $3$ operations. We can perform the following operation on $S$: $0001\underline{0}111 \rightarrow 00011111$ which has $0$ inversions. Therefore $0$ is the answer. Test case 2: We can perform the following operation on $S$: $\underline{1}0100 \rightarrow 00100$ which has $2$ inversions. It can be proven that this is the minimum number of inversions possible. Test case 3: We can perform the following operations on $S$: $1110\underline{0}0 \rightarrow 11101\underline{0} \rightarrow 111011$ which has $3$ inversions. It can be proven that this is the minimum number of inversions possible.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() zs = [0] os = [0] for c in s: if c == "1": zs.append(zs[-1]) os.append(os[-1] + 1) else: zs.append(zs[-1] + 1) os.append(os[-1]) tzs = zs[-1] tos = os[-1] zr = [] ol = [] for i in range(n): if s[i] == "1": v = tzs - zs[i + 1] if v == 0: break else: zr.append(v) for i in range(n - 1, -1, -1): if s[i] == "0": v = os[i + 1] if v == 0: break else: ol.append(v) if k >= len(zr) or k >= len(ol): print(0) continue a1 = [0] for kk in zr: a1.append(a1[-1] + kk) a2 = [0] for kk in ol: a2.append(a2[-1] + kk) mv = float("inf") diff = sum(zr) for i in range(k + 1): j = k - i mv = min(mv, diff - a1[i] - a2[j] + i * j) print(max(0, mv))
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 ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
JJ has a binary string S of length N. JJ can perform the following operation on S: Select an i such that 1 ≤ i ≤ N, and flip S_{i} (i.e. change 0 to 1 and 1 to 0) JJ wants to minimize the number of inversions in S by performing the above operation at most K times. Can you help JJ do so? Recall that a pair of indices (i, j) in S is called an inversion if i < j and S_{i} > S_{j}. ------ Input Format ------ - The first line contains a single integer T - the number of test cases. Then the test cases follow. - The first line of each test case contains two integers N and K - the length of the binary string S and the maximum number of times JJ can perform the given operation. - The second line of each test case contains a binary string S of length N containing 0s and 1s only. ------ Output Format ------ For each test case, output the minimum number of inversions possible after performing the given operation at most K times. ------ Constraints ------ $1 ≤ T ≤ 10^{5}$ $1 ≤ N ≤ 10^{5}$ $0 ≤ K ≤ N$ - Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ ----- Sample Input 1 ------ 3 8 3 00010111 5 1 10100 6 2 111000 ----- Sample Output 1 ------ 0 2 3 ----- explanation 1 ------ Test case 1: We are allowed to perform at most $3$ operations. We can perform the following operation on $S$: $0001\underline{0}111 \rightarrow 00011111$ which has $0$ inversions. Therefore $0$ is the answer. Test case 2: We can perform the following operation on $S$: $\underline{1}0100 \rightarrow 00100$ which has $2$ inversions. It can be proven that this is the minimum number of inversions possible. Test case 3: We can perform the following operations on $S$: $1110\underline{0}0 \rightarrow 11101\underline{0} \rightarrow 111011$ which has $3$ inversions. It can be proven that this is the minimum number of inversions possible.
tl = int(input()) while tl: tl -= 1 n, k = map(int, input().split()) a = str(input()) c0 = 0 c1 = 0 a0 = [] a1 = [] for i in a: if i == "0": c0 += 1 else: c1 += 1 ans = 0 temp = c0 for i in a: if i == "1": ans += temp a1.append(temp) else: temp -= 1 temp1 = c1 for i in reversed(a): if i == "0": a0.append(temp1) else: temp1 -= 1 if k >= min(len(a0), len(a1)): print(0) else: f = 0 for i in range(k): f += a0[i] fans = ans - f for i in range(k): f += a1[i] - a0[k - i - 1] + (i - (k - i - 1)) fans = min(fans, ans - f) if fans < 0: print(0) else: print(fans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) a_sorted = sorted(a, reverse=True) m = int(input()) r = [] for _ in range(m): b = [] k, pos = input().split() k = int(k) pos = int(pos) - 1 for i in range(n - 1, -1, -1): if a[i] > a_sorted[k - 1]: b.append([a[i], i]) i = 0 while len(b) < k: if a[i] == a_sorted[k - 1]: b.append([a[i], i]) i += 1 c = sorted(b, key=lambda x: x[1]) r.append(c[pos][0]) for i in r: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = [int(x) for x in input().split()] a_sorted = sorted(a, reverse=True) m = int(input()) for i in range(m): k, pos = [int(x) for x in input().split()] vals = a_sorted[0:k] result = [] for val in a: if val in vals: result.append(val) vals.remove(val) if len(vals) == 0: break print(result[pos - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) m = int(input()) info = [list(map(int, input().split())) for i in range(m)] sorted_a = sorted(a, reverse=True) ruiseki = [0] * (n + 1) for i in range(n): ruiseki[i + 1] = ruiseki[i] + a[i] for i in range(m): k, pos = info[i] max_num = ruiseki[k] use = [False] * n num = sorted_a[k - 1] cnt = 0 for i in range(n): if a[i] > num: use[i] = True cnt += 1 nokori = k - cnt for i in range(n): if nokori == 0: break if a[i] == num: use[i] = True nokori -= 1 ans = [] for i in range(n): if use[i]: ans.append(i) print(a[ans[pos - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = [a[i], n - i] a.sort() c = list(range(n)) for i in range(n): c[n - i - 1] = [n - a[i][1] + 1, a[i][0]] m = int(input()) for g in range(m): k, pos = map(int, input().split()) b = c[:k] b.sort() print(b[pos - 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = sorted( zip(list(map(int, input().split())), range(n)), key=lambda x: (x[0], -x[1]), reverse=True, ) m = int(input()) for i in range(m): k, pos = map(int, input().split()) b = sorted(a[:k], key=lambda x: x[1]) print(b[pos - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
test_case = """7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4""" length = int(input()) a = [int(x) for x in input().split()] l = [] for x in range(length): l.append([a[x], x + 1]) ll = sorted(l, key=lambda y: (y[0], -y[1])) t = int(input()) for _ in range(t): k, p = [int(x) for x in input().split()] ans = ll[length - k :] final = sorted(ans, key=lambda y: y[1]) print(final[p - 1][0])
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) m = int(input()) for i in range(m): d = dict() k, pos = map(int, input().split()) b = a + [] for i in range(k): d[b.index(max(b))] = max(b) b[b.index(max(b))] = 0 print(d[sorted(d.keys())[pos - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = a[i], -i a.sort() m = int(input()) for j in range(m): t = list(map(int, input().split())) k = t[0] p = t[1] print(sorted(a[-k:], key=lambda x: -x[1])[p - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
import sys input = sys.stdin.readline def answer(a, k, pos): while len(a) > k: mn = min(a) for i in range(len(a) - 1, -1, -1): if a[i] == mn: del a[i] break return a[pos - 1] n = int(input()) a = list(map(int, input().split())) m = int(input()) for _ in range(m): k, pos = map(int, input().split()) print(answer(list(a), k, pos))
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
def custom_key(pair): value, index = pair return -value, index n = int(input()) List = [int(x) for x in input().split()] Sorted = [] for i in range(1, n + 1): Sorted.append([List[i - 1], i]) Sorted.sort(key=custom_key) m = int(input()) for i in range(m): k, pos = map(int, input().split()) X = [] for i in range(k): X.append(Sorted[i]) X.sort(key=lambda x: x[1]) print(X[pos - 1][0])
FUNC_DEF ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) b = sorted(((a[i], -i) for i in range(n)), reverse=True) m = int(input()) for _ in range(m): k, p = map(int, input().split()) print(a[sorted(-i for _, i in b[:k])[p - 1]])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER 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 EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
import sys INF = 10**15 def solve(): global dp dp = [(+INF, []) for _ in range(n + 1)] dp[0] = 0, [] for i in range(1, n + 1): last = a[i - 1] for k in range(n, 0, -1): if dp[k - 1][0] != +INF: new_sum = dp[k - 1][0] - last new_seq = dp[k - 1][1] + [last] if dp[k] > (new_sum, new_seq): dp[k] = new_sum, new_seq (n,) = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) (t,) = map(int, sys.stdin.readline().split()) dp = [] solve() for _ in range(t): k, pos = map(int, sys.stdin.readline().split()) assert dp[k][0] != INF print(dp[k][1][pos - 1])
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER LIST VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = [int(i) for i in input().split()] b = [(a[i], n - i) for i in range(n)] b.sort(reverse=True) b = [(b[i][0], n - b[i][1]) for i in range(n)] m = int(input()) for qu in range(m): k, p = map(int, input().split()) c = b[:k] c.sort(key=lambda x: x[1]) print(c[p - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = [int(x) for x in input().split()] m = int(input()) for i in range(m): kj, posj = map(int, input().split()) a_copy = [x for x in a] for u in range(n - kj): mid = 0 for uuu in range(len(a_copy)): if a_copy[uuu] <= a_copy[mid]: mid = uuu a_copy.pop(mid) print(a_copy[posj - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version 1 ≤ n, m ≤ 100. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]: * [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list); * [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences. Suppose that an additional non-negative integer k (1 ≤ k ≤ n) is given, then the subsequence is called optimal if: * it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; * and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal. Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 ≤ t ≤ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example: * [10, 20, 20] lexicographically less than [10, 21, 1], * [7, 99, 99] is lexicographically less than [10, 21, 1], * [10, 21, 0] is lexicographically less than [10, 21, 1]. You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j. For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30. Input The first line contains an integer n (1 ≤ n ≤ 100) — the length of the sequence a. The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9). The third line contains an integer m (1 ≤ m ≤ 100) — the number of requests. The following m lines contain pairs of integers k_j and pos_j (1 ≤ k ≤ n, 1 ≤ pos_j ≤ k_j) — the requests. Output Print m integers r_1, r_2, ..., r_m (1 ≤ r_j ≤ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j. Examples Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 Note In the first example, for a=[10,20,10] the optimal subsequences are: * for k=1: [20], * for k=2: [10,20], * for k=3: [10,20,10].
n = int(input()) a = list(map(int, input().split())) b = [a] for x in range(n - 1): b.append([]) m = min(a) y = len(a) - 1 while y >= 0 and a[y] != m: b[-1].append(a[y]) y -= 1 y -= 1 while y >= 0: b[-1].append(a[y]) y -= 1 a = b[-1][::-1] m = int(input()) for x in range(m): k, l = map(int, input().split()) if k < n: k = n - (k - 1) - 1 l = len(b[k]) - (l - 1) - 1 print(b[k][l]) else: print(b[0][l - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER 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 IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements. -----Output----- In a single line print a single integer — the maximum number of points Artem can get. -----Examples----- Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
input() list = [int(x) for x in input().split()] stack = [] score = 0 stack.append(list[0]) for i in range(1, len(list)): while len(stack) > 1 and stack[-1] <= min(list[i], stack[-2]): score = score + min(list[i], stack[-2]) stack.pop() stack.append(list[i]) for i in range(1, len(stack) - 1): score = score + min(stack[i - 1], stack[i + 1]) print(score)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements. -----Output----- In a single line print a single integer — the maximum number of points Artem can get. -----Examples----- Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
n = int(input()) cards = list(map(int, input().split())) score = 0 preIndexes = [] nextIndexes = [] isChosen = [0] * n chosens = [] nextIndexes.append(1) preIndexes.append(-1) for i in range(1, n - 1): if cards[i] <= cards[i - 1] and cards[i] <= cards[i + 1]: chosens.append((-cards[i], i)) isChosen[i] = 1 preIndexes.append(i - 1) nextIndexes.append(i + 1) preIndexes.append(n - 2) nextIndexes.append(n) while len(chosens) != 0: chosen = chosens.pop() preChosen = preIndexes[chosen[1]] nextChosen = nextIndexes[chosen[1]] score += min(cards[preChosen], cards[nextChosen]) nextIndexes[preChosen] = nextChosen preIndexes[nextChosen] = preChosen if preChosen != 0 and preIndexes[preChosen] != -1 and nextIndexes[preChosen] != n: if ( cards[preChosen] <= cards[preIndexes[preChosen]] and cards[preChosen] <= cards[nextIndexes[preChosen]] ): if isChosen[preChosen] == 0: isChosen[preChosen] = 1 chosens.append((-cards[preChosen], preChosen)) if ( nextChosen != n - 1 and preIndexes[nextChosen] != -1 and nextIndexes[nextChosen] != n ): if ( cards[nextChosen] <= cards[preIndexes[nextChosen]] and cards[nextChosen] <= cards[nextIndexes[nextChosen]] ): if isChosen[nextChosen] == 0: isChosen[nextChosen] = 1 chosens.append((-cards[nextChosen], nextChosen)) tempNode = nextIndexes[0] while tempNode != n and tempNode != n - 1: score += min(cards[preIndexes[tempNode]], cards[nextIndexes[tempNode]]) tempNode = nextIndexes[tempNode] print(score)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements. -----Output----- In a single line print a single integer — the maximum number of points Artem can get. -----Examples----- Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
n = input() s = [] a = 0 for i in map(int, input().split()): while len(s) > 1 and min(s[-2], i) >= s[-1]: a += min(i, s[-2]) del s[-1] s.append(i) s.sort() print(a + sum(s[0:-2]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements. -----Output----- In a single line print a single integer — the maximum number of points Artem can get. -----Examples----- Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
def maxScore(list): score = 0 stack = [] stack.append(list[0]) for i in range(1, len(list)): while len(stack) > 1 and stack[-1] <= min(list[i], stack[-2]): score = score + min(list[i], stack[-2]) stack.pop() stack.append(list[i]) for i in range(1, len(stack) - 1): score = score + min(stack[i - 1], stack[i + 1]) return score input() l = [int(x) for x in input().split()] print(maxScore(l))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 5·10^5) — the number of elements in the array. The next line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^6) — the values of the array elements. -----Output----- In a single line print a single integer — the maximum number of points Artem can get. -----Examples----- Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
MAXN = 5 * 10**5 + 100 a = [] ans = 0 n = int(input()) a = list(map(int, input().split())) a.append(0) a = [0] + a n = n + 2 arr = [] arr.append(a[0]) arr.append(a[1]) i = 2 while i < n: ln = a[i] l1 = arr[-1] l0 = arr[-2] while l1 <= l0 and l1 <= ln: ans = ans + min(l0, ln) arr.pop() l1 = arr[-1] l0 = arr[-2] arr.append(ln) i = i + 1 for i in range(1, len(arr) - 1): ans += min(arr[i - 1], arr[i + 1]) print(ans)
ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(110000) class Node: def __init__(self): self.children = [] self.subtree_children_count = 0 def post_order(node): count = 1 for child in node.children: child_sub_trees = post_order(child) count += child_sub_trees node.subtree_children_count = count return node.subtree_children_count def subtree_count(root): try: return post_order(root) except ArithmeticError: return -1 def assign_parents(parents, nodes): for i, parent in enumerate(parents, 1): nodes[parent].children.append(nodes[i + 1]) def get_submex(root): ans = -1 def maximizer(node, prev=0): nonlocal ans value = node.subtree_children_count + prev ans = max(ans, value) for child in node.children: maximizer(child, value) maximizer(root) return ans def solve(): for _ in range(int(input())): n = int(input()) parents = list(map(int, input().strip().split())) nodes = [Node() for _ in range(n + 1)] assign_parents(parents, nodes) root = nodes[1] if subtree_count(root) == -1: print(42) continue result = get_submex(root) print(result) solve()
EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
for _ in range(int(input())): n = int(input()) p = list(map(int, input().strip().split())) ans = [0] * (n + 1) nos = [1] * (n + 1) for i in range(n - 2, -1, -1): nos[p[i]] += nos[i + 2] ans[i + 2] += nos[i + 2] ans[p[i]] = max(ans[p[i]], ans[i + 2]) print(ans[1] + nos[1])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline t = int(input()) def dfs(n, p, a): count = 0 maxval = 0 for i in a[n]: if i == p: continue tmaxval, tcount = dfs(i, n, a) maxval = max(maxval, tmaxval) count += tcount return maxval + count + 1, count + 1 for _ in range(t): n = int(input()) a = [] for i in range(n + 1): a.append(list()) e = list(map(int, input().split())) for i in range(1, n): a[e[i - 1]].append(i + 1) print(dfs(1, 1, a)[0])
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys sys.setrecursionlimit(1000000) def dfs(root, graph): retrieval = [] try: for i in graph[root]: retrieval.append(dfs(i, graph)) if retrieval != []: mx = retrieval[0][0] + retrieval[0][1] totalNodesInSubtree = 0 for i in retrieval: totalNodesInSubtree += i[1] if i[0] + i[1] > mx: mx = i[0] + i[1] return [mx, totalNodesInSubtree + 1] else: return [0, 1] except: return [0, 1] for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) graph = {} for i in range(n - 1): if graph.get(l[i]): graph[l[i]].add(i + 2) else: graph[l[i]] = set([i + 2]) ans = dfs(1, graph) print(ans[0] + ans[1])
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN LIST VAR BIN_OP VAR NUMBER RETURN LIST NUMBER NUMBER RETURN LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(999999) def children(g, n, child): if n in g.keys(): x = 0 for adg in g[n]: x += children(g, adg, child) child[n] = len(g[n]) + x return len(g[n]) + x else: child[n] = 0 return 0 def check(g, n, child): if n in g.keys(): x = 0 for adg in g[n]: l = check(g, adg, child) if l > x: x = l if x > 0: return child[n] + 1 + x else: return 1 for _ in range(int(input())): n = int(input()) a = [0] + list(map(int, input().split())) tree = {} for i in range(1, n): tree[a[i]] = tree.setdefault(a[i], []) tree[a[i]].append(i + 1) child = [0] * (n + 1) children(tree, 1, child) print(check(tree, 1, child))
EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys sys.setrecursionlimit(1000000) def dfs(root, parent, op): val = 0 if root in dic: for child in dic[root]: if child != parent: val += dfs(child, root, op) op[root] = val + 1 return op[root] def dfs2(root, parent, op): val = -1 if root in dic: for child in dic[root]: if child != parent: val = max(val, dfs2(child, root, op), 0) if val == -1: return 1 else: return op[root] + val for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) dic = {} for i in range(n - 1): x = arr[i] if x in dic: dic[x].append(i + 2) else: dic[x] = [i + 2] if i + 2 in dic: dic[i + 2].append(x) else: dic[i + 2] = [x] op = [(0) for i in range(n + 1)] parent = 0 root = 1 dfs(root, parent, op) print(dfs2(root, parent, op))
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(10**8) def max_mex(dept): for i in at_dept[dept]: m, l = 0, 0 for j in child[i]: m = max(m, mex[j][1]) l += mex[j][0] mex[i] = [l + 1, m + l + 1] if dept > 1: max_mex(dept - 1) def all_dept(arr, dept=2): for i in arr: at_dept[dept].append(i) all_dept(child[i], dept + 1) for T in range(int(input())): n = int(input()) parent = list(map(int, input().split())) child = [[] for i in range(n + 1)] for i in range(n - 1): child[parent[i]].append(i + 2) at_dept = [[] for i in range(n + 1)] at_dept[1].append(1) all_dept(child[1]) for arr in at_dept[::-1]: if arr == []: at_dept.pop() else: break mex = [0] * (n + 1) for i in at_dept[-1]: mex[i] = [1, 1] max_mex(len(at_dept) - 2) print(mex[1][1])
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(10**6) class Graph: def __init__(self, V): self.graph = [[] for i in range(V + 1)] self.Visited = set() self.height = 1 def addEdges(self): arr = [int(x) for x in input().split()] for i in range(len(arr)): self.graph[i + 1].append(arr[i] - 1) self.graph[arr[i] - 1].append(i + 1) def DFS(self, Node): self.Visited.add(Node) if len(self.graph[Node]) == 0: return 1, 1 node = 1 mex = 1 for each in self.graph[Node]: if each not in self.Visited: n1, m1 = self.DFS(each) node += n1 mex = max(m1, mex) return node, mex + node for _ in range(int(input())): V = int(input()) gra = Graph(V) gra.addEdges() x, y = gra.DFS(0) print(y - 1)
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys sys.setrecursionlimit(10**6) def recurse(root): if adj[root] == []: return 1, 1 sub = 1 max1 = 0 for v in adj[root]: x, y = recurse(v) sub += x max1 = max(max1, y) return sub, sub + max1 for _ in range(int(input())): n = int(input()) p = list(map(int, input().split())) adj = {} for i in range(1, n + 1): adj[i] = [] for i in range(n - 1): parent, child = p[i], i + 2 adj[parent].append(child) x, y = recurse(1) print(y)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR LIST RETURN NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys sys.setrecursionlimit(10**6) input = __import__("sys").stdin.readline def number_of_elements(dec, mem, node): if node in mem: return mem[node] res = 1 for j in dec[node]: res += number_of_elements(dec, mem, j) mem[node] = res return res def solve(dec, mem2, node, mem): if node in mem2: return mem2[node] if not dec[node]: mem2[node] = 1 return 1 ans = mem[node] p = 1 for j in dec[node]: p = max(p, solve(dec, mem2, j, mem)) mem2[node] = ans + p return ans + p for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) dec = {} for i in range(n - 1): if a[i] in dec: dec[a[i]].append(i + 2) else: dec[a[i]] = [i + 2] for i in range(1, n + 1): if i not in dec: dec[i] = [] mem = {} number_of_elements(dec, mem, 1) result = solve(dec, {}, 1, mem) print(result)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR DICT NUMBER VAR EXPR FUNC_CALL VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(999999) def createTree(n, par): graph = [[] for i in range(n + 1)] for i in range(len(par)): graph[par[i]].append(i + 2) return graph def recurse(graph, root): if len(graph[root]) == 0: return [1, 1] noNodes = 1 root_sum = -1 for i in range(len(graph[root])): temp = recurse(graph, graph[root][i]) noNodes += temp[0] root_sum = max(root_sum, temp[1]) return [noNodes, root_sum + noNodes] for i in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] graph = createTree(n, arr) print(recurse(graph, 1)[1])
EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER RETURN LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys input, print = sys.stdin.readline, sys.stdout.write sys.setrecursionlimit(10**6) def ans(dic, n): if dic.get(n) != None: b = [] for a in dic[n]: b.append(ans(dic, a)) mx = 0 node = 0 for a in b: if a[0] > mx: mx = a[0] node += a[1] node += len(dic[n]) return mx + node + 1, node else: return 1, 0 for i in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] dic = {} for j in range(1, n): temp = a[j - 1] if dic.get(temp) == None: dic[temp] = [j + 1] else: dic[temp].append(j + 1) anss = ans(dic, 1) print(str(anss[0]) + "\n")
IMPORT ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NONE ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
from sys import setrecursionlimit setrecursionlimit(10**8) def dfs(p): val, nm = 0, 1 for i in child[p]: x, mex = dfs(i) nm += mex val = max(val, x) return [val + nm, nm] for T in range(int(input())): n = int(input()) p = list(map(int, input().split())) child = [[] for i in range(n + 1)] for i in range(n - 1): child[p[i]].append(i + 2) print(dfs(1)[0])
EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN LIST BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
import sys def eprint(*args): print(*args, file=sys.stderr) zz = 1 sys.setrecursionlimit(10**6) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def string(s): return "".join(s) def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def bo(i): return ord(i) - ord("A") def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = fi() while t > 0: t -= 1 n = fi() p = li() a = [[] for i in range(n + 1)] for i in range(n - 1): a[i + 2].append(p[i]) a[p[i]].append(i + 2) subtree = [1] * (n + 1) def dfs(i, par=-1): for j in a[i]: if j != par: dfs(j, i) subtree[i] += subtree[j] dfs(1, -1) ans = [0] * (n + 1) for i in range(1, n + 1): if i == 1: ans[i] = n else: ans[i] += subtree[i] + ans[p[i - 2]] print(max(ans))
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) d = {(i + 1): [] for i in range(n)} for i in range(n - 1): d[l[i]] += [i + 2] ch = {(i + 1): (0) for i in range(n)} levels = [] queue = [1] while len(queue) != 0: levels.append(queue.copy()) nq = len(queue.copy()) for i in range(nq): for j in d[queue[i]]: queue.append(j) queue = queue[nq:] for i in range(len(levels) - 1, 0, -1): for node in levels[i]: ch[l[node - 2]] += ch[node] + 1 ans = 0 lst = list(ch.values()) for i in range(n): lst[i] += 1 for lev in levels[1:]: for i in lev: parent = l[i - 2] lst[i - 1] += lst[parent - 1] ans = max(lst) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
def input_int_list(): return [int(x) for x in input().split()] def dfsnr(node, tree): stack = [node] visited = set() result = dict() while stack.__len__() != 0: item = stack[-1] if item not in visited: visited.add(item) if item in tree.keys(): stack += tree[item] else: m = -1 count = 1 if item in tree.keys(): for i in tree[item]: if m < result[i][0]: m = result[i][0] count += result[i][1] else: m = 0 result[item] = m + count, count stack.pop() print(result[1][0]) def test_case(): N = int(input()) L = input_int_list() tree = dict() for i in range(len(L)): if L[i] in tree.keys(): tree[L[i]].append(i + 2) else: tree[L[i]] = [i + 2] dfsnr(1, tree) T = int(input()) for t in range(T): test_case()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The MEX of a set of integers is defined as the smallest non-negative integer that does not belong to this set. For example, $\mathrm{MEX}(\{0,2,3\}) = 1$ and $\mathrm{MEX}(\{1,3\}) = 0$. Chef has a tree with $N$ nodes (numbered $1$ through $N$). The tree is rooted at node $1$. Chef wants to assign a non-negative integer to each node in such a way that each integer between $0$ and $N-1$ (inclusive) is assigned to exactly one node. For each node $u$, consider the integers assigned to the nodes in the subtree of $u$ (including $u$); let $a_u$ denote the MEX of these integers. Chef wants $a_1 + a_2 + \ldots + a_N$ to be as large as possible. Find the maximum possible value of this sum. -----Input----- - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains a single integer $N$. - The second line contains $N-1$ space-separated integers $p_1, p_2, \ldots, p_{N-1}$. For each valid $i$, the node $p_i$ is the parent of the node $i+1$. -----Output----- For each test case, print a single line containing one integer ― the maximum sum of subtree MEX-s which can be obtained if you assign the weights optimally. -----Constraints----- - $1 \le T \le 5$ - $2 \le N \le 10^5$ - $1 \le p_i < i$ for each valid $i$ -----Subtasks----- Subtask #1 (100 points): original constraints -----Example Input----- 2 3 1 1 5 1 1 2 2 -----Example Output----- 4 9
for _ in range(int(input())): n = int(input()) con = list(map(int, input().split())) le = [(1) for i in range(n)] for i in range(n - 2, -1, -1): le[con[i] - 1] += le[i + 1] ans = [(0) for i in range(n)] ma = 0 tra = [[] for i in range(n + 1)] no = 2 for i in con: tra[i].append(no) no += 1 bfs = [1] l = 0 while l < len(bfs): go = bfs[l] for i in tra[go]: bfs.append(i) l += 1 ans[0] = le[0] for i in range(1, n, 1): ci = bfs[i] va = le[ci - 1] + ans[con[ci - 2] - 1] if va > ma: ma = va ans[ci - 1] = va print(ma)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
import sys from sys import maxsize def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_list_string(): return list(map(str, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_print_int(x): sys.stdout.write(str(x) + "\n") def get_print(x): sys.stdout.write(x + "\n") def get_print_int_same(x): sys.stdout.write(str(x) + " ") def get_print_same(x): sys.stdout.write(x + " ") def solve(): for _ in range(get_int()): n, m, x = get_ints() h = get_list() data = [] for i in range(n): data.append([h[i], i]) data.sort() ans = [0] * n for i in range(n): ans[data[i][1]] = i % m + 1 get_print("YES") get_print(" ".join(map(str, ans))) solve()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
R = lambda: map(int, input().split()) (t,) = R() exec( t * """n,m,x=R();r=[0]*n;i=0 for _,j in sorted(zip(R(),range(n))):r[j]=i=i%m+1 print('YES',*r) """ )
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
def get_ans(n, m, x, l): left = -1 right = n check_ans = [0] * m ans_l = [-1] * n dir = "l" while 1: check = False if left < right: if dir == "l": curr_left = left for i in range(curr_left + 1, min(right, curr_left + 1 + m)): ans_l[l[i][1]] = i - curr_left check_ans[i - curr_left - 1] += l[i][0] left += 1 check = True dir = "r" elif dir == "r": curr_right = right for i in range(curr_right - 1, max(curr_right - 1 - m, left), -1): ans_l[l[i][1]] = curr_right - i right -= 1 check = True check_ans[curr_right - i - 1] += l[i][0] dir = "l" if not check: break for i in range(1, m): if abs(check_ans[i] - check_ans[i - 1]) > x: return False, ans_l return True, ans_l t = int(input()) for _ in range(t): n, m, x = map(int, input().split()) l = list(map(int, input().split())) for i in range(n): l[i] = [l[i], i] l.sort(key=lambda x: x[0]) check, ans = get_ans(n, m, x, l) if check: print("YES") print(*ans) else: print("NO")
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING WHILE NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING IF VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR RETURN NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
t = int(input()) for i in range(t): n, m, x = map(int, input().split()) lst = list(map(int, input().split())) lst1 = [] for j in range(n): lst1.append((lst[j], j)) lst1.sort(key=lambda y: y[0]) dic = {} for j in lst1: dic[j] = -1 q = n // m ind = 0 c = 1 for h in range(q * m): dic[lst1[ind]] = c ind += 1 c += 1 if c > m: c = 1 rem = n % m c = 1 for h in range(rem): dic[lst1[ind]] = c ind += 1 c += 1 dic1 = {} for g in range(1, m + 1): dic1[g] = 0 for h in dic.keys(): dic1[dic[h]] += h[0] ma = -1000000000.0 mi = 1000000000.0 for h in dic1.values(): if h > ma: ma = h if h < mi: mi = h if abs(ma - mi) > x: print("NO") else: print("YES") lst2 = [] for h in range(n): lst2.append(0) for k in dic.keys(): lst2[k[1]] = dic[k] print(*lst2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
for _ in range(int(input())): n, m, x = [int(x) for x in input().split()] h = [int(x) for x in input().split()] idx = sorted(range(n), key=lambda i: h[i]) ans = [0] * n count = 0 for i in idx: ans[i] = count + 1 count = (count + 1) % m print("YES") print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
for _ in range(int(input())): n, m, x = map(int, input().split()) q = list(map(int, input().split())) q = [(q[i], i) for i in range(n)] q.sort() ans = [None] * n for i in range(n): ans[q[i][1]] = i % m print("YES") for i in ans: print(i + 1, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
for _ in range(int(input())): n, m, x = map(int, input().split()) a = sorted(zip(map(int, input().split()), [i for i in range(n)])) loc = [0] * n for i in range(n): loc[a[i][1]] = i % m + 1 print("YES") print(*loc)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
for _ in range(int(input())): n, m, x = map(int, input().split()) a1 = list(map(int, input().split())) a = [] for i in range(n): a.append([a1[i], i]) a.sort(reverse=True) move = 1 ind = 0 res = [0] * n print("YES") for i in range(n): if ind == -1: ind = 0 move = 1 elif ind == m: ind = m - 1 move = -1 res[a[i][1]] = ind + 1 ind += move print(*res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
import sys input = sys.stdin.readline inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()] for _ in range(1, inp() + 1): n, m, k = ip() x = ip() xx = sorted(x) y = [[] for i in range(m)] mind = m - 1 for i in range(n - 1, n - m - 1, -1): for j in range(i, -1, -m): y[mind].append(xx[j]) mind -= 1 w = [sum(i) for i in y] ans = "YES" for i in range(1, m): if abs(w[i] - w[i - 1]) > k: ans = "NO" break if ans == "NO": print(ans) continue dt = {} for i in x: dt[i] = [] for i in range(m): for j in y[i]: dt[j].append(i) z = [0] * n for i in range(n): z[i] = dt[x[i]].pop(-1) + 1 print(ans) print(*z)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
def solve(A, n, m, x): li = [(it, i) for i, it in enumerate(A)] li.sort() ans = [-1] * n oc = 0 while li: t = [] if oc % 2: t = li[:m] li = li[m:] else: for i in range(min(m, len(li))): t.append(li.pop()) for i, (v, ind) in enumerate(t): ans[ind] = i + 1 print("YES") print(*ans) for case in range(int(input())): n, m, x = map(int, input().split()) A = list(map(int, input().split())) solve(A, n, m, x)
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
T = int(input()) for _ in range(T): n, m, x = map(int, input().split()) a1 = list(map(int, input().split())) a = [] for i in range(n): a.append([a1[i], i]) a.sort() print("YES") ans = [] weights = [] for i in range(m): weights.append([0, i]) ans.append([]) i = 0 while i + m < n: weights.sort(reverse=True) for j in range(i, i + m): ans[weights[j - i][1]].append(a[j][1]) weights[j - i][0] += a[j][0] i += m weights.sort() for j in range(i, n): ans[weights[j - i][1]].append(a[j][1]) final = [0] * n count = 0 for i in ans: count += 1 for j in i: final[j] = count print(*final)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST NUMBER VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
def indsort(s): li = [] for i in range(len(s)): li.append([s[i], i]) li.sort() sort_index = [] for x in li: sort_index.append(x[1]) return sort_index t = int(input()) for _ in range(t): n, m, x = map(int, input().split()) h = list(map(int, input().split())) ind = indsort(h) ans = [0] * n hei = [0] * m for i in range(n): if (i + 1) % m == 0: mod = m else: mod = (i + 1) % m ans[ind[i]] = mod hei[mod - 1] += h[ind[i]] if max(hei) - min(hei) > x: print("NO") else: print("YES") print(*ans)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Phoenix has $n$ blocks of height $h_1, h_2, \dots, h_n$, and all $h_i$ don't exceed some value $x$. He plans to stack all $n$ blocks into $m$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $x$. Please help Phoenix build $m$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. -----Input----- The input consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains three integers $n$, $m$, and $x$ ($1 \le m \le n \le 10^5$; $1 \le x \le 10^4$) — the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $n$ space-separated integers ($1 \le h_i \le x$) — the heights of the blocks. It is guaranteed that the sum of $n$ over all the test cases will not exceed $10^5$. -----Output----- For each test case, if Phoenix cannot build $m$ towers that look beautiful, print NO. Otherwise, print YES, followed by $n$ integers $y_1, y_2, \dots, y_n$, where $y_i$ ($1 \le y_i \le m$) is the index of the tower that the $i$-th block is placed in. If there are multiple solutions, print any of them. -----Examples----- Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 -----Note----- In the first test case, the first tower has height $1+2+3=6$ and the second tower has height $1+2=3$. Their difference is $6-3=3$ which doesn't exceed $x=3$, so the towers are beautiful. In the second test case, the first tower has height $1$, the second tower has height $1+2=3$, and the third tower has height $3$. The maximum height difference of any two towers is $3-1=2$ which doesn't exceed $x=3$, so the towers are beautiful.
for _ in range(int(input())): n, m, x = list(map(int, input().split())) l = list(map(int, input().split())) print("YES") a, an, p = [], [0] * n, 0 for i in range(n): a.append([l[i], i]) a.sort() for i in range(n): an[a[i][1]] = p + 1 p = (p + 1) % m print(*an)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR LIST BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR