description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, nums): pair = [] for i in range(n): pair.append([nums[i], i]) pair.sort() pair[0][0] = -1 k = pair[0][1] for i in range(n): if pair[i][1] < k: pair[i][0] = k else: pair[i][0] = -1 k = pair[i][1] for i in range(n): nums[pair[i][1]] = pair[i][0] return nums
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): out = [] for lp in range(len(Arr)): rp = len(Arr) - 1 flag = 0 while rp > lp: if Arr[lp] > Arr[rp]: out.append(rp) flag = 1 break rp -= 1 if flag == 0: out.append(-1) return out
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): ans = [] for i in range(N): first_num = Arr[i] target = -1 for j in range(N - 1, i, -1): if Arr[j] < first_num: target = j break ans.append(target) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, a): ans = [-1] * n suff = [0] * n suff[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): suff[i] = min(a[i], suff[i + 1]) for i in range(n): ind = -1 low = i + 1 high = n - 1 while low <= high: mid = (high + low) // 2 if suff[mid] < a[i]: ind = mid low = mid + 1 else: high = mid - 1 ans[i] = ind return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, n, arr): i = 0 a = [-1] * n j = n - 1 for i in range(n): while j > i: if arr[i] > arr[j]: a[i] = j break else: j -= 1 j = n - 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, A): st = [] size = 0 ans = [-1] * N for i in range(N - 1, -1, -1): s = 0 e = size - 1 while s <= e: mid = (s + e) // 2 if A[st[mid]] >= A[i]: s = mid + 1 else: ans[i] = st[mid] e = mid - 1 if ans[i] == -1: st.append(i) size += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): def binary_search(l, h, x): ans = -1 while l <= h: mid = (l + h) // 2 if suf[mid] < x: ans = mid l = mid + 1 else: h = mid - 1 return ans suf = [-1] * N suf[-1] = Arr[-1] for i in range(N - 2, -1, -1): suf[i] = min(suf[i + 1], Arr[i]) res = [] for i in range(N - 1): res.append(binary_search(i + 1, N - 1, Arr[i])) res.append(-1) return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): res = [] j = N - 1 track = 0 for i in range(N): for j in range(N - 1, i, -1): if Arr[i] > Arr[j]: res.append(j) track = 1 break if track == 0: res.append(-1) track = 0 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): post = [Arr[-1] for _ in range(N)] for i in range(N - 2, -1, -1): post[i] = min(post[i + 1], Arr[i]) res = [] for i in range(N - 1): tar = Arr[i] l = i + 1 r = N - 1 while l <= r: m = l + (r - l) // 2 if post[m] < tar: l = m + 1 else: r = m - 1 if i + 1 <= r <= N - 1: res.append(r) else: res.append(-1) res.append(-1) return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): result = [] for ind in range(0, N): j = N - 1 while j > ind: if Arr[j] < Arr[ind]: result.append(j) break j -= 1 if ind == j: result.append(-1) return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
def check(arr, element, i, j): j -= 1 while i != j: if element > arr[j]: return j else: j -= 1 return -1 class Solution: def farNumber(self, N, arr): ans = [] for i in range(N): ans.append(check(arr, arr[i], i, N)) return ans
FUNC_DEF VAR NUMBER WHILE VAR VAR IF VAR VAR VAR RETURN VAR VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): n = N a = Arr l = [] suffix_min = [(0) for i in range(n)] suffix_min[n - 1] = a[n - 1] for i in range(n - 2, -1, -1): suffix_min[i] = min(suffix_min[i + 1], a[i]) for i in range(n): low = i + 1 high = n - 1 ans = -1 while low <= high: mid = (low + high) // 2 if suffix_min[mid] < a[i]: ans = mid low = mid + 1 else: high = mid - 1 l.append(ans) return l
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def binarysearch(self, arr, l, r, k): if l <= r: mid = (l + r) // 2 if arr[mid] < k: return self.binarysearch(arr, mid + 1, r, k) else: return self.binarysearch(arr, l, mid - 1, k) return r def farNumber(self, N, Arr): suffixmin = [0] * N suffixmin[N - 1] = Arr[N - 1] for i in range(N - 2, -1, -1): suffixmin[i] = min(suffixmin[i + 1], Arr[i]) res = [] for i in range(N): l = self.binarysearch(suffixmin, i + 1, N - 1, Arr[i]) if l <= i: res.append(-1) else: res.append(l) return res
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
def bs(A, key, l, r): ans = -1 while l <= r: mid = l + (r - l >> 1) if A[mid] < key: ans = mid l = mid + 1 else: r = mid - 1 return ans class Solution: def farNumber(self, N, A): suff = [0] * N suff[-1] = A[-1] for i in range(N - 2, -1, -1): suff[i] = min(A[i], suff[i + 1]) ans = [0] * N ans[-1] = -1 for i in range(N - 2, -1, -1): ix = bs(suff, A[i], i, N - 1) ans[i] = ix return ans
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): temp = [] i = 0 ans = [] while i < N: j = N - 1 while j >= 0 and j > i and Arr[j] >= Arr[i]: j -= 1 if i != j: ans.append(j) else: ans.append(-1) i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): r = Arr[::-1] b = [-1] * N for i in range(len(Arr)): for j in range(len(Arr) - 1, i, -1): if Arr[j] < Arr[i]: b[i] = j break return b
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): l = [] for i in range(N - 1): c = 0 for j in range(N - 1, i, -1): if Arr[j] < Arr[i]: l.append(j) c = 1 break if c == 0: l.append(-1) l.append(-1) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): suffix_min = [float("inf")] * N ans = [-1] * N for idx in range(N - 1, -1, -1): if idx + 1 < N: suffix_min[idx] = min(suffix_min[idx + 1], Arr[idx]) else: suffix_min[idx] = Arr[idx] for idx in range(N): low = idx + 1 high = N - 1 while low <= high: mid = (low + high) // 2 if suffix_min[mid] < Arr[idx]: ans[idx] = mid low = mid + 1 else: high = mid - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array Arr[] of size N. For every element in the array, the task is to find the index of the farthest element in the array to the right which is smaller than the current element. If no such number exists then print -1. Note: 0 based indexing. Example 1: Input: N=5 Arr[] = {3, 1, 5, 2, 4} Output: 3 -1 4 -1 -1 Explanation: Arr[3] is the farthest smallest element to the right of Arr[0]. Arr[4] is the farthest smallest element to the right of Arr[2]. And for the rest of the elements, there is no smaller element to their right. Example 2: Input: N=5 Arr[] = {1, 2, 3, 4, 0} Output: 4 4 4 4 -1 Your Task: You don't need to read input or print anything. Your task is to complete the function farNumber() which takes the N (number of elements in Array Arr) ,Arr[], and returns the array of farthest element to the right for every element of the array. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 1e5 0 ≤ Arr[i] ≤ 1e9
class Solution: def farNumber(self, N, Arr): arr = list(enumerate(Arr)) arr = sorted(arr, key=lambda x: x[1]) ans = [-1] * len(Arr) maxi = -1 for i, num in arr: if maxi > i: ans[i] = maxi else: maxi = i return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for i in range(t): n = int(input()) m = [] for j in range(n + 1): m.append([]) m[n].append([0, n - 1]) current = 1 ans = [0] * n for j in range(n): m[n - j] = sorted(m[n - j], key=lambda student: student[0]) for q in m[n - j]: if (n - j) % 2 == 0: ans[(q[1] + q[0]) // 2] = current current += 1 m[(n - j) // 2].append([(q[1] + q[0]) // 2 + 1, q[1]]) if n - j > 0: m[(n - j) // 2 - 1].append([q[0], (q[1] + q[0]) // 2 - 1]) else: ans[(q[1] + q[0]) // 2] = current current += 1 m[(n - j) // 2].append([q[0], (q[1] + q[0]) // 2 - 1]) m[(n - j) // 2].append([(q[1] + q[0]) // 2 + 1, q[1]]) for j in ans: print(j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 LIST EXPR FUNC_CALL VAR VAR LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER LIST BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER LIST VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER LIST VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER LIST BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def dfs(ans, l, r): if r - l < 0: return ans.append((l, r)) mid = l + (r - l) // 2 left = l, mid - 1 right = mid + 1, r dfs(ans, *left) dfs(ans, *right) for _ in range(int(input())): n = int(input()) ans = [0] * n a = [] dfs(a, 0, n - 1) a.sort(key=lambda x: abs(x[0] - x[1]), reverse=True) i = 1 for l, r in a: mid = l + (r - l) // 2 ans[mid] = i i += 1 print(*ans)
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR 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 VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for i in range(t): num = int(input()) ans = [0] * num l = 0 count = 1 data = {num: [l]} lisp = [num] flag = 0 for i in lisp: subseqs = sorted(data[i]) n = i for j in subseqs: l = j r = l + n - 1 if n % 2 == 0: pos = l + n // 2 - 1 if n // 2 not in lisp: lisp.append(n // 2) if n // 2 - 1 not in lisp: lisp.append(n // 2 - 1) if n // 2 in data: data[n // 2].append(pos + 1) else: data[n // 2] = [pos + 1] if n // 2 - 1 in data: data[n // 2 - 1].append(l) else: data[n // 2 - 1] = [l] ans[pos] = count count += 1 else: pos = l + n // 2 if n // 2 not in lisp: lisp.append(n // 2) if n // 2 in data: data[n // 2].append(l) data[n // 2].append(pos + 1) else: data[n // 2] = [l, pos + 1] ans[pos] = count count += 1 if count == num + 1: flag = 1 break if flag == 1: break 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR LIST VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER LIST VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def seg(a, b): if a > b: return global arr global ind arr.append((b - a, -a)) m = a + (b - a) // 2 ind[b - a, -a] = m seg(a, m - 1) seg(m + 1, b) for _ in range(int(input())): n = int(input()) arr = [] ind = {} ans = [0] * n seg(0, n - 1) arr = sorted(arr, reverse=True) for i in range(n): index = ind[arr[i]] ans[index] = i + 1 print(*ans)
FUNC_DEF IF VAR VAR RETURN EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def action(n): def order(l, r, index): if l == r: index.append((1, abs(l))) return if (l - r + 1) % 2 == 1: mid = (l + r) // 2 else: mid = (l + r - 1) // 2 index.append((r - l + 1, abs(mid))) if l < mid: order(l, mid - 1, index) if mid < r: order(mid + 1, r, index) index = [] order(-n, -1, index) index = sorted(index, reverse=True) count = 1 arr = [0] * n for length, key in index: arr[-key] = count count += 1 return arr t = int(input()) for i in range(t): n = int(input()) arr = action(n) for k in arr: print(k, end=" ") print("")
FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys def answer(n): ans = [(0) for _ in range(n)] stack = [(0, n - 1)] buckets = [[] for _ in range(n + 1)] largest_b = 0 for i in range(n): tup = stack.pop() l = tup[0] r = tup[1] mid = (l + r) // 2 w = r - l + 1 buckets[w].append(mid) if mid - 1 >= l: stack.append((l, mid - 1)) if r >= mid + 1: stack.append((mid + 1, r)) ctr = 1 for i in range(n, 0, -1): if not buckets[i]: continue buckets[i].sort() for midpoint in buckets[i]: ans[midpoint] = ctr ctr += 1 return ans def main(): t = int(sys.stdin.readline()) while t: n = int(sys.stdin.readline()) print(*answer(n)) t -= 1 return main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def f(l, r): if l > r: return mid = (l + r) // 2 a[mid] = [l - r, mid] f(l, mid - 1) f(mid + 1, r) t = int(input()) for _ in range(t): n = int(input()) a = b = [0] * (n + 1) f(1, n) i = 1 a.pop(0) a = sorted(a) for j in a: b[j[1] - 1] = i i += 1 print(*b)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER 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 VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def solve(l, r): if l > r: return mid = (l + r) // 2 if r - l in ls: ls[r - l].append(mid) else: ls[r - l] = [mid] ch.append(r - l) solve(l, mid - 1) solve(mid + 1, r) for _ in range(int(input())): n = int(input()) ls = {} ch = [] ans = [0] * n solve(1, n) ch = sorted(ch, reverse=True) val = 1 for i in ch: h = ls[i] h.sort() for j in h: ans[j - 1] = val val += 1 print(*ans)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for _ in range(int(input())): n = int(input()) rep = [(0) for i in range(n)] fila = [(n - 1, 0, n - 1)] for i in range(n): _, a, b = fila[i] pos = (a + b) // 2 bNew = pos - 1 aNew = pos + 1 if bNew - a >= 0: fila.append((bNew - a, a, bNew)) if b - aNew >= 0: fila.append((b - aNew, aNew, b)) newVec = [] for tam, a, b in fila: newVec.append((tam, -((a + b) // 2))) newVec.sort(reverse=True) for i in range(n): tam, pos = newVec[i] rep[-pos] = i + 1 for i in rep: print(i, end=" ") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def push(a, b): if a > b: return mid = (a + b) // 2 c.append([b - a, -1 * a]) push(mid + 1, b) push(a, mid - 1) for _ in range(int(input())): n = int(input()) c = [] a = [0] * n push(0, n - 1) c.sort(reverse=True) for i in range(n): d = c[i][0] // 2 - c[i][1] a[d] = i + 1 print(*a)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL 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 LIST ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for i in range(t): n = int(input()) d = {n: {(0): n - 1}} l = [0] * n m = n l1 = [n] k = 0 v = 0 while len(d) > 0: l2 = [] if m == 0: break l3 = sorted(d[m]) for v1 in l3: j = v1, d[m][v1] k += 1 if (j[1] - j[0] + 1) % 2 == 0: l[(j[0] + j[1] - 1) // 2] = k c = (j[0] + j[1] - 1) // 2 else: l[(j[0] + j[1]) // 2] = k c = (j[0] + j[1]) // 2 if j[1] - c > 0: if j[1] - c not in d: d[j[1] - c] = {} l1.append(j[1] - c) if c - j[0] > 0: if c - j[0] not in d: d[c - j[0]] = {} l1.append(c - j[0]) if j[1] - c > 0: d[j[1] - c][c + 1] = j[1] if c - j[0] > 0: d[c - j[0]][j[0]] = c - 1 v += 1 if v < len(l1): m = l1[v] else: m = 0 print(*l)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT VAR DICT NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR DICT EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER DICT EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for i in range(t): n = int(input()) dictionary = {} def search_in_depth(l, r): if l <= r: if l == r: if 1 not in dictionary: dictionary[1] = [[l]] else: dictionary[1] += [[l]] return else: if r - l + 1 not in dictionary: dictionary[r - l + 1] = [[l, r]] else: dictionary[r - l + 1] += [[l, r]] if (l + r) % 2 == 1: search_in_depth(l, (l + r - 1) // 2 - 1) search_in_depth((l + r - 1) // 2 + 1, r) else: search_in_depth(l, (l + r) // 2 - 1) search_in_depth((l + r) // 2 + 1, r) search_in_depth(1, n) sorted_dictionary = {i: dictionary[i] for i in sorted(dictionary, reverse=True)} indices = [] for key in sorted_dictionary: for value in sorted_dictionary[key]: if (value[0] + value[-1]) % 2 == 1: indices += [(value[0] + value[-1] - 1) // 2 - 1] else: indices += [(value[0] + value[-1]) // 2 - 1] answer = [0] * n for counter, ind in enumerate(indices, 1): answer[ind] = counter print(*answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER LIST LIST VAR VAR NUMBER LIST LIST VAR RETURN IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST LIST VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER LIST LIST VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR LIST BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER NUMBER VAR LIST BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def decompose(cur): return cur[1] - (-cur[0] - 1) // 2, cur[1] + (-cur[0] + 2) // 2 def getMid(l, r): return (l + r - 1) // 2 t = int(input()) while t > 0: t -= 1 n = int(input()) q = list() tot = 0 a = [(0) for i in range(n)] q.append((-n, getMid(0, n))) while len(q) > 0: q2 = sorted(q) mx = -q2[0][0] q.clear() for cur in q2: if mx > 1 and cur[0] == -1: q.append(cur) continue tot += 1 a[cur[1]] = tot e, b = cur[1], cur[1] + 1 l, r = decompose(cur) if e > l: q.append((-e + l, getMid(l, e))) if r > b: q.append((-r + b, getMid(b, r))) for i in a: print(i, end=" ") print("")
FUNC_DEF RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys def answer(n): ans = [(0) for _ in range(n)] stack = [(0, n - 1)] t_ans = [(0, 0) for _ in range(n)] while len(stack) > 0: tup = stack.pop() l = tup[0] r = tup[1] mid = (l + r) // 2 w = -(r - l + 1) t_ans[mid] = w, mid if mid - 1 >= l: stack.append((l, mid - 1)) if r >= mid + 1: stack.append((mid + 1, r)) t_ans.sort() for i in range(n): pos = t_ans[i][1] ans[pos] = i + 1 return ans def main(): t = int(sys.stdin.readline()) while t: n = int(sys.stdin.readline()) print(*answer(n)) t -= 1 return main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for i in range(int(input())): n = int(input()) a = [0] * (n + 1) b = [[] for i in range(n + 1)] b[n].append(1) j = n ccc = 1 for x in range(n, 0, -1): if len(b[x]) > 0: c = sorted(b[x]) for j in c: x1 = j x2 = x1 + x - 1 if x1 == x2: a[x1] = ccc ccc += 1 else: m = (x1 + x2) // 2 a[m] = ccc ccc += 1 if x1 < m: b[m - x1].append(x1) if m < x2: b[x2 - m].append(m + 1) print(*a[1 : n + 1])
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 BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
d = input() def test(l, r, dict1, list1): if r == l: if r - l in dict1: dict1[r - l].append(l) else: list1.append(r - l) dict1[r - l] = [l] elif r > l: if r - l not in dict1: list1.append(r - l) dict1[r - l] = [(r - l) // 2 + l] else: dict1[r - l].append((r - l) // 2 + l) test(l, (r - l) // 2 + l - 1, dict1, list1) test((r - l) // 2 + l + 1, r, dict1, list1) for j in range(int(d)): dict1 = {} list1 = [] c = input() list2 = [(0) for i in range(int(c))] test(1, int(c), dict1, list1) list1.sort(reverse=True) tag = 1 for i in list1: dict1[i].sort() for j in dict1[i]: list2[j - 1] = tag tag += 1 for i in range(len(list2) - 1): print(list2[i], end=" ") print(list2[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR IF VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) def mycmp(val1, val2): if val1[0] != val2[0]: return val1[0] > val2[0] else: return val1[1] < val2[1] def fun(l, r): if l > r: return mid = int((r + l) / 2) ans[mid] = int(r - l + 1) fun(l, mid - 1) fun(mid + 1, r) for i in range(t): n = int(input()) ans = [0] * (n + 1) fun(1, n) a = [] for i in range(1, n + 1): a.append([ans[i], n - i]) a.sort() a.reverse() for i in range(n): ans[n - a[i][1]] = i + 1 for i in range(1, n + 1): print(ans[i], end=" ") print("")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def array_building(l, r, result): if l > r: return elif l == r: result[l] = 1 return num = (l + r) // 2 result[num] = r - l + 1 array_building(l, num - 1, result) array_building(num + 1, r, result) def solve(): n = int(input()) a = [0] * n result = [0] * n array_building(0, n - 1, result) for i in range(n): result[i] = result[i], i result.sort(key=lambda x: (-x[0], x[1])) for i in range(n): a[result[i][1]] = i + 1 print(" ".join(map(str, a))) def main(): for _ in range(int(input())): solve() main()
FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def solve(): n = int(input()) small = n - 1 large = n indexes = [(0, True)] ans = [(0) for i in range(n)] x = 1 while large > 2: new_indexes = [] large_indexes = [] small_indexes = [] for i, is_large in indexes: if is_large: large_indexes.append(i + (large - 1) // 2) if large % 2 == 0: new_indexes.append((i, False)) new_indexes.append((i + (large - 1) // 2 + 1, True)) else: new_indexes.append((i, True)) new_indexes.append((i + (large - 1) // 2 + 1, True)) elif small > 0: small_indexes.append(i + (small - 1) // 2) if small % 2 == 0: new_indexes.append((i, False)) new_indexes.append((i + (small - 1) // 2 + 1, True)) else: new_indexes.append((i, False)) new_indexes.append((i + (small - 1) // 2 + 1, False)) for i in large_indexes: ans[i] = x x += 1 for i in small_indexes: ans[i] = x x += 1 indexes = new_indexes small = (small + large - 2) // 4 large = small + 1 if large == 2: for i, is_large in indexes: if is_large: ans[i] = x x += 1 for i, is_large in indexes: if is_large: ans[i + 1] = x else: ans[i] = x x += 1 elif large == 1: for i, is_large in indexes: if is_large: ans[i] = x x += 1 print(" ".join(map(str, ans))) t = int(input()) for tc in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def b(l, r): if l > r: return mid = (l + r) // 2 arr.append((l - r, mid)) b(l, mid - 1) b(mid + 1, r) for _ in range(int(input())): n = int(input()) arr = [] res = [0] * n b(0, n - 1) for i, j in enumerate(sorted(arr)): res[j[1]] = i + 1 print(*res)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys input = sys.stdin.readline def dp(l, r): if l <= r: m = (l + r) // 2 vals[m] = [-(r - l + 1), m] dp(l, m - 1) dp(m + 1, r) for _ in range(int(input())): vals = [(0) for i in range(int(input()))] ans = [(0) for i in range(len(vals))] dp(0, len(vals) - 1) vals.sort() for i in range(len(vals)): ans[vals[i][1]] = i + 1 print(*ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def solve(): srek(0, n - 1) ord = argsort(entred) ord.reverse() ans = [0] * n for i in range(n): ans[ord[i]] = i + 1 for a in ans: print(a, end=" ") print() return def srek(l, r): global entred ind = (r - l) // 2 + l if not entred[ind]: entred[ind] = float(r - l) + (n - float(ind)) * 1e-06 + 1 if ind - 1 >= l: srek(l, ind - 1) if ind + 1 <= r: srek(ind + 1, r) return def argsort(seq): return sorted(range(len(seq)), key=seq.__getitem__) t = int(input()) for i in range(1, t + 1): n = int(input()) entred = [0.0] * n solve()
FUNC_DEF EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def bubble_sort(c): n = len(c) for i in range(n): already_sorted = True for j in range(n - i - 1): if c[j][0] < c[j + 1][0]: c[j][0], c[j + 1][0] = c[j + 1][0], c[j][0] already_sorted = False if c[j][0] == c[j + 1][0]: if c[j][1] > c[j + 1][1]: c[j][1], c[j + 1][1] = c[j + 1][1], c[j][1] if already_sorted: break return c def fun(l, r): if l > r: return m = (l + r) // 2 c.append([l - r, m]) fun(l, m - 1) fun(m + 1, r) t = int(input()) while t: t -= 1 n = int(input()) a = [(0) for i in range(n)] c = [] fun(0, n - 1) c.sort() for i in range(1, n + 1): idi = c[i - 1][1] a[idi] = i print(*a)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
from sys import stdin, stdout _input, _print = stdin.buffer.readline, stdout.write _int, _range, _len = int, range, len def heap_push(h, v): i = _len(h) h.append(v) while i - 1 // 2 > 0 and v[0] >= h[(i - 1) // 2][0]: if h[(i - 1) // 2][0] == v[0]: if v[1] < h[(i - 1) // 2][1]: h[(i - 1) // 2], h[i] = h[i], h[(i - 1) // 2] else: break else: h[(i - 1) // 2], h[i] = h[i], h[(i - 1) // 2] i = (i - 1) // 2 def heap_pop(h): l = _len(h) - 1 h[0], h[l] = h[l], h[0] i = 0 while i * 2 + 2 < l: if h[i * 2 + 2][0] > h[i * 2 + 1][0]: temp = i * 2 + 2 elif h[i * 2 + 2][0] == h[i * 2 + 1][0]: if h[i * 2 + 2][1] < h[i * 2 + 1][1]: temp = i * 2 + 2 else: temp = i * 2 + 1 else: temp = i * 2 + 1 if h[i][0] < h[temp][0]: h[i], h[temp] = h[temp], h[i] elif h[i][0] == h[temp][0] and h[i][1] > h[temp][1]: h[i], h[temp] = h[temp], h[i] else: break i = temp else: if i * 2 + 1 < l: if h[i][0] < h[i * 2 + 1][0]: h[i], h[i * 2 + 1] = h[i * 2 + 1], h[i] elif h[i][0] == h[i * 2 + 1][0] and h[i][1] > h[i * 2 + 1][1]: h[i], h[i * 2 + 1] = h[i * 2 + 1], h[i] el = h[-1] h.pop() return el def solution(): for _ in _range(_int(_input())): n = _int(_input()) a = [0] * n h = [(n - 1, 1, n)] for i in _range(n): v, l, r = heap_pop(h) if (r - l) % 2 == 0: temp = (r + l) // 2 else: temp = (r + l - 1) // 2 a[temp - 1] = i + 1 if temp > l: heap_push(h, (temp - 1 - l, l, temp - 1)) if temp < r: heap_push(h, (r - (temp + 1), temp + 1, r)) print(*a) solution()
ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
class Btree: l = [] def shiftup(self, index): value = self.l[index] while index > 0: parent = index - 1 >> 1 parentval = self.l[parent] if value < parentval: self.l[index] = parentval index = parent continue break self.l[index] = value def push(self, el): self.l.append(el) index = len(self.l) - 1 self.shiftup(index) def pop(self): ans = self.l[0] value = self.l.pop() if self.l: index = 0 sonl = 1 totlen = len(self.l) while sonl < totlen: sonr = sonl + 1 if sonr < totlen: if self.l[sonl] > self.l[sonr]: sonl = sonr self.l[index] = self.l[sonl] index = sonl sonl = 2 * index + 1 self.l[index] = value self.shiftup(index) return ans def tuplmk(a, b): return a - 1 - b, a, b def ans(n): ans = [(0) for i in range(n)] h = Btree() h.push(tuplmk(1, n)) for el in range(1, n + 1): seq = h.pop() ind = (seq[1] + seq[2]) // 2 ans[ind - 1] = el h.push(tuplmk(seq[1], ind - 1)) h.push(tuplmk(ind + 1, seq[2])) return " ".join([str(i) for i in ans]) t = int(input()) tlist = [(0) for i in range(t)] d = {} for pset in range(t): n = int(input()) tlist[pset] = n if n in d: print(d[n]) else: answ = ans(n) d[n] = answ print(answ)
CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def order(i_i, i_f): m = i_i + i_f if m % 2 == 0: mid = (m - 1) // 2 else: mid = m // 2 ord.append((i_f - i_i, i_i, mid)) if i_i < mid: order(i_i, mid) if mid + 1 < i_f: order(mid + 1, i_f) for _ in range(int(input())): n = int(input()) arr = [0] * n ord = [] order(0, n) ord.sort(key=lambda x: (-x[0], x[1])) for i in range(len(ord)): arr[ord[i][2]] = i + 1 print(*arr)
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR 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 VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
from sys import exit as sys_ret from sys import stdin, stdout f_input, f_print, f_flush = stdin.readline, stdout.write, stdout.flush class Segment: def __init__(self, l, r): self.left = l self.right = r def __lt__(self, other): lenS = self.right - self.left + 1 lenO = other.right - other.left + 1 if lenS == lenO: return self.left < other.left return lenS > lenO for _ in range(int(f_input())): amount = int(f_input()) mass = [(0) for i in range(amount)] queue = [Segment(0, amount - 1)] i = 0 while i < amount: left, right = queue[i].left, queue[i].right place = (left + right) // 2 if left < place: queue.append(Segment(left, place - 1)) if place < right: queue.append(Segment(place + 1, right)) i += 1 value = 1 for i in sorted(queue): left, right = i.left, i.right place = (left + right) // 2 mass[place] = value value += 1 f_print(" ".join([str(i) for i in mass]) + "\n")
ASSIGN VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR STRING
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for i in range(t): n = int(input()) if n == 1: print(1) elif n == 2: print(1, 2) else: n2 = n // 2 segs = [[] for i in range(n2 + 1)] ans = [0] * n ans[n2 + n % 2 - 1] = 1 segs[n2 - (1 - n % 2) - 1] += [0] segs[n2 - 1] += [n2 + n % 2] num = 2 for i in range(n2, 1, -1): segs[i].sort() for j in segs[i]: n = i + 1 n2 = n // 2 beg = j ans[beg + n2 + n % 2 - 1] = num segs[n2 - (1 - n % 2) - 1] += [beg] segs[n2 - 1] += [beg + n2 + n % 2] num += 1 segs[1].sort() for i in segs[1]: segs[0] += [i + 1] ans[i] = num num += 1 segs[0].sort() for i in segs[0]: ans[i] = num num += 1 print(" ".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER LIST NUMBER VAR BIN_OP VAR NUMBER LIST BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER LIST VAR VAR BIN_OP VAR NUMBER LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for _ in range(t): n = int(input()) res = [0] * (n + 1) dp = [[] for _ in range(n + 1)] dp[n] = [1] count = 1 for i in range(n, 0, -1): if len(dp[i]) > 0: s_dp = sorted(dp[i]) for k in s_dp: if (i - 1) // 2 > 0: dp[(i - 1) // 2].append(k) res[k + (i - 1) // 2] = count count += 1 dp[i // 2].append(k + (i + 1) // 2) print(" ".join(list(map(str, res[1:]))))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for _ in range(int(input())): n = int(input()) count = 1 ar = [[0, n]] ans = [0] * n flag = True while count != n + 1 and flag: flag = False tem = [] temeven = [] for i in ar: if i[1] % 2 != 0: le = i[1] // 2 if le == 0: tem.append([i[0], 1]) else: flag = True ans[i[0] + le] = count count += 1 if le % 2 == 0: temeven.append([i[0], i[1] // 2]) temeven.append([i[0] + le + 1, i[1] // 2]) else: tem.append([i[0], i[1] // 2]) tem.append([i[0] + le + 1, i[1] // 2]) else: le = i[1] // 2 if le == 1: ans[i[0]] = count count += 1 tem.append([i[0] + 1, 1]) else: flag = True ans[i[0] + le - 1] = count count += 1 if le % 2 == 0: temeven.append([i[0] + le, i[1] // 2]) tem.append([i[0], i[1] // 2 - 1]) else: tem.append([i[0] + le, i[1] // 2]) temeven.append([i[0], i[1] // 2 - 1]) tem.sort() temeven.sort() if not tem or not temeven: ar = (tem + temeven).copy() elif tem[0][1] > temeven[0][1]: ar = (tem + temeven).copy() else: ar = (temeven + tem).copy() for i in ar: ans[i[0]] = count count += 1 print(*ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL BIN_OP VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def rec(l, r): global pos if l > r: return m = (l + r) // 2 pos[m] = [l - r, m] rec(l, m - 1) rec(m + 1, r) def zeroes(n): ans = [0] * n global pos rec(0, n - 1) for i, j in enumerate(sorted(pos)): ans[j[1]] = i + 1 print(*ans) return "" for i in range(int(input())): a = int(input()) pos = [0] * a print(zeroes(a))
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN STRING 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 VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for f in range(int(input())): n = int(input()) segs = [[n, 0]] sol = [0] * n i = 0 prev = n j = 0 while i < n: i += 1 if j < 0: segs.sort(reverse=True) j = 0 while j < len(segs) and segs[j][0] == segs[j + 1][0]: j += 1 prev = segs[0][0] m = segs[j][1] + (segs[j][0] - 1) // 2 sol[m] = i segs.append([segs[j][0] // 2, m + 1]) segs.append([(segs[j][0] - 1) // 2, segs[j][1]]) segs[j][0] = 0 j -= 1 print(*sol)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for i in range(int(input())): n = int(input()) if n == 1: print(1) continue step = 1 result = [(0) for j in range(n + 1)] recorder = {n: [[1, n]]} max_value = n while max_value > 0: recorder[max_value].sort(key=lambda a: a[0]) while recorder[max_value]: temp = recorder[max_value].pop(0) result[(temp[0] + temp[1]) // 2] = step step += 1 left_node = [temp[0], (temp[0] + temp[1]) // 2 - 1] right_node = [(temp[0] + temp[1]) // 2 + 1, temp[1]] if left_node[0] <= left_node[1]: try: recorder[left_node[1] - left_node[0]].append(left_node) except: recorder[left_node[1] - left_node[0]] = [left_node] if right_node[0] <= right_node[1]: try: recorder[right_node[1] - right_node[0]].append(right_node) except: recorder[right_node[1] - right_node[0]] = [right_node] recorder.pop(max_value) max_value = max(recorder.keys()) for j in range(1, n + 1): if result[j] == 0: result[j] = step step += 1 print(" ".join(map(str, result[1:])))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT VAR LIST LIST NUMBER VAR ASSIGN VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER LIST VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
a = [(0, 0)] * 200001 b = [0] * 200001 def d(l, r): if l > r: return m = (l + r) // 2 a[m] = l - r, m d(l, m - 1) d(m + 1, r) for _ in range(int(input())): n = int(input()) d(1, n) c = 1 for i in sorted(a[1 : n + 1]): b[i[1]] = c c += 1 print(*b[1 : n + 1])
ASSIGN VAR BIN_OP LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def generate(l, n): if n <= 0: return if n == 1: d.append((l, 1, l)) return elif n % 2 == 1: d.append((l, n, l + (n - 1) // 2)) generate(l, (n - 1) // 2) generate(l + (n - 1) // 2 + 1, (n - 1) // 2) else: d.append((l, n, l + (n - 1) // 2)) generate(l, (n - 1) // 2) generate(l + n // 2, (n - 1) // 2 + 1) t = int(input()) cnt = 0 while cnt < t: cnt += 1 n = int(input()) a = [0] * n d = [] generate(0, n) d = sorted(d, key=lambda l: (l[1], -l[0]), reverse=True) i = 1 for elem in d: a[elem[2]] = i i += 1 print(" ".join(str(i) for i in a))
FUNC_DEF IF VAR NUMBER RETURN IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR RETURN IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for _ in range(int(input())): n = int(input()) d = {n: [0]} s, r = [n], [0] * n i = 1 while i <= n: s = sorted(s) n1 = s.pop() for j in sorted(d[n1]): start, stop = j, j + n1 if n1 % 2 == 0: mid = (start + stop - 1) // 2 else: mid = (start + stop) // 2 r[mid] = i i += 1 if mid - j in d: d[mid - j] += [j] else: d[mid - j] = [j] if stop - 1 - mid in d: d[stop - 1 - mid] += [mid + 1] else: d[stop - 1 - mid] = [mid + 1] if mid - j not in s: s += [mid - j] if stop - 1 - mid not in s: s += [stop - 1 - mid] del d[n1] print(*r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT VAR LIST NUMBER ASSIGN VAR VAR LIST VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR BIN_OP VAR VAR LIST VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR LIST BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) for tt in range(t): n = int(input()) a = [0] * n a.append(-1) p = 1 while p <= n: size = 0 i = -1 j = -1 bhul = False while True: i = j while a[i]: i += 1 if i == n + 1: bhul = True break if bhul: break j = i while a[j] == 0: j += 1 size = max(size, j - i) i = -1 j = -1 bhul = False while True: i = j while a[i]: i += 1 if i == n + 1: bhul = True break if bhul: break j = i while a[j] == 0: j += 1 if j - i == size: if size % 2 == 1: a[int((i + j) / 2)] = p p += 1 else: a[round((i + j) / 2) - 1] = p p += 1 a.pop() for x in a: print(x, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys input = sys.stdin.readline def build(l, r, a): if l > r: return if l == r: a[l] = 1 return m = (l + r) // 2 a[m] = r - l + 1 build(l, m - 1, a) build(m + 1, r, a) def solve(a): res = [] for ii in range(len(a)): res.append((a[ii], ii)) res.sort(key=lambda x: (-x[0], x[1])) val = 1 ans = [(0) for _ in range(len(res))] for el in res: ans[el[1]] = val val += 1 return " ".join(map(str, ans)) t = int(input()) for _ in range(t): n = int(input()) a = [(0) for _ in range(n)] build(0, n - 1, a) print(solve(a))
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR ASSIGN VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def recurse(start, end, log): if start > end: return else: mid = (start + end) // 2 log.append((start, mid, end)) recurse(mid + 1, end, log) recurse(start, mid - 1, log) for _ in range(int(input())): size = int(input()) memo = [] recurse(0, size - 1, memo) memo = sorted(memo, key=lambda x: (x[2] - x[0], -x[0]), reverse=True) ans = [0] * size for i in range(len(memo)): ans[memo[i][1]] = i + 1 print(*ans)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) answers = [] for _ in range(t): n = int(input()) a = [(0) for i in range(n)] store = [[]] store += [[] for i in range(n)] store[n].append((0, n - 1)) iter_num = 1 for i in range(n): if iter_num == n + 1: break num_zeros = n - i store[num_zeros].sort() for l, r in store[num_zeros]: if (r - l) % 2 == 0: center = int((r + l) / 2) else: center = int((r + l - 1) / 2) a[center] = iter_num iter_num += 1 if center - l > 0: store[center - l].append((l, center - 1)) if r - center > 0: store[r - center].append((center + 1, r)) a = [(str(i) + " ") for i in a] answers.append("".join(a)) print(*answers, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR STRING
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
i, j, mn = 1, 2, [] def ms(l, l1, r, x): global i, j if l1 <= r: m = (l1 + r) // 2 mn.append((l1, r)) l[m] = i i += 1 ms(l, l1, m - 1, 1) ms(l, m + 1, r, 0) return mn for _ in range(int(input())): i, mn, n = 1, [], int(input()) l = [0] * n a = ms(l, 0, n - 1, 1) a.sort(key=lambda x: -(x[1] - x[0])) for i in range(n): l[(a[i][0] + a[i][1]) // 2] = i + 1 print(" ".join(list(map(str, l))))
ASSIGN VAR VAR VAR NUMBER NUMBER LIST FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def solve(): def add_to_arr(L, R): length = R - L + 1 middle = (L + R) // 2 arr.append((-length, middle)) if L < middle: add_to_arr(L, middle - 1) if R > middle: add_to_arr(middle + 1, R) return N = getInt() A = [0] * N arr = [] add_to_arr(0, N - 1) arr.sort() for i in range(1, N + 1): A[arr[i - 1][1]] = i print(*A) return T = getInt() for t in range(T): solve()
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
tests = int(input()) for i in range(tests): n = int(input()) answer = [1] * n line = [[1, n, 1]] result = [] while n > 1: n = n // 2 stack = [] for j in range(len(line)): left = line[j][0] right = line[j][1] if right == left: stack.append([left, right, 0]) elif (right + left) % 2 == 0: if (right + left) // 2 - left == n: stack.append([left, (left + right) // 2 - 1, 1]) stack.append([(left + right) // 2 + 1, right, 1]) else: stack.append([left, (left + right) // 2 - 1, 0]) stack.append([(left + right) // 2 + 1, right, 0]) else: stack.append([left, (left + right) // 2 - 1, 0]) stack.append([(left + right) // 2 + 1, right, 1]) if line[j][2] == 1: result.append((right + left) // 2) for j in range(len(line)): if line[j][2] == 0: left = line[j][0] right = line[j][1] if left != right: result.append((right + left) // 2) if n == 1: for j in range(len(stack)): if stack[j][0] == stack[j][1]: result.append(stack[j][0]) line = stack for j in range(len(result)): answer[result[j] - 1] = j + 1 for j in range(len(answer)): print(answer[j], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST NUMBER VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys input = sys.stdin.readline def construct(A, count, MAX): B = [] NMAX = 0 for l, r in A: if r - l + 1 == MAX: ANS[(r + l) // 2] = count count += 1 if l <= (r + l) // 2 - 1: NMAX = max(NMAX, (r + l) // 2 - l) B.append((l, (r + l) // 2 - 1)) if r >= (r + l) // 2 + 1: NMAX = max(NMAX, r - (r + l) // 2) B.append(((r + l) // 2 + 1, r)) else: NMAX = max(NMAX, r - l + 1) B.append((l, r)) if NMAX != 0: construct(B, count, NMAX) t = int(input()) for tests in range(t): n = int(input()) ANS = [0] * n construct([(0, n - 1)], 1, n) print(*ANS)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR LIST NUMBER BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
li = [] def add_element(left, right): if right - left == 0: return if right - left == 1: li.append([-1, left]) return mid = (left + right) // 2 if (right - left) % 2 == 0: mid = mid - 1 li.append([-(right - left), mid]) add_element(left, mid) add_element(mid + 1, right) T = int(input()) for tc in range(T): n = int(input()) arr = [0] * n li = [] add_element(0, n) li.sort() val = 1 for i in range(n): arr[li[i][1]] = val val += 1 print(*arr)
ASSIGN VAR LIST FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER 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 BIN_OP LIST NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def solve(a, l, r): if r < l: return 0 mid = (l + r) // 2 a.append([mid, l, r]) solve(a, mid + 1, r) solve(a, l, mid - 1) def comp(x): return 200000 * (x[2] - x[1] + 1) + 100005 - x[1] for _ in range(int(input())): n = int(input()) ans = [] solve(ans, 0, n - 1) ans = sorted(ans, key=comp, reverse=True) a = [0] * n for i in range(len(ans)): a[ans[i][0]] = i + 1 print(*a)
FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def fun(a, b): if a > b: return mid = (a + b) // 2 temp.append((b - a, -mid)) fun(mid + 1, b) fun(a, mid - 1) for _ in range(int(input())): n = int(input()) Ans = [0] * (n + 1) temp = [] fun(1, n) temp.sort(reverse=True) t = 1 for i in temp: Ans[abs(i[1])] = t t += 1 print(*Ans[1:])
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def inverse_permutation(arr): inverse = [0] * len(arr) for i, (el, runlen) in enumerate(arr): inverse[el - 1] = i, runlen return inverse solve_dict = {(1): [(1, 1)], (2): [(1, 2), (2, 1)]} def solve(n): if n in solve_dict: return solve_dict[n] answer = [None] * n k = (n - 1) // 2 answer[k] = 1, n lip = inverse_permutation(solve(k)) rip = inverse_permutation(solve(n - k - 1)) left_i = 0 right_i = 0 for i in range(2, n + 1): left_run_length = lip[left_i][1] right_run_length = rip[right_i][1] if left_run_length >= right_run_length: answer[lip[left_i][0]] = i, lip[left_i][1] left_i += 1 if left_i == k: while right_i < n - k - 1: i += 1 answer[rip[right_i][0] + k + 1] = i, rip[right_i][1] right_i += 1 break else: answer[rip[right_i][0] + k + 1] = i, rip[right_i][1] right_i += 1 if right_i == n - k - 1: while left_i < k: i += 1 answer[lip[left_i][0]] = i, lip[left_i][1] left_i += 1 break solve_dict[n] = answer return answer t = int(input()) for _ in range(t): n = int(input()) answer = solve(n) res = "" for x in answer: res += str(x[0]) + " " res = res[:-1] print(res)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR DICT NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
def bins(l, r): global c, count if l > r: return mid = (l + r) // 2 c.append([r - l, -1 * l]) bins(l, mid - 1) bins(mid + 1, r) t = int(input()) for _ in range(t): count = 0 c = [] n = int(input()) a = [0] * n bins(0, n - 1) c.sort(reverse=True) for i in c: ans = i[0] // 2 - i[1] a[ans] = count + 1 count += 1 print(*a)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
for j in range(int(input())): h = 1 n = int(input()) A = [] for i in range(n): A.append(0) B = [[0, n - 1]] while len(B) > 0: for b in B: A[(b[0] + b[1]) // 2] = h h += 1 B = [] if A[-1] == 0: x = 0 for i in range(1, len(A)): if A[i] != 0 and A[i - 1] == 0: B.append([x, i - 1]) elif A[i] == 0 and A[i - 1] != 0: x = int(i) elif A[i] != 0: x = int(i) B.append([x, i]) m = -1 for b in B: if b[1] - b[0] > m: m = b[1] - b[0] B2 = [] for b in B: if b[1] - b[0] == m: B2.append(b) B = list(B2) print(*A)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST LIST NUMBER BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
import sys def answer(n): widths = [[] for _ in range(n + 1)] ans = [(0) for _ in range(n)] widths[n].append(0) ctr = 1 for w in range(n, 0, -1): ls = widths[w] ls.sort() for l in ls: mid = (l + (l + w - 1)) // 2 ans[mid] = ctr ctr += 1 if w > 1: lw = mid - 1 - l + 1 widths[lw].append(l) rw = l + w - (mid + 1) widths[rw].append(mid + 1) return ans def main(): t = int(sys.stdin.readline()) while t: n = int(sys.stdin.readline()) print(*answer(n)) t -= 1 return main()
IMPORT FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN EXPR FUNC_CALL VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
t = int(input()) def d(l, r): if l > r: return m = (l + r) // 2 a[m] = l - r, m d(l, m - 1) d(m + 1, r) for _ in range(t): n = int(input()) a = [0] * n b = [0] * n d(0, n - 1) for i, j in enumerate(sorted(a)): b[j[1]] = i + 1 print(*b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a$ of length $n$ consisting of zeros. You perform $n$ actions with this array: during the $i$-th action, the following sequence of operations appears: Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; Let this segment be $[l; r]$. If $r-l+1$ is odd (not divisible by $2$) then assign (set) $a[\frac{l+r}{2}] := i$ (where $i$ is the number of the current action), otherwise (if $r-l+1$ is even) assign (set) $a[\frac{l+r-1}{2}] := i$. Consider the array $a$ of length $5$ (initially $a=[0, 0, 0, 0, 0]$). Then it changes as follows: Firstly, we choose the segment $[1; 5]$ and assign $a[3] := 1$, so $a$ becomes $[0, 0, 1, 0, 0]$; then we choose the segment $[1; 2]$ and assign $a[1] := 2$, so $a$ becomes $[2, 0, 1, 0, 0]$; then we choose the segment $[4; 5]$ and assign $a[4] := 3$, so $a$ becomes $[2, 0, 1, 3, 0]$; then we choose the segment $[2; 2]$ and assign $a[2] := 4$, so $a$ becomes $[2, 4, 1, 3, 0]$; and at last we choose the segment $[5; 5]$ and assign $a[5] := 5$, so $a$ becomes $[2, 4, 1, 3, 5]$. Your task is to find the array $a$ of length $n$ after performing all $n$ actions. Note that the answer exists and unique. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. The only line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer — the array $a$ of length $n$ after performing $n$ actions described in the problem statement. Note that the answer exists and unique. -----Example----- Input 6 1 2 3 4 5 6 Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
d = dict() def seive(s, e): if s >= e: return mid = (s + e) // 2 d[s, mid - 1] = min(mid - s, e - mid) d[mid + 1, e] = max(mid - s, e - mid) seive(s, mid - 1) seive(mid + 1, e) for i in range(int(input())): n = int(input()) d[1, n] = n seive(1, n) l = [0] * n j = 1 for i in sorted(d, key=d.get, reverse=True): if j > n: break mid = (i[0] + i[1]) // 2 l[mid - 1] = j j += 1 for i in l: print(i, end=" ") print("") d.clear()
ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
import sys for _ in range(int(sys.stdin.readline())): n = int(sys.stdin.readline()) a = [int(i) for i in sys.stdin.readline().split()][:n] a.sort() print(a[int(n / 2)] - a[int(n / 2 - 1)])
IMPORT 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 VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) i = sorted(a) print(i[n // 2] - i[n // 2 - 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 VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() if n % 2 == 1: print(abs(a[(n - 1) // 2] - a[(n - 1) // 2 - 1])) elif n % 4 == 0: print(abs(a[n // 2 - 1] - a[n // 2 - 2])) else: print(abs(a[n // 2] - a[n // 2 - 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 VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for j in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() b = (len(a) + 1) // 2 c = abs(a[b - 1] - a[b - 2]) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() mid = n // 2 print(abs(arr[mid] - arr[mid - 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 VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for i in range(t): a = int(input()) l = list(map(int, input().split())) new_list = sorted(l) middle = a // 2 middle_left = middle - 1 ans = new_list[middle] - new_list[middle_left] 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) lis.sort() res = lis[int(n / 2)] - lis[int(n / 2) - 1] print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for test in range(t): n = int(input()) l = list(map(int, input().split())) l.sort() l1 = [] l2 = [] c1 = 0 c2 = 0 for i in range(n): if i % 2 == 0: l1.append(l[i]) c1 += 1 else: l2.append(l[i]) c2 += 1 a = (c1 - 1) // 2 b = (c2 - 1) // 2 print(abs(l1[a] - l2[b]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() f = (n + 1) // 2 if f % 2 == 0: print(abs(arr[f - 1] - arr[f - 2])) else: print(abs(arr[f - 1] - arr[f - 2]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) arr.sort() p, q = [], [] flag = True for i in arr: if flag: p.append(i) else: q.append(i) flag ^= True p1 = len(p) q1 = len(q) ans = abs(p[(p1 + 1) // 2 - 1] - q[(q1 + 1) // 2 - 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) list1 = list(map(int, input().split())) list1.sort() list2 = [] list3 = [] for i in range(n): if i % 2 == 0: list2.append(list1[i]) else: list3.append(list1[i]) a = len(list2) b = len(list3) if a % 2 == 0: med1 = list2[(a + 1) // 2 - 1] else: med1 = list2[a // 2] if b % 2 == 0: med2 = list3[(b + 1) // 2 - 1] else: med2 = list3[b // 2] print(abs(med1 - med2))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split(" "))) a.sort() x = (n - 1) // 2 - 1 print(a[x + 1] - a[x])
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 STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for i in range(t): n = int(input()) a = [int(x) for x in input().split()] a.sort() print(a[n // 2] - a[n // 2 - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() b = len(a) k = b // 2 j = b // 2 - 1 print(a[k] - a[j])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) while t > 0: n = int(input()) li = list(map(int, input().strip().split())) li.sort() ans = li[n // 2] - li[n // 2 - 1] print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for tc in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() a1 = [char for char in a[::2]] a2 = [cahr for cahr in a[1::2]] result = abs(a1[(len(a1) + 1) // 2 - 1] - a2[(len(a2) + 1) // 2 - 1]) print(result)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
pi = int(input()) while pi != 0: n = int(input()) l = list(input().split()) for i in range(len(l)): l[i] = int(l[i]) l.sort() print(abs(l[len(l) // 2] - l[len(l) // 2 - 1])) pi -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
def mgc(arr, n): arr.sort() return abs(arr[(n - 3) // 2] - arr[(n - 3) // 2 + 1]) return min(temp[1] - temp[0], temp[2] - temp[1]) for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) print(mgc(arr, n))
FUNC_DEF EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for ih in range(int(input())): n = int(input()) l = list(map(int, input().split())) l.sort() ans = 10**9 + 7 mid = l[n // 2] for i in range(n // 2): ans = min(ans, abs(mid - l[i])) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
t = int(input()) while t: t -= 1 n = int(input()) l = [int(x) for x in input().split()] l = l[:n] l.sort() print(abs(l[n // 2] - l[n // 2 - 1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for _ in range(int(input())): n = int(input()) nli = list(map(int, input().split())) nli.sort() x = (n - 1) // 2 y = (n - 1) // 2 - 1 mn = abs(nli[x] - nli[y]) print(mn)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
n = int(input()) while n: l = int(input()) arr1 = [int(l) for l in input().split()] arr = sorted(arr1) div = (len(arr) + 1) // 2 mid = div - 1 ans = abs(arr[mid] - arr[mid - 1]) print(ans) n -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
for s in [*open(0)][2::2]: arr = sorted(map(int, s.split())) x = len(arr) // 2 print(arr[x] - arr[x - 1] if len(arr) % 2 else arr[x + 1] - arr[x])
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR
JJ loves playing with medians. He has an array A of length N (N is odd). He wants to partition the array A into two non-empty subsets P and Q such that the value of |median(P) - median(Q)| is as small as possible. (Note that each A_{i} must belong to either subset P or subset Q). Help him find this minimum value of |median(P) - median(Q)|. As a reminder, the median of a subset X of size N is the element which occupies the \lfloor\frac{N+1}{2}\rfloor^{th} position after we sort the elements in non-decreasing order. For example, median([3, 1, 4]) = 3, median([3, 1, 4, 2]) = 2. (Here \lfloor x \rfloor denotes the largest integer which does not exceed x). ------ Input Format ------ - The first line contains T - the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N - the size of the array A. - The second line of each test case contains N space separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ Output the minimum value of |median(P) - median(Q)|. ------ Constraints ------ $1 ≤ T ≤ 100$ $3 ≤ N ≤ 999$ $1 ≤ A_{i} ≤ 10^{9}$ $N$ is odd. ----- Sample Input 1 ------ 3 5 2 7 4 8 2 3 1 2 3 5 1 1 1 1 1 ----- Sample Output 1 ------ 2 1 0 ----- explanation 1 ------ - Test case $1$: We can divide the array into following two subsets: $P = [2, 8], Q = [7, 4, 2]$, which has $|median(P) - median(Q)| = |2 - 4| = 2$. It can be proven that the answer can not be improved further. - Test case $2$: We can divide the array into following two subsets: $P = [1, 3], Q = [2]$, which has $|median(P) - median(Q)| = |1 - 2| = 1$. It can be proven that the answer can not be improved further. - Test case $3$: Observe that, for any partition of the array into any two non-empty subsets, the median of both the subsets will be $1$. Therefore the answer is $|1 - 1| = 0$.
try: def solve(nums): nums.sort() return nums[len(nums) // 2] - nums[len(nums) // 2 - 1] t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) print(solve(l)) except: pass
FUNC_DEF EXPR FUNC_CALL VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def check(self, divisor, nums, threshold): result = 0 for number in nums: k = int(number / divisor) if number % divisor != 0: k += 1 result += k if result > threshold: return False return True def smallestDivisor(self, nums: List[int], threshold: int) -> int: low = 1 high = max(nums) while low <= high: mid = int((low + high) / 2) if self.check(mid, nums, threshold): high = mid - 1 else: low = mid + 1 return low
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: l = 1 r = 2**31 - 1 calc = lambda cand: sum(ceil(x / cand) for x in nums) while l + 1 < r: mid = l + (r - l) // 2 if calc(mid) > threshold: l = mid else: r = mid if calc(l) <= threshold: return l else: return r
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: i = 1 j = max(nums) m = (i + j) // 2 ans = 0 while i <= j: total = 0 for num in nums: total += (num + m - 1) // m if total > threshold: i = m + 1 elif total <= threshold: j = m - 1 ans = m m = (i + j) // 2 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5). It is guaranteed that there will be an answer.   Example 1: Input: nums = [1,2,5,9], threshold = 6 Output: 5 Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1. If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). Example 2: Input: nums = [2,3,5,7,11], threshold = 11 Output: 3 Example 3: Input: nums = [19], threshold = 5 Output: 4   Constraints: 1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= 10^6 nums.length <= threshold <= 10^6
class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: lo, hi = 1, max(nums) while lo <= hi: mid = (lo + hi) // 2 if sum([math.ceil(i / mid) for i in nums]) <= threshold: hi = mid - 1 else: lo = mid + 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR