description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
def binarySearch(arr, elem): a = 0 b = len(arr) - 1 i = -1 while a <= b: mid = (a + b) // 2 element = arr[mid] if element == elem: i = mid if element >= elem: b = mid - 1 else: a = mid + 1 return i class Solution: def getMinDiff(self, arr, n, k): heights = [(0) for i in range(2 * n)] for i in range(n): height = arr[i] heights[i] = height + k heights[i + n] = height - k heights.sort() i = 0 j = binarySearch(heights, heights[-1] - 2 * k) minSumDiff = -1 leftElemMax = heights[0] + 2 * k while i < 2 * n and heights[i] <= leftElemMax: diff = heights[j] - heights[i] if minSumDiff == -1 or diff < minSumDiff: minSumDiff = diff leftH = heights[i] while heights[i] == leftH: i += 1 if i == 2 * n: break while heights[j] < leftH + 2 * k: j += 1 if j == 2 * n: break return minSumDiff
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR WHILE VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR WHILE VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[-1] - arr[0] smallest = arr[0] + k largest = arr[-1] - k mi, ma = float("inf"), float("-inf") for i in range(n - 1): mi = min(smallest, arr[i + 1] - k) ma = max(largest, arr[i] + k) ans = min(ans, ma - mi) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() mn = arr[0] mx = arr[n - 1] diff = mx - mn for i in range(1, n): mn = min(arr[0] + k, arr[i] - k) mx = max(arr[-1] - k, arr[i - 1] + k) diff = min(diff, mx - mn) return diff
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): point = arr point.sort() currentMin = point[0] currentMax = point[-1] minSum = abs(currentMax - currentMin) for i in range(1, n): currentMin = min(point[0] + k, point[i] - k) currentMax = max(point[i - 1] + k, point[-1] - k) minSum = min(minSum, abs(currentMax - currentMin)) return minSum
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): t = sorted(arr) max1 = t[n - 1] min1 = t[0] res = max1 - min1 for i in range(n): max1 = max(t[i - 1] + k, t[n - 1] - k) min1 = min(t[i] - k, t[0] + k) res = min(res, max1 - min1) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[n - 1] - arr[0] for i in range(1, n): ans = min( ans, max(arr[i - 1] + k, arr[n - 1] - k) - min(arr[0] + k, arr[i] - k) ) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() n = len(arr) var1 = max(arr) var2 = min(arr) result = var1 - var2 for i in range(1, len(arr)): maxi = max(arr[n - 1] - k, arr[i - 1] + k) mini = min(arr[0] + k, arr[i] - k) result = min(result, maxi - mini) return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): d = [] mapped = [(0) for i in range(n)] c = 0 for i in range(n): d.append((arr[i] + k, i)) d.append((arr[i] - k, i)) d.sort(key=lambda x: x[0]) j = 0 ans = 1e100 for i in range(n): while c < n and j < 2 * n: if mapped[d[j][1]] == 0: c += 1 mapped[d[j][1]] += 1 j += 1 if c == n: ans = min(ans, d[j - 1][0] - d[i][0]) if mapped[d[i][1]] == 1: c -= 1 mapped[d[i][1]] -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): if n == 1: return 0 arr.sort() prevDiff = abs(arr[0] - arr[n - 1]) for i in range(1, n): maxi = max(arr[i - 1] + k, arr[n - 1] - k) mini = min(arr[0] + k, arr[i] - k) prevDiff = min(prevDiff, maxi - mini) return prevDiff
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() min_diff = arr[-1] - arr[0] for i in range(n - 1): min_tower = min(arr[0] + k, arr[i + 1] - k) max_tower = max(arr[i] + k, arr[-1] - k) diff = max_tower - min_tower min_diff = min(min_diff, diff) min_tower = min(arr[i + 1] - k, arr[0] + k) max_tower = max(arr[i] + k, arr[-1] - k) diff = max_tower - min_tower min_diff = min(min_diff, diff) return min_diff
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[-1] - arr[0] tmax = arr[-1] tmin = arr[0] for x in range(1, n): tmax = max(arr[-1] - k, arr[x - 1] + k) tmin = min(arr[0] + k, arr[x] - k) ans = min(ans, tmax - tmin) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[n - 1] - arr[0] small = arr[0] + k large = arr[n - 1] - k for i in range(n - 1): mi = min(small, arr[i + 1] - k) ma = max(large, arr[i] + k) if small < 0: continue ans = min(ans, ma - mi) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): v = [] taken = [0] * n for i in range(n): v.append([arr[i] - k, i]) v.append([arr[i] + k, i]) v.sort() elements_in_range = 0 left = right = 0 while elements_in_range < n and right < len(v): if taken[v[right][1]] == 0: elements_in_range += 1 taken[v[right][1]] += 1 right += 1 ans = v[right - 1][0] - v[left][0] while right < len(v): if taken[v[left][1]] == 1: elements_in_range -= 1 taken[v[left][1]] -= 1 left += 1 while elements_in_range < n and right < len(v): if taken[v[right][1]] == 0: elements_in_range += 1 taken[v[right][1]] += 1 right += 1 if elements_in_range == n: ans = min(ans, v[right - 1][0] - v[left][0]) else: break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): aux = [] for i in range(n): aux.append([arr[i] - k, i]) aux.append([arr[i] + k, i]) aux.sort(key=lambda x: x[0]) counts = [(0) for i in range(n)] partial_min = 1e100 j = 0 index_count = 0 for i in range(0, 2 * n): while index_count < n and j < 2 * n: counts[aux[j][1]] += 1 if counts[aux[j][1]] == 1: index_count += 1 j += 1 if index_count == n: partial_min = min(partial_min, aux[j - 1][0] - aux[i][0]) counts[aux[i][1]] -= 1 if counts[aux[i][1]] == 0: index_count -= 1 return partial_min
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR WHILE VAR VAR VAR BIN_OP NUMBER VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: Assume that height of the tower can be negative. A slight modification of the problem can be found here. Example 1: Input: K = 2, N = 4 Arr[] = {1, 5, 8, 10} Output: 5 Explanation: The array can be modified as {3, 3, 6, 8}. The difference between the largest and the smallest is 8-3 = 5. Example 2: Input: K = 3, N = 5 Arr[] = {3, 9, 12, 16, 20} Output: 11 Explanation: The array can be modified as {6, 12, 9, 13, 17}. The difference between the largest and the smallest is 17-6 = 11. Your Task: You don't need to read input or print anything. Your task is to complete the function getMinDiff() which takes the arr[], n and k as input parameters and returns an integer denoting the minimum difference. Expected Time Complexity: O(N*logN) Expected Auxiliary Space: O(N) Constraints 1 ≤ K ≤ 10^{4} 1 ≤ N ≤ 10^{5} 1 ≤ Arr[i] ≤ 10^{5}
class Solution: def getMinDiff(self, arr, n, k): arr.sort() i = 0 ans = 1000000000.0 while i < n: mn = min([arr[0], arr[i] - k]) if i > 0: mx = max([arr[n - 1] - k, arr[i - 1]]) else: mx = arr[n - 1] - k ans = min([ans, mx - mn]) arr[i] += k i += 1 ans = min([ans, arr[n - 1] - arr[0]]) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, k = map(int, input().split()) dic = dict() for _ in range(k): u, v, d = map(int, input().split()) if u not in dic: dic[u] = -d else: dic[u] -= d if v not in dic: dic[v] = d else: dic[v] += d dic_neg = dict() dic_pos = dict() for key, value in dic.items(): if value > 0: dic_pos[key] = value elif value < 0: dic_neg[key] = value li_pos = sorted(dic_pos.items(), key=lambda kv: [kv[1], kv[0]], reverse=True) li_neg = sorted(dic_neg.items(), key=lambda kv: [kv[1], kv[0]]) li_pos = [[key, val] for key, val in li_pos] li_neg = [[key, val] for key, val in li_neg] pos_ind, neg_ind = 0, 0 li = [] while pos_ind != len(li_pos): if li_pos[pos_ind][1] >= abs(li_neg[neg_ind][1]): li.append([li_neg[neg_ind][0], li_pos[pos_ind][0], abs(li_neg[neg_ind][1])]) li_pos[pos_ind][1] += li_neg[neg_ind][1] li_neg[neg_ind][1] = 0 else: li.append([li_neg[neg_ind][0], li_pos[pos_ind][0], li_pos[pos_ind][1]]) li_neg[neg_ind][1] += li_pos[pos_ind][1] li_pos[pos_ind][1] = 0 if li_neg[neg_ind][1] == 0: neg_ind += 1 if li_pos[pos_ind][1] == 0: pos_ind += 1 print(len(li)) for i in li: print(*i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
from sys import stdin def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) def main(): n, m = lin() dept = [0] * (n + 1) for _ in range(m): v, u, d = lin() dept[v] += d dept[u] -= d ps = [] mn = [] for i in range(1, n + 1): if dept[i] > 0: ps.append([dept[i], i]) elif dept[i] < 0: mn.append([-dept[i], i]) i, j = 0, 0 l1, l2 = len(ps), len(mn) ans = [] while i < l1 and j < l2: if ps[i][0] > mn[j][0]: ps[i][0] -= mn[j][0] ans.append([ps[i][1], mn[j][1], mn[j][0]]) j += 1 elif ps[i][0] == mn[j][0]: ans.append([ps[i][1], mn[j][1], mn[j][0]]) j += 1 i += 1 else: mn[j][0] -= ps[i][0] ans.append([ps[i][1], mn[j][1], ps[i][0]]) i += 1 print(len(ans)) for i in ans: print(*i) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, m = map(int, input().split()) balance = [0] * n for _ in range(m): u, v, w = map(int, input().split()) u -= 1 v -= 1 balance[u] -= w balance[v] += w i = 0 j = 0 ans = [] while i < n: if balance[i] < 0: while balance[j] <= 0: j += 1 delta = min(-balance[i], balance[j]) balance[i] += delta balance[j] -= delta ans.append((i + 1, j + 1, delta)) else: i += 1 print(len(ans)) for edge in ans: print(*edge)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def comp(a, b): if a > b: return 2 return 2 if a == b else 0 def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print("Case #" + str(tc) + ":", ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): a[i], a[j] = a[j], a[i] def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord("a") def graph(n, m): for i in range(m): x, y = mi() a[x].append(y) a[y].append(x) t = 1 uu = t while t > 0: t -= 1 n, m = mi() debt = [0] * (n + 1) for i in range(m): x, y, w = mi() debt[x] += w debt[y] -= w p = [] m = [] for i in range(1, n + 1): if debt[i] > 0: p.append([debt[i], i]) else: m.append([-debt[i], i]) ans = [] i = j = 0 n = len(p) mm = len(m) while i < n and j < mm: mini = min(p[i][0], m[j][0]) if mini: ans.append([p[i][1], m[j][1], mini]) p[i][0] -= mini m[j][0] -= mini if p[i][0] == 0: i += 1 if m[j][0] == 0: j += 1 print(len(ans)) for i in ans: print(*i)
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
def f(): n, m = [int(s) for s in input().split()] bank = [0] * (n + 1) for i in range(m): u, v, d = [int(s) for s in input().split()] bank[u] += d bank[v] -= d borrow = [] lend = [] for i in range(1, n + 1): if bank[i] > 0: borrow.append([i, bank[i]]) if bank[i] < 0: lend.append([i, -bank[i]]) memo = [] ind = 0 for lender, money in lend: while borrow[ind][1] < money: memo.append((borrow[ind][0], lender, borrow[ind][1])) money -= borrow[ind][1] ind += 1 if borrow[ind][1] == money: memo.append((borrow[ind][0], lender, money)) ind += 1 else: memo.append((borrow[ind][0], lender, money)) borrow[ind][1] -= money print(len(memo)) for u, v, d in memo: print("{} {} {}".format(u, v, d)) f()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR EXPR FUNC_CALL VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys def main(): n, m = readIntArr() balance = [[0, 0, i] for i in range(n + 1)] for _ in range(m): u, v, d = readIntArr() balance[u][1] += d balance[v][0] += d balance.pop(0) for i in range(n): delta = min(balance[i][0], balance[i][1]) balance[i][0] -= delta balance[i][1] -= delta balance.sort(key=lambda x: x[1] - x[0]) ans = [] l = 0 r = n - 1 while l <= r: lIn, lOut, lI = balance[l] rIn, rOut, rI = balance[r] if lIn == 0: l += 1 continue if rOut == 0: r -= 1 continue delta = min(lIn, rOut) balance[l][0] -= delta balance[r][1] -= delta ans.append([rI, lI, delta]) print(len(ans)) multiLineArrayOfArraysPrint(ans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, m = map(int, input().split()) bal = [0] * n for i in range(m): x, y, z = map(int, input().split()) bal[x - 1] -= z bal[y - 1] += z owe = [] for i in range(n): if bal[i] < 0: owe.append([i, -bal[i]]) ans = [] for i in range(n): while bal[i] > 0 and owe: val = min(bal[i], owe[0][1]) ans.append([owe[0][0] + 1, i + 1, val]) bal[i] -= val owe[0][1] -= val if owe[0][1] == 0: owe.pop(0) print(len(ans)) for i in range(len(ans)): print(" ".join(map(str, ans[i])))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER VAR IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [0] * N for _ in range(M): a, b, d = map(int, input().split()) A[a - 1] += d A[b - 1] -= d Plus = [] Minus = [] for i in range(N): if A[i] > 0: Plus.append((i, A[i])) if A[i] < 0: Minus.append((i, -A[i])) ans = [] ind = 0 for ip, pd in Plus: while pd > 0 and ind < len(Minus): im, md = Minus[ind] if pd >= md: ans.append((ip, im, md)) ind += 1 pd -= md else: ans.append((ip, im, pd)) Minus[ind] = im, md - pd pd = 0 print(len(ans)) for p, m, d in ans: print(p + 1, m + 1, d)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys N, M = map(int, input().split()) money = [0] * N for u, v, d in (map(int, l.split()) for l in sys.stdin): money[u - 1] -= d money[v - 1] += d plus, minus = [], [] for i, m in enumerate(money): if m > 0: plus.append([i, m]) elif m < 0: minus.append([i, -m]) ans = [] for i, m in minus: while m > 0: x = m if m < plus[-1][1] else plus[-1][1] ans.append((i + 1, plus[-1][0] + 1, x)) m -= x if plus[-1][1] == x: plus.pop() else: plus[-1][1] -= x print(len(ans)) for l in ans: print(*l)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n, m = [int(x) for x in input().split()] dp = [0] * (n + 1) for i in range(m): x, y, z = [int(x) for x in input().split()] if x == y: continue dp[x] -= z dp[y] += z delta = [] for i in range(n + 1): delta.append([dp[i], i]) delta.sort() i = 0 j = n res = [] while i < j: minn = min(abs(delta[i][0]), abs(delta[j][0])) delta[i][0] += minn delta[j][0] -= minn if minn > 0: gamma = [delta[i][1], delta[j][1], minn] res.append(gamma) if delta[i][0] == 0: i += 1 if delta[j][0] == 0: j -= 1 print(len(res)) for i in range(len(res)): delta = res[i] for j in range(len(delta)): print(delta[j], end=" ") print("")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) D = [0] * (n + 1) for i in range(m): x, y, z = map(int, input().split()) D[x] += z D[y] -= z PLUS = [] MINUS = [] for i in range(n + 1): if D[i] > 0: PLUS.append([D[i], i]) elif D[i] < 0: MINUS.append([-D[i], i]) Pind = 0 Mind = 0 PMAX = len(PLUS) MMAX = len(MINUS) ANS = [] while Pind < PMAX and Mind < MMAX: if PLUS[Pind][0] > MINUS[Mind][0]: PLUS[Pind][0] -= MINUS[Mind][0] ANS.append((PLUS[Pind][1], MINUS[Mind][1], MINUS[Mind][0])) Mind += 1 elif PLUS[Pind][0] == MINUS[Mind][0]: ANS.append((PLUS[Pind][1], MINUS[Mind][1], MINUS[Mind][0])) Mind += 1 Pind += 1 else: MINUS[Mind][0] -= PLUS[Pind][0] ANS.append((PLUS[Pind][1], MINUS[Mind][1], PLUS[Pind][0])) Pind += 1 print(len(ANS)) for x, y, z in ANS: print(x, y, z)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) info = [list(map(int, input().split())) for i in range(m)] val = [0] * n for i in range(m): a, b, cost = info[i] a -= 1 b -= 1 val[a] -= cost val[b] += cost minus = [] plus = [] for i in range(n): if val[i] < 0: minus.append([i, -val[i]]) if val[i] > 0: plus.append([i, val[i]]) ans = [] num1 = 0 for num2, cost in minus: a = num2 + 1 value = cost while True: if value == 0: break if num1 >= len(plus): break if plus[num1][1] <= value: ans.append((a, plus[num1][0] + 1, plus[num1][1])) value -= plus[num1][1] num1 += 1 else: ans.append((a, plus[num1][0] + 1, value)) plus[num1][1] -= value break print(len(ans)) for i in ans: print(*i)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys input = sys.stdin.readline l = input().split() n = int(l[0]) m = int(l[1]) owe = [(0) for i in range(n)] for i in range(m): l = input().split() u = int(l[0]) v = int(l[1]) d = int(l[2]) u -= 1 v -= 1 owe[u] += d owe[v] -= d owers = [] loan = [] for i in range(n): if owe[i] > 0: owers.append([i, owe[i]]) elif owe[i] < 0: loan.append([i, -owe[i]]) z = len(owers) y = len(loan) currz = 0 curry = 0 l = [] while currz != z: if owers[currz][1] <= loan[curry][1]: l.append((owers[currz][0], loan[curry][0], owers[currz][1])) loan[curry][1] -= owers[currz][1] if loan[curry][1] == 0: curry += 1 currz += 1 else: l.append((owers[currz][0], loan[curry][0], loan[curry][1])) owers[currz][1] -= loan[curry][1] if owers[currz][1] == 0: currz += 1 curry += 1 print(len(l)) for i in l: print(i[0] + 1, i[1] + 1, i[2])
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, m = map(int, input().split()) A = [0] * (n + 1) P = [] N = [] for i in range(m): u, v, d = map(int, input().split()) A[u] -= d A[v] += d for i in range(1, n + 1): if A[i] > 0: P.append([A[i], i]) if A[i] < 0: N.append([A[i], i]) i = j = 0 R = [] while i < len(P): r = min(P[i][0], -1 * N[j][0]) R.append([N[j][1], P[i][1], r]) if r == -1 * N[j][0] == P[i][0]: N[j][0] = 0 P[i][0] = 0 i += 1 j += 1 elif r == P[i][0]: N[j][0] += r i += 1 else: P[i][0] -= r j += 1 print(len(R)) for i in range(len(R)): print(R[i][0], R[i][1], R[i][2])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, m = map(int, input().split()) lst = [0] * (n + 1) for i in range(m): u, v, d = map(int, input().split()) lst[u] -= d lst[v] += d dena = [] dene_wala = [] lena = [] lene_wala = [] for j in range(len(lst)): if lst[j] < 0: dena.append(abs(lst[j])) dene_wala.append(j) elif lst[j] > 0: lena.append(abs(lst[j])) lene_wala.append(j) l = [] i, j = 0, 0 while i < len(dena): while j < len(lena): l.append(f"{dene_wala[i]} {lene_wala[j]} {min(dena[i], lena[j])}") a = min(dena[i], lena[j]) dena[i] -= a lena[j] -= a if lena[j] <= 0: j += 1 if dena[i] <= 0: break if dena[i] <= 0: i += 1 print(len(l)) for k in l: print(k)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING VAR VAR STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
n, m = map(int, input().split()) d = {} for i in range(m): x, y, a = map(int, input().split()) d[x] = d.get(x, 0) - a d[y] = d.get(y, 0) + a pos = [] neg = [] for i in d: if d[i] < 0: neg.append([d[i], i]) elif d[i] > 0: pos.append([d[i], i]) ans = [] i = 0 j = 0 while i < len(neg) and j < len(pos): z = min(-neg[i][0], pos[j][0]) ans.append([neg[i][1], pos[j][1], z]) neg[i][0] += z pos[j][0] -= z if pos[j][0] == 0: j += 1 if neg[i][0] == 0: i += 1 print(len(ans)) for i in ans: print(*i)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,b)$ denote the debt of $a$ towards $b$, or $0$ if there is no such debt. Sometimes, this becomes very complex, as the person lending money can run into financial troubles before his debtor is able to repay his debt, and finds himself in the need of borrowing money. When this process runs for a long enough time, it might happen that there are so many debts that they can be consolidated. There are two ways this can be done: Let $d(a,b) > 0$ and $d(c,d) > 0$ such that $a \neq c$ or $b \neq d$. We can decrease the $d(a,b)$ and $d(c,d)$ by $z$ and increase $d(c,b)$ and $d(a,d)$ by $z$, where $0 < z \leq \min(d(a,b),d(c,d))$. Let $d(a,a) > 0$. We can set $d(a,a)$ to $0$. The total debt is defined as the sum of all debts: $$\Sigma_d = \sum_{a,b} d(a,b)$$ Your goal is to use the above rules in any order any number of times, to make the total debt as small as possible. Note that you don't have to minimise the number of non-zero debts, only the total debt. -----Input----- The first line contains two space separated integers $n$ ($1 \leq n \leq 10^5$) and $m$ ($0 \leq m \leq 3\cdot 10^5$), representing the number of people and the number of debts, respectively. $m$ lines follow, each of which contains three space separated integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), $d_i$ ($1 \leq d_i \leq 10^9$), meaning that the person $u_i$ borrowed $d_i$ burles from person $v_i$. -----Output----- On the first line print an integer $m'$ ($0 \leq m' \leq 3\cdot 10^5$), representing the number of debts after the consolidation. It can be shown that an answer always exists with this additional constraint. After that print $m'$ lines, $i$-th of which contains three space separated integers $u_i, v_i, d_i$, meaning that the person $u_i$ owes the person $v_i$ exactly $d_i$ burles. The output must satisfy $1 \leq u_i, v_i \leq n$, $u_i \neq v_i$ and $0 < d_i \leq 10^{18}$. For each pair $i \neq j$, it should hold that $u_i \neq u_j$ or $v_i \neq v_j$. In other words, each pair of people can be included at most once in the output. -----Examples----- Input 3 2 1 2 10 2 3 5 Output 2 1 2 5 1 3 5 Input 3 3 1 2 10 2 3 15 3 1 10 Output 1 2 3 5 Input 4 2 1 2 12 3 4 8 Output 2 1 2 12 3 4 8 Input 3 4 2 3 1 2 3 2 2 3 4 2 3 8 Output 1 2 3 15 -----Note----- In the first example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 2$, $d = 3$ and $z = 5$. The resulting debts are: $d(1, 2) = 5$, $d(2, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(1, 2) = 5$, $d(1, 3) = 5$, all other debts are $0$. In the second example the optimal sequence of operations can be the following: Perform an operation of the first type with $a = 1$, $b = 2$, $c = 3$, $d = 1$ and $z = 10$. The resulting debts are: $d(3, 2) = 10$, $d(2, 3) = 15$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the first type with $a = 2$, $b = 3$, $c = 3$, $d = 2$ and $z = 10$. The resulting debts are: $d(2, 2) = 10$, $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 2$. The resulting debts are: $d(3, 3) = 10$, $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 3$. The resulting debts are: $d(2, 3) = 5$, $d(1, 1) = 10$, all other debts are $0$; Perform an operation of the second type with $a = 1$. The resulting debts are: $d(2, 3) = 5$, all other debts are $0$.
import sys input = sys.stdin.readline inf = 100000000000000000 mod = 998244353 n, m = map(int, input().split()) B = [0] * n JIE = [] HUAN = [] for i in range(m): x, y, z = map(int, input().split()) x -= 1 y -= 1 B[x] += z B[y] -= z for i in range(n): if B[i] > 0: JIE.append(i) elif B[i] < 0: HUAN.append(i) for i in range(len(B)): B[i] = abs(B[i]) poshuan = 0 posjie = 0 ANS = [] while posjie < len(JIE) and poshuan < len(HUAN): if B[JIE[posjie]] < B[HUAN[poshuan]]: ANS.append((JIE[posjie] + 1, HUAN[poshuan] + 1, B[JIE[posjie]])) B[HUAN[poshuan]] -= B[JIE[posjie]] posjie += 1 elif B[JIE[posjie]] > B[HUAN[poshuan]]: ANS.append((JIE[posjie] + 1, HUAN[poshuan] + 1, B[HUAN[poshuan]])) B[JIE[posjie]] -= B[HUAN[poshuan]] poshuan += 1 else: ANS.append((JIE[posjie] + 1, HUAN[poshuan] + 1, B[HUAN[poshuan]])) poshuan += 1 posjie += 1 print(len(ANS)) for aa, bb, cc in ANS: print(aa, bb, cc)
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
from builtins import map def main(): for t in range(int(input())): n = int(input()) ans = [(0) for i in range(n)] a = list(map(lambda ai: int(ai), input().split(" "))) k = sum(a) // n lb = n - k for i in range(1, k + 1): a[n - i] -= i for i in range(n - 1, -1, -1): if a[i] == i and k > 0: k -= 1 ans[i] = 1 else: ans[i] = 0 lb -= 1 if lb >= 0: a[lb] -= k for i in range(n): print(ans[i], end=" ") print(end="\n") main()
FUNC_DEF 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 FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
def solve(bits): first_one_encountered = False depth = 0 next_zero = set() answer = [] for i, sum in enumerate(bits): if sum == 0: answer.append(0) elif not first_one_encountered: answer.append(1) first_one_encountered = True depth = sum next_zero.add(depth) elif i in next_zero: answer.append(0) depth = i + sum next_zero.add(depth) else: answer.append(1) depth = sum next_zero.add(depth) return " ".join(list(map(str, answer))) t = int(input()) for _ in range(t): n = int(input()) bits = list(map(int, input().split())) print(solve(bits))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
for i in range(int(input())): n = int(input()) dat = list(map(int, input().split())) s = sum(dat) num = s // n nul = [(0) for i in range(n)] c = 0 rs = [(0) for i in range(n)] for i in range(n - 1, -1, -1): c -= nul[i] if num: c += 1 if i - num >= 0: nul[i - num] += 1 dat[i] -= c if dat[i] == i and num: rs[i] = 1 num -= 1 print(*rs)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) it = -1 ans = [1] * n for i in range(n): if ans[i] == 0: it = a[i] + i elif a[i] == 0: it = i else: it = a[i] if it == n: break ans[it] = 0 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 ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
T = int(input()) for t in range(T): N = int(input()) C = list(map(int, input().split())) ans = [0] * N k = sum(C) // N i = N - 1 while i > -1 and k > 0: if C[i] == N: ans[i] = 1 k -= 1 else: C[i - k] += N - i i -= 1 print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
def solve(): n = int(input()) c = list(map(int, input().split())) sum = 0 for i in range(n): sum += c[i] ones = sum // n ans = [(1) for _ in range(n + 1)] first_0 = c[0] ans[first_0] = 0 for i in range(n): if ans[i] == 1: if c[i] == 0: ans[i] = 0 if ans[i] == 1: next_0 = c[i] ans[next_0] = 0 else: next_0 = i + c[i] ans[next_0] = 0 print(ans[i], end=" ") print() t = int(input()) for _ in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
def reversesortsum(nums): total = sum(nums) k = total // n ans = [0] * n b = [0] * n lf = n - k for i in range(lf, n): b[i] = n - 1 for i in range(n - 1, -1, -1): if lf > i: break cur = nums[i] - (b[i] - i) if cur == i + 1: ans[i] = 1 elif cur == 1: ans[i] = 0 lf -= 1 b[lf] = i - 1 return ans t = int(input()) for _ in range(t): n = int(input()) nums = [int(x) for x in input().split()] res = reversesortsum(nums) print(" ".join(str(x) for x in res))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted. Now consider the arrays $B_1, B_2,\ldots, B_n$ generated by $f(1,A), f(2,A),\ldots,f(n,A)$. Let $C$ be the array obtained by taking the element-wise sum of $B_1, B_2,\ldots, B_n$. For example, let $A=[0,1,0,1]$. Then we have $B_1=[0,1,0,1]$, $B_2=[0,1,0,1]$, $B_3=[0,0,1,1]$, $B_4=[0,0,1,1]$. Then $C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$. You are given $C$. Determine a binary array $A$ that would give $C$ when processed as above. It is guaranteed that an array $A$ exists for given $C$ in the input. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. Each test case has two lines. The first line contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($0 \leq c_i \leq n$). It is guaranteed that a valid array $A$ exists for the given $C$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output a single line containing $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $0$ or $1$). If there are multiple answers, you may output any of them. -----Examples----- Input 5 4 2 4 2 4 7 0 3 4 2 3 2 7 3 0 0 0 4 0 0 0 4 3 1 2 3 Output 1 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 1 -----Note----- Here's the explanation for the first test case. Given that $A=[1,1,0,1]$, we can construct each $B_i$: $B_1=[\color{blue}{1},1,0,1]$; $B_2=[\color{blue}{1},\color{blue}{1},0,1]$; $B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1]$; $B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]$ And then, we can sum up each column above to get $C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$.
import sys def solve(): inp = sys.stdin.readline n = int(input()) c = list(map(int, inp().split())) a = ["0"] * n sub = [0] * (n + 1) have = 0 j = 0 cur = 0 for i in range(0, n): sub[i - have + 1] += 1 sub[i + 1] -= 1 if j >= i - have + 1: cur += 1 while j < i - have: j += 1 cur += sub[j] while j > i - have: cur -= sub[j] j -= 1 if j >= 0 and cur < c[j]: a[i] = "1" sub[i] += i sub[i + 1] -= i if j == i: cur += i have += 1 if i - have + 1 < i + 1: sub[i - have + 1] += 1 sub[i - have + 2] -= 1 if j == i - have + 1: cur += 1 print(" ".join(a)) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR STRING VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
import sys input = sys.stdin.readline n, q = map(int, input().split()) A = list(map(int, input().split())) if max(A) == min(A): print(0) sys.exit() L = max(A) MM = [[200005, -1, i] for i in range(L + 1)] COUNT = [0] * (L + 1) for i in range(n): a = A[i] MM[a][0] = min(MM[a][0], i) MM[a][1] = max(MM[a][1], i) COUNT[a] += 1 MM.sort() i, j, k = MM[0] MAX = j CC = COUNT[k] MAXC = COUNT[k] ANS = 0 for i, j, k in MM[1:]: if i == 200005: ANS += CC - MAXC break if MAX < i: ANS += CC - MAXC MAX = j CC = COUNT[k] MAXC = COUNT[k] else: CC += COUNT[k] MAX = max(MAX, j) MAXC = max(MAXC, COUNT[k]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
from sys import stdin n, m = list(map(int, stdin.readline().strip().split())) s = list(map(int, stdin.readline().strip().split())) mx = 200000 arr = [(-1) for i in range(mx + 1)] visited = [(False) for i in range(mx + 1)] cnt = [(0) for i in range(mx + 1)] for i in range(n): arr[s[i]] = i x = 0 ans = 0 inf = mx + 30 while x < n: ind = arr[s[x]] v = [] l = x while x <= ind and x < n: ind = max(arr[s[x]], ind) v.append(s[x]) cnt[s[x]] += 1 x += 1 aux = 0 for i in v: aux = max(aux, cnt[i]) ans += x - l - aux print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
MAXN = 200100 n, q = list(map(int, input().split())) a = list(map(int, input().split())) lpos = [-1] * MAXN for i in range(n): lpos[a[i]] = i need = 0 i = 0 while i < n: start = i r = lpos[a[i]] cnts = {} while i <= r: r = max(r, lpos[a[i]]) if a[i] in cnts: cnts[a[i]] += 1 else: cnts[a[i]] = 1 i += 1 need += i - start - max(cnts.values()) print(need)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
from sys import stdin n, q = tuple(int(x) for x in stdin.readline().split()) tpl = tuple(x for x in stdin.readline().split()) dic = {} amt = {} for i in range(n): dic[tpl[i]] = i if tpl[i] not in amt: amt[tpl[i]] = 1 else: amt[tpl[i]] += 1 ans = 0 counter = 0 while counter < n: right_bound = dic[tpl[counter]] involved = set((tpl[counter],)) counter += 1 while counter < right_bound: if tpl[counter] not in involved: involved.add(tpl[counter]) right_bound = max(right_bound, dic[tpl[counter]]) counter += 1 temp = tuple(amt[x] for x in involved) ans += sum(temp) - max(temp) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
import itertools import sys n, q = map(int, input().split()) blocks = list(map(int, input().split())) dif = dict() for i in range(n): if blocks[i] in dif: dif[blocks[i]][1] = i dif[blocks[i]][2] = dif[blocks[i]][2] + 1 elif blocks[i] not in dif: dif[blocks[i]] = [i, i, 1] rez = 0 end = -1 maxi = -1 for i in range(n): if dif[blocks[i]][1] >= end: end = dif[blocks[i]][1] if dif[blocks[i]][2] >= maxi: maxi = dif[blocks[i]][2] if i == end: rez = rez + maxi maxi = 0 rez = -1 * rez for i in dif: rez = rez + dif[i][2] print(rez)
IMPORT IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
n, q = map(int, input().split()) A = list(map(int, input().split())) left = {} right = {} for i in range(n): if A[i] not in left: left[A[i]] = i right[A[i]] = i E = [] for elem in left: E.append([left[elem], -1]) E.append([right[elem], 1]) E.sort() u = 0 b = 0 cntr = {} ans = 0 for i in range(n): while u < len(E) and E[u][0] == i: b -= E[u][1] u += 1 if A[i] not in cntr: cntr[A[i]] = 0 cntr[A[i]] += 1 if b == 0: s = 0 m = 0 for iss in cntr: s += cntr[iss] m = max(m, cntr[iss]) ans += s - m cntr = {} print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
MAXN = 200100 n, q = map(int, input().split()) a = list(map(int, input().split())) lpos = [-1] * MAXN for i in range(n): lpos[a[i]] = i need = 0 i = 0 while i < n: start = i r = lpos[a[i]] j = i + 1 while j < r: r = max(r, lpos[a[j]]) j += 1 cnts = {} while i <= r: if a[i] in cnts: cnts[a[i]] += 1 else: cnts[a[i]] = 1 i += 1 best = 0 for k, v in cnts.items(): best = max(best, v) need += i - start - best print(need)
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
n, _q = map(int, input().split()) mni = [-1] * 200001 mxi = [-1] * 200001 cnt = [0] * 200001 nd = 0 a = list(map(int, input().split())) for i, v in enumerate(a): if mni[v] == -1: mni[v] = i nd += 1 mxi[v] = i cnt[v] += 1 r = 0 z = 0 currmax = 0 for i, v in enumerate(a): if i == mni[v]: z += 1 if i == mxi[v]: z -= 1 currmax = max(currmax, cnt[v]) if z == 0: r += currmax currmax = 0 print(n - r)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} def max_frequent(s, e, a): d = {} for x in a[s : e + 1]: if x not in d: d[x] = 0 d[x] += 1 return e - s + 1 - max(list(d.values())) for i, x in enumerate(a): if x not in d: d[x] = [] d[x].append(i) segment = [[v[0], v[-1]] for v in d.values()] end = -1 start = -1 block = [] for s, e in segment: if s > end: if end != -1: block.append([start, end]) start = s end = e if e > end: end = e block.append([start, end]) cnt = 0 for s, e in block: cnt += max_frequent(s, e, a) print(cnt)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
N, Q = list(map(int, input().split())) A = [int(a) for a in input().split()] B = sorted(list(set(A))) M = len(B) IA = {} for i in range(M): IA[B[i]] = i A = [IA[a] for a in A] L = [N] * M R = [-1] * M C = [0] * M for i in range(N): L[A[i]] = min(L[A[i]], i) R[A[i]] = max(R[A[i]], i) C[A[i]] += 1 X = [] for i in range(M): X.append((L[i], R[i], C[i])) X = sorted(X, key=lambda x: x[1]) Y = [(-1, 0, 0)] for i in range(M): l, r, t = X[i] m = t while Y[-1][0] > l: a, b, c = Y.pop() t += b m = max(m, c) Y.append((r, t, m)) print(sum([(y[1] - y[2]) for y in Y[1:]]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
n, q = map(int, input().split()) A = list(map(int, input().split())) sizes = dict() for j in range(n): if A[j] in sizes: sizes[A[j]][2] += 1 sizes[A[j]][1] = j else: sizes[A[j]] = [j, j, 1] answer = 0 end = -1 max_size = -1 for j in range(n): end = max(end, sizes[A[j]][1]) max_size = max(max_size, sizes[A[j]][2]) if j == end: answer += max_size max_size = 0 answer = -answer for j in sizes: answer += sizes[j][2] print(answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value $x$ to value $y$, you must also change all other elements of value $x$ into $y$ as well. For example, for $[3, 3, 1, 3, 2, 1, 2]$ it isn't allowed to change first $1$ to $3$ and second $1$ to $2$. You need to leave $1$'s untouched or change them to the same value. You are given a sequence of integers $a_1, a_2, \ldots, a_n$ and $q$ updates. Each update is of form "$i$ $x$" — change $a_i$ to $x$. Updates are not independent (the change stays for the future). Print the difficulty of the initial sequence and of the sequence after every update. -----Input----- The first line contains integers $n$ and $q$ ($1 \le n \le 200\,000$, $q = 0$), the length of the sequence and the number of the updates. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 200\,000$), the initial sequence. Each of the following $q$ lines contains integers $i_t$ and $x_t$ ($1 \le i_t \le n$, $1 \le x_t \le 200\,000$), the position and the new value for this position. -----Output----- Print $q+1$ integers, the answer for the initial sequence and the answer after every update. -----Examples----- Input 5 0 3 7 3 7 3 Output 2 Input 10 0 1 2 1 2 3 1 1 1 50 1 Output 4 Input 6 0 6 6 3 3 4 4 Output 0 Input 7 0 3 3 1 3 2 1 2 Output 4
import sys input = sys.stdin.buffer.readline def find(x): if par[x] == x: return x par[x] = find(par[x]) return par[x] def union(a, b): xa = find(a) xb = find(b) if size[xa] > size[xb]: xa, xb = xb, xa par[xa] = xb size[xb] += size[xa] n, m = map(int, input().split()) a = list(map(int, input().split())) par = [i for i in range(max(a) + 1)] size = [(1) for i in range(max(a) + 1)] counted = [(0) for i in range(max(a) + 1)] for i in range(n): counted[a[i]] += 1 first = [(0) for i in range(max(a) + 1)] last = [(0) for i in range(max(a) + 1)] for i in range(n): if first[a[i]] == 0: first[a[i]] = i for i in range(n - 1, -1, -1): if last[a[i]] == 0: last[a[i]] = i count = 0 spread = 0 counted1 = counted[:] for i in range(n): if count > 0: union(a[i], a[i - 1]) spread = max(spread, last[a[i]]) counted1[a[i]] -= 1 if spread == last[a[i]] and counted1[a[i]] == 0: count = 0 else: count += 1 maxid = [(0) for i in range(max(a) + 1)] for i in range(1, max(a) + 1): par[i] = find(par[i]) for i in range(1, max(a) + 1): maxid[par[i]] = max(maxid[par[i]], counted[i]) diff = 0 for i in range(max(a) + 1): diff += maxid[i] print(n - diff)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i def f(l, st, en): tmp = [] for j in range(st, en): tmp.append(j) if j % M == M - 1: while tmp: k = tmp.pop() mm = l.pop() S[mm] = k tmp = [] while tmp: k = tmp.pop() mm = l.pop() S[mm] = k (T,) = map(int, input().split()) for _ in range(T): N, M = map(int, input().split()) X = list(map(int, input().split())) Z = [] S = [0] * N * M for i in range(N * M): Z.append((i, X[i], X[i] * 10**6 + (M - i))) Z.sort(key=lambda x: x[2]) b = Z[0][1] tmp = [] start = 0 for i in range(N * M): j, a, _ = Z[i] if a != b: f(tmp, start, i) tmp = [] start = i tmp.append(j) b = a f(tmp, start, N * M) bt = [Bit(M) for _ in range(N)] R = 0 for i in range(N * M): kk = S[i] // M R += bt[kk].sum(S[i] % M + 1) bt[kk].add(S[i] % M + 1, 1) print(R)
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def argsort(seq): return sorted(range(len(seq)), key=seq.__getitem__) t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) x_all = list(map(int, input().split())) sorted_x = argsort(x_all) cnt = 0 for k in range(n): x = sorted(sorted_x[k * m : (k + 1) * m]) d = dict() for i in range(m): for k in d: if x_all[x[i]] > k: cnt += d[k] if x_all[x[i]] in d: d[x_all[x[i]]] += 1 else: d[x_all[x[i]]] = 1 print(cnt)
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 VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys class fenwick_tree: __slots__ = ["n", "data"] def __init__(self, n): self.n = n self.data = [0] * self.n def add(self, p, x): p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): return self._sum(r) - self._sum(l) def _sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s input = lambda: sys.stdin.readline().rstrip() def solve(): N, M = map(int, input().split()) As = list(map(int, input().split())) a2A = sorted(set(As)) A2a = {A: a for a, A in enumerate(a2A)} As = [A2a[A] for A in As] A_indices = [[] for _ in range(N * M)] for i, A in enumerate(sorted(As)): n = i // M m = i % M A_indices[A].append((n, m)) for indices in A_indices: indices.sort(key=lambda t: (t[0], -t[1]), reverse=True) fwts = [fenwick_tree(M) for _ in range(N)] answer = 0 for A in As: n, m = A_indices[A].pop() answer += fwts[n].sum(0, m) fwts[n].add(m, 1) print(answer) T = int(input()) for _ in range(T): solve()
IMPORT CLASS_DEF ASSIGN VAR LIST STRING STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline class fenwicktree: def __init__(self, Size): self.Tree = [0] * Size def add(self, Index, Value): Index += 1 while Index <= len(self.Tree): self.Tree[Index - 1] += Value Index += Index & -Index def get(self, R): Value = 0 while R != 0: Value += self.Tree[R - 1] R -= R & -R return Value def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) D = dict() Key = [] for a in A: if a in D: D[a] += 1 else: D[a] = 1 Key.append(a) Key.sort() K = len(Key) Comp = dict() Ninzu = [] for i in range(K): key = Key[i] Comp[key] = i Ninzu.append(D[key]) Seg1L = [0] * K Seg1R = [0] * K Seg2L = [0] * K Seg2R = [0] * K Extra = [0] * K L = 0 for i in range(K): R = L + Ninzu[i] Seg1L[i] = L LM = L // M RM = R // M if LM == RM: Seg1R[i] = R else: Seg1R[i] = (LM + 1) * M Extra[i] = (RM - LM - 1) * M Seg2L[i] = RM * M Seg2R[i] = R L = R Ans = 0 Seat = [fenwicktree(M) for _ in range(N)] for i in range(N * M): a = Comp[A[i]] if Seg1R[a] - Seg1L[a]: Seg1R[a] -= 1 h, w = divmod(Seg1R[a], M) Ans += Seat[h].get(w) Seat[h].add(w, 1) elif Extra[a]: Extra[a] -= 1 else: Seg2R[a] -= 1 h, w = divmod(Seg2R[a], M) Ans += Seat[h].get(w) Seat[h].add(w, 1) print(Ans) T = int(input()) for _ in range(T): main()
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
for _ in range(int(input())): n, m = map(int, input().split()) tot = n * m a = list(map(int, input().split())) l = list(range(tot)) l.sort(key=lambda x: (a[x], x)) mat = [] for i in range(n): mat.append(l[m * i : m * (i + 1)]) for i in range(n): mat[i].sort(key=lambda x: (a[x], -x)) res = 0 for i in range(n): cnt = 0 for j in range(m): for k in range(j): cnt += mat[i][k] < mat[i][j] res += cnt print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
from sys import stdin input = stdin.readline def answer(): s = [[(0) for i in range(m)] for j in range(n)] ans, ind = 0, 0 for i in range(n): for j in range(m): x = d[ind] // m y = d[ind] % m value = 0 for k in range(y + 1): value += s[x][k] ans += value s[x][y] = 1 ind += 1 return ans for T in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) x = [[a[i], i] for i in range(len(a))] x.sort() d = dict() ind = 0 for i in range(n): b = [] for j in range(m): b.append(x[ind + j][:]) b.sort(key=lambda x: [x[0], -x[1]]) for j in range(m): d[b[j][1]] = ind + j ind += m print(answer())
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline def s(a): n = len(a) r = 0 for i in range(n): if a[i] == 0: if i > 0 and a[i - 1] == 1 or i + 1 < n and a[i + 1] == 1: r += 2 else: r += 1 return r def solve(): n, m = map(int, input().split()) i = 0 a = [0] * (n * m) for v in map(int, input().split()): a[i] = v, i i += 1 a.sort() b = [0] * (n * m) for i in range(n): j = i * m k = j + m while j < k: z = j + 1 v = a[j][0] while z < k and a[z][0] == v: z += 1 for v in range(z - j): b[a[z - v - 1][1]] = j + v j = z h = [([0] * m) for i in range(n)] r = 0 for i in range(n * m): j = b[i] y, x = divmod(j, m) z = x H = h[y] while z >= 0: r += H[z] z = (z & z + 1) - 1 z = x while z < m: H[z] += 1 z = z | z + 1 print(r) for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
T = int(input()) for t in range(T): n, m = map(int, input().split()) a = list(enumerate(map(int, input().split()))) a.sort(key=lambda x: (x[1], x[0])) first = 0 num = a[0][1] for i in range(1, n * m): if a[i][1] != num: a[first:i] = reversed(a[first:i]) first = i num = a[i][1] if i % m == m - 1: a[first : i + 1] = reversed(a[first : i + 1]) first = i + 1 indxs = [None] * (n * m) for i in range(n * m): indxs[a[i][0]] = i cinema = [([0] * m) for _ in range(n)] ans = 0 for i in range(n * m): row = indxs[i] // m seat = indxs[i] % m ans += sum(cinema[row][:seat]) cinema[row][seat] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n, m = ma() a = lis() b = sorted(a) r = [] dic = {} cc = 0 row = [[(-1) for i in range(m)] for j in range(n)] for i in range(n): for j in range(m): row[i][j] = b[cc] cc += 1 ind = {} for i in range(n * m): try: ind[a[i]].append(i) except: ind[a[i]] = [i] for i in ind.keys(): ind[i] = ind[i][::-1] res = 0 for i in range(n): lr = [] for j in range(m): z = ind[row[i][j]].pop() lr.append([z, row[i][j]]) lr.sort() for j in range(m): for k1 in range(j + 1, m): if lr[j][1] < lr[k1][1]: res += 1 print(res)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
class segt: def __init__(self, n): self.bp = 1 while self.bp < n: self.bp <<= 1 self.heap = [0] * (2 * self.bp) def add(self, idx): idx += self.bp self.heap[idx] = 1 while idx > 0: idx //= 2 self.heap[idx] = self.heap[idx * 2] + self.heap[idx * 2 + 1] def q(self, l, r): l += self.bp r += self.bp ans = 0 while r >= l: if l & 1: ans += self.heap[l] l += 1 if not r & 1: ans += self.heap[r] r -= 1 l //= 2 r //= 2 return ans t = int(input()) for _ in range(t): n, m = map(int, input().split()) eyes = list(map(int, input().split())) e2 = [[eyes[i], i] for i in range(n * m)] e2 = sorted(e2, key=lambda x: (x[0], x[1])) cnt = dict() for el in e2: if el[0] in cnt: cnt[el[0]] += 1 else: cnt[el[0]] = 1 seats = [[(0) for i in range(m)] for j in range(n)] rs = set() d = dict() ptr = 0 for r in range(n): for c in range(m): el = e2[ptr] if not el[0] in rs: d[el[0]] = list() seats[r][c] = id rs.add(el[0]) d[el[0]].append(ptr + 1) ptr += 1 order = dict() for ky in d.keys(): order[ky] = list() stop = 0 for i in range(len(d[ky])): if d[ky][i] % m == 0 or i == len(d[ky]) - 1: for j in range(i, stop - 1, -1): order[ky].append(d[ky][j]) stop = i + 1 for ky in order.keys(): order[ky].reverse() segts = dict() for i in range(n): segts[i] = segt(m) anse = 0 for el in eyes: place = order[el].pop() q = (place - 1) // m anse += segts[q].q(0, (place - 1) % m) segts[q].add((place - 1) % m) print(anse)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys class BIT: def __init__(self, n): self.n = n self.data = [0] * n def build(self, arr): for i, a in enumerate(arr): self.data[i] = a for i in range(1, self.n + 1): if i + (i & -i) <= self.n: self.data[i + (i & -i) - 1] += self.data[i - 1] def add(self, p, x): p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, r): s = 0 while r: s += self.data[r - 1] r -= r & -r return s def range_sum(self, l, r): return self.sum(r) - self.sum(l) def solve(): n, m = map(int, input().split()) arr = list(zip(map(int, input().split()), range(n * m))) arr.sort() p = [0] * (n * m) i = 0 while i < m * n: a = arr[i][0] j = i while j < m * n and arr[j][0] == a: j += 1 L = [(k // m, -(k % m), k) for k in range(i, j)] L.sort() for k in range(i, j): p[arr[k][1]] = L[k - i][2] i = j v = BIT(n * m) ans = 0 for i, j in enumerate(p): ans += v.range_sum(j // m * m, j) v.add(j, 1) return ans input = lambda: sys.stdin.readline().rstrip() t = int(input()) for i in range(t): print(solve())
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) a = sorted((a[i], i) for i in range(len(a))) r, d = 0, 0 a.append((10**9 + 9, -1)) b = 0 ca = [] ans = [] for i in range(n * m + 1): if a[b][0] != a[i][0]: rw = sorted(ca) nd = d + len(rw) if d - m >= i - b: rw = rw[::-1] ans.extend(rw) else: ans.extend(rw[m - d - 1 :: -1]) rw = rw[m - d :] fd = len(rw) - len(rw) % m ans.extend(rw[:fd][::-1]) rw = rw[fd:] ans.extend(rw[::-1]) ca = [] b = i r += nd // m d = nd % m ca.append(a[i][1]) v = 0 for i in range(0, n * m, m): for j in range(i, i + m): for k in range(j, i + m): v += ans[j] < ans[k] print(v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
a = int(input()) for i in range(a): ok = dict() p1, p2 = map(int, input().split()) p3 = list(map(int, input().split())) op = p3[:] for j in range(p1 * p2): ok[p3[j]] = [] for j in range(p1 * p2): ok[p3[j]].append(j) p3 = sorted(p3) miss = [] for j in range(p1 * p2 - 1, -1, -1): miss.append(p3[p1 * p2 - 1 - j]) lol = [] for t in miss: ok[t] = sorted(ok[t])[::-1] for j in range(p1 * p2): lol.append(ok[miss[j]][-1]) ok[miss[j]].pop() z = [] for j in range(p1 * p2): z.append(0) stack = set() rez = 0 for j in range(p1 * p2): if j % p2 == 0: stack = set() for k in stack: if k < lol[j] and op[k] != op[lol[j]]: rez += 1 stack.add(lol[j]) print(rez)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.buffer.readline def solve(): indices = list(range(N * M)) indices.sort(key=lambda i: A[i]) indexOf = {} for i, val in enumerate(indices): indexOf[val] = i indices.sort(key=lambda i: (A[i], indexOf[i] // M, -i)) ans = 0 row = [0] * M for i in range(N): for j in range(M): row[j] = indices[i * M + j] for j in range(M): for k in range(j): if row[k] < row[j]: ans += 1 return ans test_cases = int(input()) for test_case in range(test_cases): N, M = map(int, input().split()) A = list(map(int, input().split())) print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) d = {} for i in range(n * m): d[a[i]] = d.get(a[i], []) d[a[i]].append(i) o = [[] for _ in range(n)] i = k = 0 dd = sorted(d) for j in range(n): while len(d[dd[i]][k:]) + len(o[j]) < m: o[j] += d[dd[i]][k:][::-1] k = 0 i += 1 kk = m - len(o[j]) o[j] += d[dd[i]][k : k + kk][::-1] k += kk d = {} for i in range(n): for j in range(m): d[o[i][j]] = i, j c = 0 s = [([False] * m) for _ in range(n)] for k in range(n * m): i, j = d[k] s[i][j] = True c += sum(s[i][:j]) print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys pin = sys.stdin.readline def update(tree, n): while n < len(tree): tree[n] += 1 n += n & -n def psum(tree, n): ans = 0 while n: ans += tree[n] n -= n & -n return ans for T in range(int(pin())): N, M = map(int, pin().split()) A = sorted([(v, k) for k, v in enumerate([*map(int, pin().split())])]) A = sorted([(v[0], i // M, -v[1], -(i % M)) for i, v in enumerate(A)]) A = sorted([(-v[2], v[1], i % M) for i, v in enumerate(A)]) Tree = [([0] * (M + 1)) for _ in range(N)] ans = 0 for i in range(M * N): r = A[i][1] c = A[i][2] update(Tree[r], c + 1) ans += psum(Tree[r], c) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
t = int(input()) for _ in range(t): n, m = tuple(map(int, input().split())) sp = input().split() sp = [(i, int(sp[i])) for i in range(n * m)] sp.sort(key=lambda x: (x[1], x[0])) new = [] for i in range(0, n * m, m): new.append(sorted(sp[i : i + m], key=lambda x: (x[1], -1 * x[0]))) ans = 0 for e in new: for i in e: for j in e: if i[0] > j[0]: ans += 1 elif i[0] == j[0]: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def solve(): n, m = map(int, input().split()) l = list(map(int, input().split())) sit = sorted([[l[i], i] for i in range(n * m)]) grid = [[(0) for i in range(m)] for j in range(n)] sit_number = {} for x in range(len(sit)): sit_number[sit[x][1]] = [x // m, x % m] ans = 0 for i in range(len(l)): x, y = sit_number[i] for c in range(y): if grid[x][c] != 0 and grid[x][c] != l[i]: ans += 1 grid[x][y] = l[i] print(ans) T = int(input()) for _ in range(T): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER LIST BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def mergeSort(arr, n): temp_arr = [0] * n return _mergeSort(arr, temp_arr, 0, n - 1) def _mergeSort(arr, temp_arr, left, right): inv_count = 0 if left < right: mid = (left + right) // 2 inv_count += _mergeSort(arr, temp_arr, left, mid) inv_count += _mergeSort(arr, temp_arr, mid + 1, right) inv_count += merge(arr, temp_arr, left, mid, right) return inv_count def merge(arr, temp_arr, left, mid, right): i = left j = mid + 1 k = left inv_count = 0 while i <= mid and j <= right: if arr[i] < arr[j]: temp_arr[k] = arr[i] k += 1 i += 1 else: temp_arr[k] = arr[j] inv_count += mid - i + 1 k += 1 j += 1 while i <= mid: temp_arr[k] = arr[i] k += 1 i += 1 while j <= right: temp_arr[k] = arr[j] k += 1 j += 1 for loop_var in range(left, right + 1): arr[loop_var] = temp_arr[loop_var] return inv_count t = int(input()) for _ in range(t): n, m = map(int, input().split()) l = list(map(int, input().split())) l = [[l[i], i] for i in range(n * m)] l.sort() ans = 0 i = 0 while i < n * m: c = [] d = 0 while d < m: c.append(l[i][-1::-1]) i += 1 d += 1 c = [i[1] for i in sorted(c)] ans += m * (m - 1) // 2 - mergeSort(c, m) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
for _ in range(int(input())): n, m = map(int, input().split()) l = [int(x) for x in input().split()] ans = [[l[i], i] for i in range(n * m)] ans.sort() inconv = 0 cnt = 0 for i in range(n): lst = ans[i * m : i * m + m] lst.sort(key=lambda x: x[1]) for i in range(m): for j in range(i): if lst[i][0] > lst[j][0]: cnt += 1 print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = iter(sys.stdin.read().splitlines()).__next__ def solve(): n, m = map(int, input().split()) sights = {} for i, sight in enumerate(map(int, input().split())): if sight not in sights: sights[sight] = [] sights[sight].append(i) seating = [None] * (n * m) current_row = 0 current_col = 0 for sight in sorted(sights): i = 0 while current_col + len(sights[sight]) - i > m: for person in reversed(sights[sight][i : i + m - current_col]): seating[person] = current_row, current_col current_col += 1 i += 1 current_col = 0 current_row += 1 for person in reversed(sights[sight][i:]): seating[person] = current_row, current_col current_col += 1 inconvenience = 0 occupied = [([False] * m) for _ in range(n)] for i in range(n * m): row_occupied = occupied[seating[i][0]] for j in range(seating[i][1]): if row_occupied[j]: inconvenience += 1 row_occupied[seating[i][1]] = True return inconvenience t = int(input()) output = [] for _ in range(t): output.append(solve()) print(*output, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
TESTS = int(input()) for t in range(TESTS): n, m = map(int, input().split()) people = list(map(int, input().split())) seats = people.copy() seats.sort() lookup = {} for i, s in enumerate(seats): if s not in lookup.keys(): lookup[s] = [i] else: lookup[s].append(i) inconvenience = 0 grid = [([0] * m) for i in range(n)] for key in lookup.keys(): lookup[key].sort(key=lambda x: -100000 * (x // m) + x % m) for p in people: index = lookup[p].pop(-1) nn = index // m mm = index % m grid[nn][mm] = 1 inconvenience += grid[nn][:mm].count(1) print(inconvenience)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def range_sum(self, l, r): return self.sum(r - 1) - self.sum(l - 1) def sum(self, i): i += 1 s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def get(self, i): return self.sum(i) - self.sum(i - 1) def add(self, i, x): i += 1 while i <= self.size: self.tree[i] += x i += i & -i def main(): n, m = map(int, input().split()) alst = list(map(int, input().split())) alst = [(a, i) for i, a in enumerate(alst)] alst.sort(key=lambda x: (x[0], x[1])) row = [[] for _ in range(n)] for j, (a, i) in enumerate(alst): row[j // m].append((i, a)) ans = 0 for alst in row: alst.sort() se = set() alst = [a for i, a in alst] se = set(alst) lst = sorted(se) dic = {a: i for i, a in enumerate(lst)} alst = [dic[a] for a in alst] l = len(lst) bit = Bit(l) for a in alst: ans += bit.sum(a - 1) bit.add(a, 1) print(ans) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def pos(sa, k, l=0, r=None): if r is None: r = len(sa) m = (l + r) // 2 if l == r: return l elif k > sa[m]: return pos(sa, k, m + 1, r) elif k < sa[m]: return pos(sa, k, l, m) else: return m def val(q): inc = 0 slist = [] for i in q: if not slist: slist.append(i) else: p = pos(slist, i) inc += p slist.insert(p, i) return inc t = int(input()) for _ in range(t): n, m = [int(i) for i in input().split()] a = [int(i) for i in input().split()] sights = sorted(list(set(a))) bins = {i: [] for i in sights} for i in range(len(a)): bins[a[i]].append(i) total_inc = 0 row = [] for i in sights: b = bins[i] z = 0 while z < len(b): if len(row) + len(b) - z < m: row += b[z:][::-1] z = len(b) else: fz = z + m - len(row) addition = b[z:fz][::-1] row += addition total_inc += val(row) row = [] z = fz print(total_inc)
FUNC_DEF NUMBER NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) s = sorted((v, i) for i, v in enumerate(a)) suma = 0 for i in range(n): start = s[m * i][0] end = s[m * (i + 1) - 1][0] if start != end: primers = 1 llista = s[m * i : m * (i + 1)] llista.sort(key=lambda x: x[1]) for k in range(m): val = llista[k][0] suma += sum(llista[j][0] > val for j in range(k + 1, m)) print(suma)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
t = int(input()) for _ in range(t): n, m = map(int, input().split(" ")) arr = list(map(int, input().split())) a = [] k = 0 for i in arr: a.append((i, k)) k += 1 a.sort() ans = 0 for i in range(0, n * m, m): y = a[i : i + m] y.sort(key=lambda x: x[1]) for j in range(1, m): jj = 0 while jj < j: if y[jj][0] < y[j][0]: ans += 1 jj += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys class SegmentTree: def __init__(self, data, default=0, func=lambda x, y: x + y): self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size : _size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): start += self._size stop += self._size res_left = res_right = self._default while start < stop: if start & 1: res_left = self._func(res_left, self.data[start]) start += 1 if stop & 1: stop -= 1 res_right = self._func(self.data[stop], res_right) start >>= 1 stop >>= 1 return self._func(res_left, res_right) def __repr__(self): return "SegmentTree({0})".format(self.data) input = sys.stdin.readline t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) l = [(a[i], i) for i in range(n * m)] l.sort() pos = [0] * (n * m) extra = 0 last = -1 start = -1 for i in range(n * m): if l[i][0] != last: last = l[i][0] start = i fst = max(start, i // m * m) extra += i - fst pos[l[i][1]] = i seg = SegmentTree([0] * (m * n)) out = 0 q = [] for i in range(m * n): loc = pos[i] rowst = loc // m * m out += seg.query(rowst, loc) q.append(loc) seg[loc] = 1 print(out - extra)
IMPORT CLASS_DEF FUNC_DEF NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().strip().split()) arr = [[int(x)] for x in input().strip().split()] for x in range(n * m): arr[x].append(x + 1) arr.sort() cnt = 0 for r in range(0, n * m, m): for i in range(r, r + m): for j in range(r, i): if arr[j][0] < arr[i][0] and arr[j][1] < arr[i][1]: cnt += 1 print(cnt)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$. $1$ $2$ $\cdots$ $m - 1$ $m$ $m + 1$ $m + 2$ $\cdots$ $2 m - 1$ $2 m$ $2m + 1$ $2m + 2$ $\cdots$ $3 m - 1$ $3 m$ $\vdots$ $\vdots$ $\ddots$ $\vdots$ $\vdots$ $m (n - 1) + 1$ $m (n - 1) + 2$ $\cdots$ $n m - 1$ $n m$ The table with seats indices There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person. It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i < a_j$ it should be satisfied that $s_i < s_j$. After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through. Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$. Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied). -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 300$) — the number of rows and places in each row respectively. The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person. It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a single integer — the minimal total inconvenience that can be achieved. -----Examples----- Input 7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3 Output 1 0 4 0 0 0 1 -----Note----- In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is $1$. In the second test case the optimal seating looks like this: In the third test case the optimal seating looks like this: The number in a cell is the person's index that sits on this place.
def mergeSort(myList, count): if len(myList) > 1: mid = len(myList) // 2 left = myList[:mid] right = myList[mid:] mergeSort(left, count) mergeSort(right, count) i = 0 j = 0 k = 0 while i < len(left) and j < len(right): if left[i] < right[j]: count[0] += len(right) - j myList[k] = left[i] i += 1 else: myList[k] = right[j] j += 1 k += 1 while i < len(left): myList[k] = left[i] i += 1 k += 1 while j < len(right): myList[k] = right[j] j += 1 k += 1 count = [0] for __ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) lis = [] for i, j in enumerate(a): temp = i lis.append([temp, j]) lis = sorted(lis, key=lambda x: (x[1], x[0])) tot = 0 for i in range(n): arr = [] for j in range(i * m, (i + 1) * m): arr.append(lis[j]) arr = sorted(arr, key=lambda x: x[0]) newarr = [] for jk in arr: newarr.append(jk[1]) count = [0] mergeSort(newarr, count) tot += count[0] print(tot)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l = 0 h = len(people) - 1 remain_capacity = limit if len(people) == 0: return 0 boat = 1 ppl_in_boat = 2 while l <= h: if people[h] <= remain_capacity and ppl_in_boat != 0: remain_capacity -= people[h] ppl_in_boat -= 1 h = h - 1 elif remain_capacity >= people[l] and ppl_in_boat != 0: remain_capacity -= people[l] ppl_in_boat -= 1 l = l + 1 else: boat += 1 remain_capacity = limit ppl_in_boat = 2 return boat
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() print(people) boats = 0 left_index = 0 right_index = len(people) - 1 while left_index <= right_index: if people[left_index] + people[right_index] <= limit: left_index += 1 boats += 1 right_index -= 1 return boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: bucket = [0] * (limit + 1) for i in people: bucket[i] += 1 start = 0 end = len(bucket) - 1 count = 0 while start <= end: while start <= end and bucket[start] <= 0: start += 1 while start <= end and bucket[end] <= 0: end -= 1 if bucket[start] <= 0 and bucket[end] <= 0: break count += 1 if start + end <= limit: bucket[start] -= 1 bucket[end] -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) count = 0 left = 0 right = len(people) - 1 while left <= right: cur = people[left] + people[right] if cur <= limit: left += 1 right -= 1 elif cur > limit: right -= 1 count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if people == None or len(people) == 0: return 0 people.sort() start = 0 end = len(people) - 1 counter = 0 while start <= end: if people[start] + people[end] <= limit: start += 1 end -= 1 else: end -= 1 counter += 1 return counter
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() p1 = 0 p2 = len(people) - 1 count = 1 cur = 0 curNum = 0 while p1 <= p2: if people[p2] <= limit - cur and curNum < 2: cur = cur + people[p2] curNum += 1 p2 -= 1 continue if people[p1] <= limit - cur and curNum < 2: cur = cur + people[p1] curNum += 1 p1 += 1 continue count += 1 cur = 0 curNum = 0 return count
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if not people: return 0 people = sorted(people) left = 0 right = len(people) - 1 board_cnt = 0 while left <= right: if left == right: board_cnt += 1 break if people[left] + people[right] <= limit: left += 1 right -= 1 board_cnt += 1 else: right -= 1 board_cnt += 1 return board_cnt
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() boats = 0 l = 0 r = len(people) - 1 while r > l: boats += 1 if people[l] + people[r] <= limit: l += 1 r = r - 1 else: r = r - 1 if r == l: boats += 1 return boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: boats = 0 people = sorted(people) print(people) i, j = 0, len(people) - 1 while i <= j: if people[i] + people[j] > limit: j -= 1 else: i += 1 j -= 1 boats += 1 return boats
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() num_boats = 0 last = len(people) - 1 first = 0 while first < last: if people[first] + people[last] <= limit: last -= 1 first += 1 num_boats += 1 else: num_boats += 1 last -= 1 if first == last: num_boats += 1 return num_boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: l = len(people) if l <= 1: return 1 people.sort() i = 0 j = l - 1 c = 0 while i < j: print((i, j, c)) if people[i] + people[j] <= limit: c += 1 i += 1 j -= 1 else: j -= 1 c += 1 if i == j: c += 1 return c def sortPeople(self, arr): l = len(arr) if l <= 1: return arr pivot = arr[l - 1] larr = [] rarr = [] for i in range(l - 1): if arr[i] >= pivot: rarr.append(arr[i]) else: larr.append(arr[i]) return self.sortPeople(larr) + [pivot] + self.sortPeople(rarr)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: heap = [] people.sort() heap.append((people[len(people) - 1], 1)) i = len(people) - 2 while i >= 0: ele = people[i] if limit >= ele + heap[0][0] and heap[0][1] < 2: minimum = heapq.heappop(heap) heapq.heappush(heap, (float("inf"), 2)) else: heapq.heappush(heap, (ele, 1)) i -= 1 print(heap) return len(heap)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo = 0 count = 0 for hi in range(len(people))[::-1]: if lo < hi and people[lo] + people[hi] <= limit: lo += 1 if lo == hi: return len(people) - hi
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() N = len(people) num_boats = 0 if len(people) == 1: return 1 less_than_half = [p for p in people if p <= limit / 2] more_than_half = [p for p in people if p > limit / 2] while len(more_than_half) > 0 and len(less_than_half) > 0: if more_than_half[-1] + less_than_half[0] > limit: more_than_half.pop(-1) num_boats += 1 continue else: more_than_half.pop(-1) less_than_half.pop(0) num_boats += 1 continue num_boats += len(more_than_half) num_boats += int(len(less_than_half) / 2 + 0.5) return num_boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) boats = people[: (1 + len(people)) // 2] onboard = [1] * len(boats) i = 0 j = len(boats) k = len(people) - 1 while j <= k: if i == len(boats): boats.append(people[j]) j += 1 if j > k: break target = limit - boats[i] if people[k] > target: i += 1 if i == len(boats): boats.append(people[j]) j += 1 else: boats[i] += people[k] k -= 1 i += 1 return len(boats)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: left = 0 right = len(people) - 1 boat = 0 people.sort() while left <= right: if left == right: boat += 1 break elif people[left] + people[right] <= limit: left += 1 right -= 1 boat += 1 return boat
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people, reverse=True) i = 0 j = len(people) - 1 n = 0 while True: if i >= j: break n += 1 w1 = people[i] i += 1 rem = limit - w1 if rem >= people[j]: j -= 1 if i == j: n += 1 return n
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: new = sorted(people) i, j = 0, len(people) - 1 res = 0 while i <= j: if new[j] + new[i] <= limit: i += 1 j -= 1 res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: minBoats = 0 people.sort() left = 0 right = len(people) - 1 while left <= right: if people[left] + people[right] <= limit: left += 1 minBoats += 1 right -= 1 return minBoats
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR