description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) f = input for _ in range(t): n, k = map(int, f().split()) x = sorted(map(int, f().split())) f() r = [0] * n c = n - 1 for i in range(n - 1, -1, -1): while x[c] > x[i] + k: c -= 1 r[i] = c m = [0] * n c = 0 for i in range(n - 1, -1, -1): m[i] = c c = max(r[i] - i + 1, c) o = [(r[i] - i + 1 + m[r[i]]) for i in range(n)] print(max(o))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for p in range(t): arr = [int(i) for i in input().split()] n = arr[0] k = arr[1] x_list = [int(i) for i in input().split()] y_list = [int(i) for i in input().split()] coordinates = [] for i in range(n): coordinates.append([x_list[i], y_list[i]]) coordinates = sorted(coordinates, key=lambda x: x[0]) dp = [(0) for i in range(n)] j = n - 1 for i in range(n - 1, -1, -1): while j >= i and coordinates[j][0] - coordinates[i][0] > k: j -= 1 dp[i] = j - i + 1 dp1 = list(dp) dp1 = dp1[::-1] suffix_dp = [(0) for i in range(n)] suffix_dp[0] = dp1[0] for i in range(1, n): suffix_dp[i] = max(suffix_dp[i - 1], dp1[i - 1]) suffix_dp = suffix_dp[::-1] dp2 = [(0) for i in range(n)] dp2[0] = 1 j = 0 for i in range(n): while j <= i and coordinates[i][0] - coordinates[j][0] > k: j += 1 dp2[i] = i - j + 1 ans = 0 for i in range(n): if i == n - 1: ans = max(ans, dp2[i]) else: ans = max(dp2[i] + suffix_dp[i], ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN 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 LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = list(map(int, input().split())) xcor = list(map(int, input().split())) ycor = list(map(int, input().split())) arr = [] for i in range(n): arr.append([xcor[i], ycor[i]]) arr.sort() dp = [(0) for i in range(n)] dp[n - 1] = 1 for i in range(n - 2, -1, -1): dp[i] = dp[i + 1] lo = i hi = n - 1 e = 0 while lo <= hi: mid = (lo + hi) // 2 if arr[mid][0] <= arr[i][0] + k: e = mid - i + 1 lo = mid + 1 else: hi = mid - 1 dp[i] = max(dp[i], e) ans = 0 for i in range(n): lo = i hi = n - 1 pos = i while lo <= hi: mid = (lo + hi) // 2 if arr[mid][0] <= arr[i][0] + k: pos = mid lo = mid + 1 else: hi = mid - 1 me = pos - i + 1 if pos + 1 < n: me += dp[pos + 1] ans = max(ans, me) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) xs = list(map(int, input().split())) ys = list(map(int, input().split())) pts = list(zip(xs, ys)) pts.sort(key=lambda p: p[0]) dp = [] rp = 0 for i in range(n): while rp < n and pts[rp][0] - pts[i][0] <= k: rp += 1 dp.append(rp - i) suffixes = [0] * n suffixes[-1] = dp[-1] for i in range(n - 2, -1, -1): suffixes[i] = max(suffixes[i + 1], dp[i]) suffixes.append(0) ans = 0 rp = 0 for i in range(n): while rp < n and pts[rp][0] - pts[i][0] <= k: rp += 1 ans = max(ans, dp[i] + suffixes[rp]) 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 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 VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def arr(A, k): n = len(A) pre = [0] * n l = 0 r = 0 while r < n: if abs(A[r] - A[l]) <= k: r += 1 else: while l < r and abs(A[r] - A[l]) > k: l += 1 r += 1 pre[r - 1] = r - l return pre def answer(n, k, A): if n == 1: return 1 A.sort() pre = arr(A, k) suff = arr(A[::-1], k) suff = suff[::-1] maxip = [0] * n maxi = 0 maxis = [0] * n for i in range(n): maxi = max(maxi, pre[i]) maxip[i] = maxi maxi = 0 for i in range(n - 1, -1, -1): maxi = max(maxi, suff[i]) maxis[i] = maxi maxi = 0 for i in range(n - 1): maxi = max(maxi, maxip[i] + maxis[i + 1]) return maxi t = int(input()) for i in range(t): n, k = map(int, input().split()) arr1 = list(map(int, input().split())) arr2 = list(map(int, input().split())) print(answer(n, k, arr1))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def solve(): n, k = map(int, input().split()) a = [int(i) for i in input().split()] input() dp = [0] * (n + 10) ma = [0] * (n + 10) r = -1 a.sort() for i in range(n): r = max(r, i) for j in range(r, n): if a[j] <= a[i] + k: r = j else: break dp[i] = r - i + 1 for i in range(n, -1, -1): ma[i] = max(dp[i], ma[i + 1]) ans = 0 for i in range(n): ans = max(dp[i] + ma[i + dp[i]], ans) print(ans) t = int(input()) for i in range(t): solve()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP 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
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = list(map(int, input().split())) nums, _ = sorted(map(int, input().split())), list(map(int, input().split())) if 2 * k >= nums[-1] - nums[0]: print(len(nums)) continue f = [0] * (n + 1) l = ans = 0 for r, v in enumerate(nums): while v - nums[l] > k: l += 1 f[r + 1] = max(f[r], r - l + 1) ans = max(ans, f[l] + r - l + 1) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
T = int(input()) for case in range(T): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) suf = [(0) for i in range(n + 2)] ans = [(0) for i in range(n + 2)] x.sort() j = 0 for i in range(n): while j < n and x[j] <= x[i] + k: j += 1 ans[i] = j - i for i in range(n - 1, -1, -1): suf[i] = max(ans[i], suf[i + 1]) mx = 0 for i in range(n): mx = max(mx, ans[i] + suf[min(n, ans[i] + i)]) print(mx)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def solve(coords, k): left = 0 right = 0 dp = [0] result = 0 while True: while right < len(coords) and coords[right] - coords[left] <= k: right += 1 dp.append(max(dp[right - 1], right - left)) result = max(result, dp[left] + right - left) if right == len(coords): break left += 1 return result for _ in range(int(input())): n, k = map(int, input().split()) coords = sorted(map(int, input().split())) input() print(solve(coords, k))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for i in range(t): params = input() n, k = int(params.split()[0]), int(params.split()[1]) coordinates = input() something = input() numbers = list(map(int, coordinates.split())) numbers.sort() if numbers[-1] - numbers[0] <= 2 * k or len(numbers) == 1: print(n) continue right = [0] * len(numbers) left = [0] * len(numbers) left[0] = 1 p = 0 q = 0 while True: while q < len(numbers) and numbers[q] - numbers[p] <= k: left[q] = q - p + 1 q += 1 if q == len(numbers): right[p] = q - p break right[p] = q - p while numbers[q] - numbers[p] > k: p += 1 right[p] = q - p if numbers[q] - numbers[p] == k: right[p] = right[p - 1] for j in range(0, len(numbers) - 1): left[j + 1] = max(left[j], left[j + 1]) for j in range(len(numbers) - 1, 0, -1): right[j - 1] = max(right[j - 1], right[j]) maximum = 0 for j in range(0, len(numbers) - 1): maximum = max(left[j] + right[j + 1], maximum) print(maximum)
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 FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def ans(n, k, x, y): x.sort() l = [0] * len(x) r = [0] * len(x) i = 0 j = 0 temp = 0 while i < len(x) and j < len(x): temp = i - j + 1 if i == j: l[i] = temp i += 1 elif x[i] - x[j] <= k: l[i] = temp i += 1 else: j += 1 i = n - 1 j = n - 1 temp = 0 while i >= 0 and j >= 0: temp = j - i + 1 if i == j: r[i] = temp i -= 1 elif x[j] - x[i] <= k: r[i] = temp i -= 1 else: j -= 1 prefix = [0] * len(x) suffix = [0] * len(x) prefix[0] = l[0] suffix[-1] = r[-1] for i in range(1, len(x)): prefix[i] = max(l[i], prefix[i - 1]) for i in range(len(x) - 2, -1, -1): suffix[i] = max(r[i], suffix[i + 1]) answer = 1 for i in range(n - 1): answer = max(answer, prefix[i] + suffix[i + 1]) print(answer) m = int(input()) for i in range(m): arr = input().split() n = int(arr[0]) k = int(arr[1]) a = input().split() b = input().split() x = [] y = [] for j in a: x.append(int(j)) for j in b: y.append(int(j)) ans(n, k, x, y)
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) y = list(map(int, input().split())) arr.sort() i = j = 0 end = [0] * n start = [0] * n while j < n: if arr[j] - arr[i] <= k: if j: end[j] = max(end[j - 1], j - i + 1) else: end[j] = j - i + 1 j += 1 else: i += 1 i = j = n - 1 while i >= 0: if arr[j] - arr[i] <= k: if i == n - 1: start[i] = j - i + 1 else: start[i] = max(start[i + 1], j - i + 1) i -= 1 else: j -= 1 ans = 1 for i in range(n - 1): ans = max(ans, start[i + 1] + end[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin, stdout def find(N, K, X): X.sort() arrA = [0] j = 0 r = 0 for i in range(N): r += 1 if i < N - 1 and X[i] == X[i + 1]: continue while X[i] - X[j] > K: j += 1 r -= 1 arrA.append(r) X.reverse() arrB = [0] j = 0 r = 0 for i in range(N): r += 1 if i < N - 1 and X[i] == X[i + 1]: continue while -X[i] + X[j] > K: j += 1 r -= 1 arrB.append(r) for i in range(1, len(arrA)): arrA[i] = max(arrA[i], arrA[i - 1]) arrB[i] = max(arrB[i], arrB[i - 1]) arrB.reverse() return max([(arrA[i] + arrB[i]) for i in range(len(arrA))]) def main(): for _ in range(int(stdin.readline())): N, K = list(map(int, stdin.readline().split())) X = list(map(int, stdin.readline().split())) list(map(int, stdin.readline().split())) print(find(N, K, X)) main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) y = map(int, input().split()) x.sort() start = 0 p = [0] * n p[0] = 1 for i in range(1, n): if x[i] - x[start] <= k: p[i] = i - start + 1 else: while x[i] - x[start] > k: start += 1 p[i] = i - start + 1 last = n - 1 s = [0] * n s[-1] = 1 for i in range(len(s) - 2, -1, -1): if x[last] - x[i] <= k: s[i] = last - i + 1 else: while x[last] - x[i] > k: last -= 1 s[i] = last - i + 1 for i in range(1, n): p[i] = max(p[i], p[i - 1], 0) mx = p[0] for i in range(n - 1): mx = max(mx, p[i] + s[i + 1]) print(max(mx, max(p), max(s)))
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 FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin, stdout input = stdin.buffer.readline for _ in range(int(input())): n, k = map(int, input().split()) (*x,) = map(int, input().split()) (*y,) = map(int, input().split()) a = [[x[i], y[i]] for i in range(n)] b = [0] * n c = [0] * (n + 1) dp = [0] * (n + 1) a.sort() j = 0 c[0] = a[0][0] b[0] = 1 for i in range(1, n): if a[i][0] != a[i - 1][0]: j += 1 c[j] = a[i][0] b[j] += 1 pref = [0] * (n + 1) for i in range(n): pref[i] = pref[i - 1] + b[i] i = j = 0 ans = 0 while i < n: while c[i] - c[j] > k: j += 1 ans = max(ans, dp[j - 1] + pref[i] - pref[j - 1]) dp[i] = max(dp[i - 1], pref[i] - pref[j - 1]) i += 1 print(ans)
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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def run(): n, k = map(int, input().split()) xS = list(map(int, input().split())) yS = list(map(int, input().split())) lMax, rMax = [0] * n, [0] * n xS.sort() j = n - 1 for i in range(n - 1, -1, -1): while xS[j] - xS[i] > k: j -= 1 rMax[i] = j - i + 1 if i + 1 < n: rMax[i] = max(rMax[i], rMax[i + 1]) j = 0 for i in range(n): while xS[i] - xS[j] > k: j += 1 lMax[i] = i - j + 1 if i > 0: lMax[i] = max(lMax[i], lMax[i - 1]) ans = 1 for i in range(n - 1): ans = max(ans, rMax[i + 1] + lMax[i]) print(ans) if 1: for i in range(int(input())): run() else: run()
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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for test in range(t): n, k = map(int, input().split()) points = [] x = list(map(int, input().split())) y = list(map(int, input().split())) for i in range(n): points.append([x[i], y[i]]) points.sort() l, r = 0, 0 lrs = [] while l < n: r = max(r, l) while r < n and points[r][0] - points[l][0] <= k: r += 1 lrs.append(r - l) l += 1 l, r = 0, 0 rrs = [] points.reverse() while l < n: r = max(r, l) while r < n and points[l][0] - points[r][0] <= k: r += 1 rrs.append(r - l) l += 1 rrs.reverse() prer = [0] for i in range(len(lrs) - 1, -1, -1): prer.append(max(prer[-1], lrs[i])) ans = 0 prer.reverse() for i in range(n): ans = max(rrs[i] + prer[i + 1], ans) 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 LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
T = int(input()) while T > 0: n, k = map(int, input().split()) xs = list(map(int, input().split())) ys = list(map(int, input().split())) xs.sort() dp1 = [0] * n start = 0 for i in range(n): while xs[i] - xs[start] > k: start += 1 dp1[i] = max(dp1[i - 1], i - start + 1) start = 0 dp2 = [0] * n for i in range(n): while xs[i] - xs[start] > k: start += 1 if start > 0: dp2[i] = max(dp2[i - 1], i - start + 1 + dp1[start - 1]) else: dp2[i] = max(dp2[i - 1], i - start + 1) res = dp2[-1] print(res) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split())) MI = lambda: map(int, sys.stdin.readline().strip("\n").split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline().strip("\n")) for _ in range(II()): n, k = MI() a = sorted(MI()) b = MI() l, r = [0] * n, [0] * n for i in range(n): s, e = 0, i - 1 ret = -1 val = a[i] - k while s <= e: m = (s + e) // 2 if a[m] >= val: e = m - 1 else: s = m + 1 ret = m l[i] = i - ret if ret != -1 else i + 1 s, e = i + 1, n - 1 ret = -1 val = a[i] + k while s <= e: m = (s + e) // 2 if a[m] <= val: s = m + 1 else: e = m - 1 ret = m r[i] = ret - i if ret != -1 else n - i for i in range(1, n): l[i] = max(l[i], l[i - 1]) for i in range(n - 2, -1, -1): r[i] = max(r[i], r[i + 1]) ans = 1 for i in range(n - 1): ans = max(ans, l[i] + r[i + 1]) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin class Input: def readline(self): return stdin.readline().strip() def read_int(self): return int(self.readline()) def read_list(self): return self.readline().split() def test_cases(self): cases = self.read_int() for case in range(cases): yield self class Problem: def __init__(self, input): n, k = list(map(int, input.read_list())) xcoord = list(map(int, input.read_list())) ycoord = list(map(int, input.read_list())) count = {(0): 0} for x in xcoord: count[x] = count.get(x, 0) + 1 a = [] for pair in count.items(): a.append(pair) a.sort(key=lambda p: p[0]) i = 1 j = 0 n = len(a) s = [(0) for i in range(n)] dp = [(0) for i in range(n)] ans = 0 while i < n: s[i] = s[i - 1] + a[i][1] while j + 1 < i and a[j + 1][0] + k < a[i][0]: j += 1 s[i] -= a[j][1] dp[i] = max(dp[i - 1], s[i]) ans = max(ans, s[i] + dp[j]) i += 1 print(ans) for case_input in Input().test_cases(): Problem(case_input)
CLASS_DEF FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = list(map(int, input().split())) y = input() x.sort() L = [(0) for _ in range(n)] R = [(0) for _ in range(n)] prev = 0 L[0] = 1 for i in range(1, n): L[i] = L[i - 1] + 1 while x[i] - x[prev] > k: prev += 1 L[i] -= 1 prev = n - 1 R[prev] = 1 for i in range(n - 2, -1, -1): R[i] = R[i + 1] + 1 while x[prev] - x[i] > k: prev -= 1 R[i] -= 1 for i in range(1, n): L[i] = max(L[i], L[i - 1]) for i in range(n - 2, -1, -1): R[i] = max(R[i], R[i + 1]) ans = 1 for i in range(n - 1): ans = max(ans, R[i + 1] + L[i]) 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input().strip()) for _ in range(t): n, k = map(int, input().split()) arr = [int(i) for i in input().split()] xx = input() if n == 1: print(1) else: arr.sort() cnt = [(0) for i in range(n)] left = arr[0] right = left + k for i in range(n): if arr[i] <= right: cnt[0] += 1 else: break index = 1 right_point = i cons = 1 while index < n: left = arr[index] if arr[index] != arr[index - 1]: cnt[index] = cnt[index - 1] - cons cons = 1 for i in range(right_point, n): if arr[i] > left + k: right_point = i break if i == n - 1: right_point = n cnt[index] += 1 else: cons += 1 cnt[index] = cnt[index - 1] index += 1 dp = [(0) for i in range(n + 1)] for i in range(n - 1, -1, -1): dp[i] = max(dp[i + 1], cnt[i]) ans = 0 for i in range(n): result = cnt[i] + (0 if i + cnt[i] >= n else dp[i + cnt[i]]) if result > ans: ans = result print(ans)
ASSIGN VAR FUNC_CALL 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() plat1 = [0] * n plat2 = [0] * n p_left = 0 for i in range(n): if x[i] > x[p_left] + k: p_left += 1 plat1[i] = i - p_left + 1 if i > 0: plat1[i] = max(plat1[i - 1], plat1[i]) p_right = n - 1 for i in range(n - 1, -1, -1): if x[i] < x[p_right] - k: p_right -= 1 plat2[i] = p_right - i + 1 if i < n - 1: plat2[i] = max(plat2[i + 1], plat2[i]) ans = 1 for i in range(n - 1): ans = max(ans, plat1[i] + plat2[i + 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def solve(): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() l = [0] * len(x) r = [0] * len(x) st = 0 for i in range(len(x)): while x[i] - k > x[st]: st += 1 l[i] = i - st + 1 if i > 0: l[i] = max(l[i], l[i - 1]) en = len(x) - 1 for i in reversed(range(len(x))): while x[i] + k < x[en]: en -= 1 r[i] = en - i + 1 if i < len(x) - 1: r[i] = max(r[i], r[i + 1]) ans = 0 for i in range(len(x)): if i == len(x) - 1: ans = max(ans, l[i]) else: ans = max(ans, l[i] + r[i + 1]) 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP 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
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
(T,) = map(int, input().split()) for _ in range(T): N, k = map(int, input().split()) X = list(sorted(list(map(int, input().split())))) + [10**18] Y = list(map(int, input().split())) + [10**18] yi = 0 Z = [0] * N V = [1] * N for i in range(N): while X[yi + 1] <= X[i] + k: yi += 1 V[yi] = yi - i + 1 Z[i] = yi - i + 1 for i in range(1, N): V[i] = max(V[i], V[i - 1]) for i in range(N - 2, -1, -1): Z[i] = max(Z[i], Z[i + 1]) R = 1 for i in range(N - 1): R = max(R, V[i] + Z[i + 1]) print(R)
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 BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def solution(n, k, x, y): x.sort() l, r, a, b, c = [], [], 0, 0, 1 while b < n: if c <= n - 1: while x[c] - x[b] <= k: c += 1 if c == n: break while x[b] - x[a] > k: a += 1 r.append(c - b) l.append(b - a + 1) b += 1 for i in range(1, n): l[i] = max(l[i - 1], l[i]) r[n - 1 - i] = max(r[n - 1 - i], r[n - i]) max_ = l[0] for i in range(n - 1): max_ = max(max_, l[i] + r[i + 1]) return max_ t = int(input()) for _ in range(t): n, k = map(int, input().split(" ")) x = list(map(int, input().split(" "))) y = list(map(int, input().split(" "))) print(solution(n, k, x, y))
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR LIST LIST NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys stdin = sys.stdin stdout = sys.stdout def binSearch(arr, num, start, end): while start != end: mid = (start + end) // 2 if arr[mid] >= num: end = mid else: start = mid + 1 return start test = int(stdin.readline()) for t in range(test): n, k = [int(_) for _ in stdin.readline().split()] x = [int(_) for _ in stdin.readline().split()] y = [int(_) for _ in stdin.readline().split()] x.sort() dp = [(0) for i in range(n + 1)] dp2 = [(0) for i in range(n + 1)] for i in range(1, len(x) + 1): dp[i] = dp[i - 1] dp2[i] = dp2[i - 1] covered = x[i - 1] - k idx = binSearch(x, covered, 0, i - 1) dp[i] = max(dp[i], i - idx) dp2[i] = max(dp2[i], i - idx + dp[idx]) stdout.write(f"{dp2[n]}\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR 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 NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys as _sys def main(): t = int(input()) for i in range(t): n, k = _read_ints() xs = tuple(_read_ints()) ys = tuple(_read_ints()) result = find_max_points_can_save_n(xs, ys, platform_size=k) print(result) def _read_ints(): return map(int, _sys.stdin.readline().split(" ")) def find_max_points_can_save_n(points_xs, points_ys, platform_size): del points_ys max_points_can_save = _find_max_range(points_xs, platform_size * 2 + 1)[1] ranges = _compute_ranges(points_xs, platform_size) first_ranges = list(ranges) second_ranges = sorted(ranges, key=lambda range_: range_[1], reverse=True) i_second = 0 for (first_x, last_x), points_saved in first_ranges: while i_second < len(second_ranges) and second_ranges[i_second][0][0] <= last_x: i_second += 1 if i_second >= len(second_ranges): break points_saved_second = second_ranges[i_second][1] max_points_can_save = max( max_points_can_save, points_saved + points_saved_second ) return max_points_can_save def _find_max_range(points_xs, range_size): if not points_xs: return (0, range_size), 0 points_xs = sorted(points_xs) points_n = len(points_xs) first_x = points_xs[0] last_x = first_x + range_size max_range = None max_range_value = 0 i_points_begin = 0 i_points_end = 0 while True: while i_points_begin < points_n and points_xs[i_points_begin] < first_x: i_points_begin += 1 while i_points_end < points_n and points_xs[i_points_end] <= last_x: i_points_end += 1 if i_points_end - i_points_begin > max_range_value: max_range = first_x, last_x max_range_value = i_points_end - i_points_begin if i_points_end >= points_n: break last_x = points_xs[i_points_end] first_x = last_x - range_size return max_range, max_range_value def _compute_ranges(points_xs, range_size): if not points_xs: return (0, range_size), 0 points_xs = sorted(points_xs) points_n = len(points_xs) computed_ranges = [] first_x = points_xs[0] last_x = first_x + range_size i_points_begin = 0 i_points_end = 0 while True: while i_points_begin < points_n and points_xs[i_points_begin] < first_x: i_points_begin += 1 while i_points_end < points_n and points_xs[i_points_end] <= last_x: i_points_end += 1 computed_ranges.append(((first_x, last_x), i_points_end - i_points_begin)) if i_points_end >= points_n: break last_x = points_xs[i_points_end] first_x = last_x - range_size return computed_ranges main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF IF VAR RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys t = int(input()) for _ in range(t): [n, k] = map(int, input().split()) x = list(map(int, input().split())) x.sort() input() j = n - 1 r = [0] * n for i in range(n - 1, -1, -1): while x[j] - x[i] > k: j -= 1 r[i] = j - i + 1 j = 0 l = [0] * n for i in range(0, n): while x[i] - x[j] > k: j += 1 l[i] = i - j + 1 for i in range(1, n): l[i] = max(l[i - 1], l[i]) for i in range(n - 2, -1, -1): r[i] = max(r[i + 1], r[i]) ans = 1 for i in range(0, n - 1): ans = max(l[i] + r[i + 1], ans) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys input = sys.stdin.readline t = int(input()) for ii in range(t): n, k = map(int, input().split()) x = [int(i) for i in input().split()] y = [int(i) for i in input().split()] x.sort() i, j = 0, 0 ans, tmp = 0, 1 end, start = 0, 0 store = [] while j < n: if x[j] - x[i] <= k: j += 1 if j < n and x[j] - x[i] <= k: tmp += 1 else: j -= 1 store.append((tmp, i, j)) tmp -= 1 i += 1 store.append((tmp, i, j - 1)) store.sort(reverse=True) start = [0] * (n + 1) for i in range(len(store)): start[store[i][1]] = max(start[store[i][1]], store[i][0]) for i in range(n - 2, -1, -1): start[i] = max(start[i + 1], start[i]) ans = 0 for i in range(n): if start[i] == 0: start[i] = max(0, start[i - 1] - 1) for i in store: ans = max(ans, i[0] + start[i[2] + 1]) sys.stdout.write(str(ans) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) x, y = [int(i) for i in input().split()], [int(i) for i in input().split()] x.sort() x.append(9999999999999999999) t, ans, arr, r = 0, 0, [0] * (n + 5), 0 ll = 0 for i in range(n): while x[r + 1] - x[i] <= k: r = r + 1 can = r - i + 1 arr[i] = can, x[i] + k + 1 while ll <= i and arr[ll][1] < x[i]: t = max([t, arr[ll][0]]) ll = ll + 1 ans = max([ans, can + t]) r = 0 for i in range(n): while x[r + 1] - x[i] <= k * 2 + 1: r = r + 1 ans = max([ans, r - i + 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) l = list(map(int, input().split())) l2 = list(map(int, input().split())) l.sort() l3 = [] l4 = [0] kk = 1 count = 0 c = 0 i = 0 j = 0 ans = 0 while i < n and j < n: if l[j] - l[i] <= k and j >= i: ans = max(ans, j - i + 1) l3.append(ans) j += 1 else: i += 1 i = n - 1 j = n - 1 ans = 0 while i > 0 and j > 0: if l[i] - l[j] <= k and j <= i: ans = max(ans, i - j + 1) l4.append(ans) j -= 1 else: i -= 1 c = len(l3) ans = 0 for i in range(c): ans = max(ans, l3[i] + l4[n - i - 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER 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 VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys input = sys.stdin.readline t = int(input()) for rrr in range(t): n, k = list(map(int, input().split())) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() pl = [] i = 0 j = 0 tmp = [] for j in range(n): if x[j] - x[i] > k: pl.append((i, j - 1)) while i < len(x) and x[j] - x[i] > k: i += 1 if j == n - 1: while i < len(x) and x[j] - x[i] > k: i += 1 pl.append((i, j)) rr = list(pl) rr.sort(key=lambda x: x[1]) if len(x) < 3: print(len(x)) else: best = 0 ans = 0 j = 0 for i in range(len(pl)): l, r = pl[i][0], pl[i][1] if l > rr[j][1]: best = max(best, rr[j][1] - rr[j][0] + 1) if l <= rr[j][1]: ans = max(ans, r - rr[j][0] + 1) ans = max(ans, r - l + 1 + best) while rr[j][1] < l: if l > rr[j][1]: best = max(best, rr[j][1] - rr[j][0] + 1) if l <= rr[j][1]: ans = max(ans, r - rr[j][0] + 1) ans = max(ans, r - l + 1 + best) j += 1 if l > rr[j][1]: best = max(best, rr[j][1] - rr[j][0] + 1) if l <= rr[j][1]: ans = max(ans, r - rr[j][0] + 1) ans = max(ans, r - l + 1 + best) if False: if rrr == 251: print(n, k) print(pl) print(rr) print(x) print(ans) else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR WHILE VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for i in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) input() a.sort() dp = [0] * n dp[0] = 1 i = 1 while i < n and a[i] - a[0] <= k: dp[i] = i + 1 i += 1 l = i t = 0 while i < n: while a[i] - a[t] > k: t += 1 dp[i] = max(dp[i - 1], i - t + 1) i += 1 if l == n: print(len(a)) else: s = 0 t = 0 while l < n: while a[l] - a[t] > k: t += 1 s = max(dp[t - 1] + l - t + 1, s) l += 1 print(s)
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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for i in range(t): n, k = list(map(int, input().split())) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() num = x[0] for j in range(n): x[j] -= num j = 0 while j < n: if x[j] <= k: j += 1 else: break if j == n or x[-1] - x[0] <= 2 * k or n == 1: print(n) else: r = [j] p = j - 1 for j in range(1, n): while p < n: temp = x[p] - x[j] if temp <= k: p += 1 else: break r.append(min(p, n) - j) p -= 1 l = [] q = 0 for j in range(n): while x[j] - x[q] > k: q += 1 l.append(j - q + 1) pref = [l[0]] suff = [0] * n suff[-1] = r[-1] for j in range(1, n): pref.append(max(pref[-1], l[j])) suff[-j - 1] = max(suff[-j], r[-j - 1]) ans = 0 for j in range(n - 1): ans = max(ans, pref[j] + suff[j + 1]) 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, p = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = input() a.sort() b = [] c = [] j = 0 k = 0 if n <= 2: print(n) continue for i in range(n): while a[i] - a[j] > p: j += 1 while k < n and a[k] - a[i] <= p: k += 1 k -= 1 b.append(i - j + 1) c.append(k - i + 1) for i in range(1, n - 1): b[i] = max(b[i], b[i - 1]) c[n - i - 1] = max(c[n - i - 1], c[n - i]) ss = 0 for i in range(n - 1): ss = max(ss, b[i] + c[i + 1]) print(ss)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = [int(zz) for zz in input().split()] x = [int(zz) for zz in input().split()] y = [int(zz) for zz in input().split()] x.sort() l = x[0] r = x[-1] if k * 2 + 1 >= r - l: print(n) continue else: cnts = [(-1) for __ in range(len(x))] j = 0 for i in range(len(x)): while x[i] - x[j] > k: j += 1 cnts[i] = i - j + 1 mr = [(0) for __ in range(len(x))] for i in range(len(x) - 1, -1, -1): mr[i] = cnts[i] if i + 1 < len(x): mr[i] = max(mr[i], mr[i + 1]) ans = 1 j = 0 for i in range(len(x)): while j < len(x) and x[j] - x[i] <= k: j += 1 if j == len(x): break ans = max(ans, cnts[i] + mr[j]) print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() l = [1] r = [1] left = 0 right = n - 1 for pos in range(1, n): while x[left] < x[pos] - k: left += 1 l.append(max(l[-1], pos - left + 1)) pos = 0 for pos in range(n - 2, -1, -1): while x[right] > x[pos] + k: right -= 1 r.append(max(r[-1], right - pos + 1)) r.reverse() ans = 1 for i in range(n - 1): ans = max(l[i] + r[i + 1], ans) 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin, stdout def two_platforms(n, k, x_a): x_a.sort() la = [0] * n ra = [0] * n hi = 0 mx = 0 for i in range(n): while hi < i and x_a[i] - x_a[hi] > k: hi += 1 mx = max(mx, i - hi + 1) la[i] = mx tj = n - 1 mx = 0 for j in range(n - 1, -1, -1): while tj > j and x_a[tj] - x_a[j] > k: tj -= 1 mx = max(mx, tj - j + 1) ra[j] = mx ans = ra[0] for i in range(0, n - 1): ans = max(ans, la[i] + ra[i + 1]) return ans t = int(stdin.readline()) for num in range(t): n, k = map(int, stdin.readline().split()) x_a = list(map(int, stdin.readline().split())) y_a = list(map(int, stdin.readline().split())) ans = two_platforms(n, k, x_a) stdout.write(str(ans) + "\n")
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin, stdout class SOLVE: def bsearch(self, left, right, j, x, k, pos, cmd): if left > right: return pos mid = (left + right) // 2 if cmd == "rpos": if x[mid] <= x[j] + k: pos = mid return self.bsearch(mid + 1, right, j, x, k, pos, cmd) else: return self.bsearch(left, mid - 1, j, x, k, pos, cmd) elif x[mid] >= x[j] - k: pos = mid return self.bsearch(left, mid - 1, j, x, k, pos, cmd) else: return self.bsearch(mid + 1, right, j, x, k, pos, cmd) def solve(self): R = stdin.readline W = stdout.write ans = [] for i in range(int(input())): n, k = [int(temp) for temp in R().split()] x = [int(temp) for temp in R().split()] y = [int(temp) for temp in R().split()] x.sort() rpos = {} for j in range(n): rpos[j] = self.bsearch(j, n - 1, j, x, k, j, "rpos") rmx = {} rmx[n - 1] = 1 for j in range(n - 2, -1, -1): rmx[j] = max(rmx[j + 1], rpos[j] - j + 1) mx = 1 for j in range(n - 1): mx = max( mx, rpos[j] - j + 1 + (rmx[rpos[j] + 1] if rpos[j] + 1 < n else 0) ) ans += [str(mx)] W("\n".join(ans)) def main(): s = SOLVE() s.solve() main()
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR STRING ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) x_coordinates = list(map(int, input().split())) x_coordinates.sort() input() if n == 1: print(1) continue L = 0 R = 0 length = 0 points = 0 platform_points = [] while True: length = x_coordinates[R] - x_coordinates[L] if length <= k: points = R - L + 1 R += 1 if R >= n: platform_points.append((L, points)) break else: platform_points.append((L, points)) while x_coordinates[R] - x_coordinates[L] > k: L += 1 maximum_points_to_the_right = [1] * n for i, points in platform_points: maximum_points_to_the_right[i] = points for i in range(n - 1, 0, -1): maximum_points_to_the_right[i - 1] = max( maximum_points_to_the_right[i - 1], maximum_points_to_the_right[i] ) for i in range(n - 1): maximum_points_to_the_right[i + 1] = max( maximum_points_to_the_right[i + 1], maximum_points_to_the_right[i] - 1 ) L = n - 1 R = n - 1 length = 0 points = 0 platform_points = [] while True: length = x_coordinates[R] - x_coordinates[L] if length <= k: points = R - L + 1 L -= 1 if L < 0: platform_points.append((R, points)) break else: platform_points.append((R, points)) while x_coordinates[R] - x_coordinates[L] > k: R -= 1 maximum_points_to_the_left = [1] * n for i, points in platform_points: maximum_points_to_the_left[i] = points for i in range(n - 1): maximum_points_to_the_left[i + 1] = max( maximum_points_to_the_left[i + 1], maximum_points_to_the_left[i] ) for i in range(n - 1, 0, -1): maximum_points_to_the_left[i - 1] = max( maximum_points_to_the_left[i - 1], maximum_points_to_the_left[i] - 1 ) s = [] for i in range(n - 1): x = maximum_points_to_the_left[i] + maximum_points_to_the_right[i + 1] s.append(x) print(max(s))
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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin for _ in range(int(stdin.readline())): n, k = map(int, stdin.readline().split()) arr_x = list(map(int, stdin.readline().split())) arr_y = list(map(int, stdin.readline().split())) arr_x.sort() left = [0] * (n + 1) i = 0 j = 0 while i < n: while j <= i and arr_x[i] - arr_x[j] > k: j += 1 left[i + 1] = max(left[i], i - j + 1) i += 1 right = [0] * (n + 1) i = n - 1 j = n - 1 while i >= 0: while j >= i and arr_x[j] - arr_x[i] > k: j -= 1 right[i] = max(right[i + 1], j - i + 1) i -= 1 ans = 0 for i in range(n + 1): ans = max(ans, left[i] + right[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for i in range(int(input())): n, k = list(map(int, input().split())) x = sorted(map(int, input().split())) input() a = [] j = 0 for i in range(n): while j < n and x[j] - x[i] <= k: j += 1 a.append(j - i) c = a[:] a1 = a[:] for i in range(1, n): a[i] = max(a[i], a[i - 1]) for i in range(n - 2, -1, -1): a1[i] = max(a1[i], a1[i + 1]) j = n - 1 l = n - 1 z = 0 for i in range(n - 1, -1, -1): while x[l] - x[i] > k: l -= 1 b = 0 if l != n - 1: b = a1[l + 1] while j >= 0 and x[i - 1] - x[j] <= k: j -= 1 if j >= 0: b = max(b, a[j]) else: z = max(z, l + 1) z = max(z, b + c[i]) print(min(z, n))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) cnt = {} for i in x: cnt[i] = 0 for i in x: cnt[i] += 1 keys, first, last, val = list(cnt.keys()), {}, {}, 0 for i in keys: first[i], last[i] = 0, 0 keys.sort() i, j = 0, 0 while i < len(keys) or j < len(keys): if j < len(keys) and keys[j] <= keys[i] + k: val += cnt[keys[j]] if i < len(keys): first[keys[i]] = max(first[keys[i]], val) if j < len(keys): last[keys[j]] = max(last[keys[j]], val) j += 1 else: val -= cnt[keys[i]] i += 1 if i < len(keys): first[keys[i]] = max(first[keys[i]], val) if j < len(keys): last[keys[j]] = max(last[keys[j]], val) mx, res = 0, first[keys[0]] for i in range(1, len(keys)): mx = max(mx, last[keys[i - 1]]) res = max(res, mx + first[keys[i]]) print(res)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR DICT DICT NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) y = input() a.sort() l = [0] * (n + 1) r = [0] * (n + 1) i = 0 for j in range(n): while a[j] - a[i] > k: i += 1 l[j + 1] = j - i + 1 l[j + 1] = max(l[j], l[j + 1]) i = n - 1 for j in range(n - 1, -1, -1): while a[i] - a[j] > k: i -= 1 r[j] = i - j + 1 r[j] = max(r[j], r[j + 1]) maximum = 0 for i in range(n + 1): maximum = max(maximum, l[i] + r[i]) print(maximum)
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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for t in range(int(input())): n, k = map(int, input().split()) lx = list(map(int, input().split())) ly = list(map(int, input().split())) lx = list(sorted(lx)) ll = [0] pi = 0 for i in range(len(lx)): while lx[i] > lx[pi] + k: pi += 1 ll.append(i - pi + 1) lr = [0] pi = n - 1 for i in range(len(lx) - 1, -1, -1): while lx[pi] > lx[i] + k: pi -= 1 lr.append(pi - i + 1) for i in range(1, len(ll)): ll[i] = max(ll[i], ll[i - 1]) for i in range(1, len(lr)): lr[i] = max(lr[i], lr[i - 1]) lr = lr[::-1] ans = 0 for i in range(len(ll)): ans = max(ans, ll[i] + lr[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = sorted(list(map(int, input().split()))) y = list(map(int, input().split())) mostSaved = [0] * (n + 1) savedFromStart = [0] * n i, j = 0, 0 prevX = -1 while i < n: if x[i] == prevX: savedFromStart[i] = savedFromStart[i - 1] else: prevX = x[i] while j < n and x[j] <= x[i] + k: j += 1 savedFromStart[i] = j - i i += 1 for i in range(n - 1, -1, -1): mostSaved[i] = max(mostSaved[i + 1], savedFromStart[i]) out = [ (savedFromStart[i] + mostSaved[min(n, i + savedFromStart[i])]) for i in range(n) ] print(max(out))
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
h = int(input()) l = [] for i in range(h): n, k = list(map(int, input().split())) x = list(map(int, input().split())) y = list(map(int, input().split())) a = [None] * n b = [None] * n x.sort() j = n - 1 for h in range(n - 1, -1, -1): while x[j] - x[h] > k: j -= 1 a[h] = j - h + 1 if h + 1 < n: a[h] = max(a[h], a[h + 1]) j = 0 for h in range(n): while x[h] - x[j] > k: j += 1 b[h] = h - j + 1 if h > 0: b[h] = max(b[h], b[h - 1]) ans = 1 for h in range(n - 1): ans = max(ans, a[h + 1] + b[h]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
TC = int(input()) for tc in range(TC): N, K = map(int, input().split()) X = list(map(int, input().split())) Y = list(map(int, input().split())) if N < 3: print(N) continue X.sort() l = [(0) for _ in X] r = [(0) for _ in X] j = 0 for i, x in enumerate(X): while x > X[j] + K: j += 1 l[i] = i - j + 1 j = N - 1 for i in range(N - 1, -1, -1): while X[j] > X[i] + K: j -= 1 r[i] = j - i + 1 max_l = l.copy() max_r = r.copy() for i, ml in enumerate(l[1:], 1): max_l[i] = max(ml, max_l[i - 1]) for i in range(N - 2, -1, -1): max_r[i] = max(r[i], max_r[i + 1]) result = 0 for i in range(N - 1): result = max(result, max_l[i] + max_r[i + 1]) print(result)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def is_ok(X, K, i, mid): if X[mid] - X[i] <= K: return True else: return False def binary_search(X, K, i): ng = len(X) ok = i while abs(ok - ng) > 1: mid = ng + (ok - ng) // 2 if is_ok(X, K, i, mid): ok = mid else: ng = mid return ok def is_ok2(X, K, i, mid): if X[mid] < X[i] - K: return True else: return False def binary_search2(X, K, i): ng = i ok = 0 while abs(ok - ng) > 1: mid = ng + (ok - ng) // 2 if is_ok2(X, K, i, mid): ok = mid else: ng = mid return ok def main(): T = int(input()) for _ in range(T): N, K = (int(i) for i in input().split()) X = [int(i) for i in input().split()] _ = [int(i) for i in input().split()] X.sort() count = [0] * N for i in range(N): ret = binary_search(X, K, i) count[i] = ret - i + 1 ans = count[0] count_max = [0] * N count_max[0] = count[0] for i in range(1, N): cur = count[i] if X[i] - X[0] <= K: cur += i else: j = binary_search2(X, K, i) cur += count_max[j] ans = max(ans, cur) count_max[i] = max(count_max[i - 1], count[i]) print(ans) main()
FUNC_DEF IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() ret, ret2 = 1, 0 j, l = 1, 0 for i in range(1, n): while a[i] - a[j] > k: j += 1 while a[j - 1] - a[l] > k: l += 1 ret2 = max(ret2, j - l) ret2 = max(ret2, j - l) ret = max(ret, i - j + 1 + ret2) print(ret)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] a.sort() input() l = [] r = [0] * n j = 0 for i in range(n): while a[i] - a[j] > k: j += 1 l.append(i - j + 1) j = n - 1 for i in range(n - 1, -1, -1): while a[j] - a[i] > k: j -= 1 r[i] = j - i + 1 ml = [l[0]] * n for i in range(1, n): ml[i] = max(l[i], ml[i - 1]) mr = [r[-1]] * n for i in range(n - 2, -1, -1): mr[i] = max(r[i], mr[i + 1]) ans = 0 for i in range(n - 1): ans = max(ans, ml[i] + mr[i + 1]) if n == 1: print(1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin def inp(): return stdin.buffer.readline().rstrip().decode("utf8") def itg(): return int(stdin.buffer.readline()) def mpint(): return map(int, stdin.buffer.readline().split()) for __ in range(itg()): n, k = mpint() arr = list(mpint()) arr.sort() inp() left = [0] * n j = 0 for i in range(n): while arr[i] - arr[j] > k: j += 1 left[i] = i - j + 1 right = [0] * n j = n - 1 for i in range(n - 1, -1, -1): while arr[j] - arr[i] > k: j -= 1 right[i] = j - i + 1 for i in range(1, n): left[i] = max(left[i], left[i - 1]) for i in range(n - 2, -1, -1): right[i] = max(right[i], right[i + 1]) ans = left[-1] for i in range(n - 1): ans = max(ans, left[i] + right[i + 1]) print(ans)
FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys class ETwoPlatforms: def solve(self, tc=0): for _ in range(int(input())): n, k = [int(_) for _ in input().split()] x = [int(_) for _ in input().split()] y = [int(_) for _ in input().split()] x.sort() ans = 0 s, e = 0, 1 mx = 1 n = len(x) pre = [0] * (n + 1) pre[1] = 1 last = -1 while e < n: while e < n and x[e] - x[s] <= k: pre[e] = max(pre[last], e - s) last = e e += 1 pre[e] = max(pre[last], e - s) last = e s += 1 post = [0] * n post[-1] = 1 s, e = n - 1, n - 2 last = 0 while e >= 0: while e >= 0 and x[s] - x[e] <= k: post[e] = max(post[last], s - e + 1) last = e e -= 1 e += 1 post[e] = max(post[last], s - e + 1) last = e e -= 1 s -= 1 ans = 0 for i in range(n): ans = max(ans, pre[i] + post[i]) ans = max(ans, pre[n]) print(ans) solver = ETwoPlatforms() input = sys.stdin.readline solver.solve()
IMPORT CLASS_DEF FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
z = lambda: map(int, input().split()) r = range p = print for _ in r(int(input())): n, k = z() l = sorted(list(z())) z() if n < 3: p(n) continue a = i = 0 b = m = e = o = 1 f = 2 c = [0, 1] d = [1] while a < n: while b < n and l[b] <= l[a] + k: b += 1 m = max(m, b - a) c += [m] a += 1 while e < n + 1: while f < n + 1 and l[-f] >= l[-e] - k: f += 1 o = max(o, f - e) d += [o] e += 1 p(max(c[i] + d[-i - 1] for i in r(n)))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR LIST VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) for i in range(t): n, k = map(int, input().split()) xs = map(int, input().split()) ys = map(int, input().split()) points_at_x = dict() for x in xs: points_at_x[x] = points_at_x.get(x, 0) + 1 points = sorted(points_at_x.keys()) m = len(points) saved = [0] * m p_ends = [m] * m start = 0 end = 0 while start < m and start <= end and end < m + 1: if end < m and points[end] - points[start] <= k: saved[start] += points_at_x[points[end]] end += 1 else: p_ends[start] = end start += 1 if start < m: saved[start] = saved[start - 1] - points_at_x[points[start - 1]] max_saved = [0] * m max_saved[m - 1] = saved[m - 1] for j in range(m - 1): max_saved[m - 2 - j] = max(max_saved[m - 1 - j], saved[m - 2 - j]) result = 0 for j in range(m): temp = saved[j] temp2 = p_ends[j] if temp2 < m: temp += max_saved[temp2] result = max(result, temp) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
tc = int(input()) for _ in range(tc): n, k = map(int, input().split(" ")) x = sorted(list(map(int, input().split(" ")))) input() left = [1] * n l = 0 for r in range(1, n): while x[r] - x[l] > k: l += 1 left[r] = r - l + 1 left[r] = max(left[r], left[r - 1]) right = [1] * n r = n - 1 for l in reversed(range(n - 1)): while x[r] - x[l] > k: r -= 1 right[l] = r - l + 1 right[l] = max(right[l], right[l + 1]) answer = 1 for i in range(n - 1): answer = max(answer, left[i] + right[i + 1]) print(answer)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
tc = int(input()) for _ in range(tc): n, k = map(int, input().split()) x = list(map(int, input().split())) input() if n == 1: print(1) continue x.sort() j = 0 a = [] for i in range(n): while j + 1 < n and x[j + 1] - x[i] <= k: j += 1 a.append(j - i + 1) for i in range(n - 2, -1, -1): a[i] = max(a[i], a[i + 1]) j = n - 1 b = [] for i in range(n - 1, -1, -1): while j - 1 >= 0 and x[i] - x[j - 1] <= k: j -= 1 b.append(i - j + 1) b.reverse() for i in range(1, n): b[i] = max(b[i], b[i - 1]) print(max(a[i + 1] + b[i] for i in range(n - 1)))
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys input = sys.stdin.buffer.readline def print(val): sys.stdout.write(str(val) + "\n") def prog(): for _ in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() most_at_point = [0] * n most_after_point = [0] * (n + 1) tot = 0 j = -1 for i in range(n): while j + 1 < n and x[j + 1] <= x[i] + k: j += 1 tot += 1 most_at_point[i] = tot tot -= 1 for i in range(n - 1, -1, -1): most_after_point[i] = max(most_after_point[i + 1], most_at_point[i]) best = 0 j = 0 for i in range(n): while j < n and x[j] <= x[i] + k: j += 1 if j == n: best = max(best, most_at_point[i]) else: best = max(best, most_at_point[i] + most_after_point[j]) print(best) prog()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) x = sorted(map(int, input().split())) input() j = n - 1 r = [-1] * n l = [-1] * n for i in range(n - 1, -1, -1): if x[j] - x[i] > k: j = j - 1 r[i] = j - i + 1 if i < n - 1: r[i] = max(r[i], r[i + 1]) j = 0 for i in range(0, n, 1): if x[i] - x[j] > k: j = j + 1 l[i] = i - j + 1 if i > 0: l[i] = max(l[i], l[i - 1]) ans = 1 for i in range(n - 1): ans = max(ans, l[i] + r[i + 1]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
t = int(input()) while t > 0: n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() prefix = [0] * (n + 2) suffix = [0] * (n + 2) i = 1 j = 1 while i <= n and j <= n: diff = x[j - 1] - x[i - 1] if diff <= k: prefix[j] = max(j - i + 1, prefix[j - 1]) j += 1 else: i += 1 i = n j = n while i >= 0 and j >= 0: diff = abs(x[j - 1] - x[i - 1]) if diff <= k: suffix[i] = max(abs(j - i + 1), suffix[i + 1]) i -= 1 else: j -= 1 max_points = 0 for i in range(1, n + 1): max_points = max(max_points, prefix[i] + suffix[i + 1]) print(max_points) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
def solve(n, k, x, y): x = sorted(x) l = [0] * n r = [0] * n ptr = n - 1 for i in range(n - 1, -1, -1): while x[ptr] - x[i] > k: ptr -= 1 r[i] = ptr - i + 1 if i + 1 < n: r[i] = max(r[i], r[i + 1]) ptr = 0 for i in range(n): while x[i] - x[ptr] > k: ptr += 1 l[i] = i - ptr + 1 if i > 0: l[i] = max(l[i], l[i - 1]) ans = 1 for i in range(n - 1): ans = max(ans, l[i] + r[i + 1]) print(ans) t = int(input()) for _ in range(t): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) solve(n, k, x, y)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for t in range(int(input())): n, k = map(int, input().split()) x = [int(i) for i in input().split()] y = input() x.sort() mx = [] price = [] cur = 0 ptr = 0 for p, i in enumerate(x): while ptr < len(x) and x[ptr] - i <= k: cur += 1 ptr += 1 price.append(cur) cur -= 1 cur = 0 for p, i in enumerate(price): cur = max(cur, i) mx.append(cur) ans = 0 ptr = 0 for p, i in enumerate(price): while x[p] - x[ptr] > k: ptr += 1 if ptr != 0: ans = max(ans, mx[ptr - 1] + i) else: ans = max(ans, i) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for _ in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() left, right = [0] * n, [0] * n first = 0 for i in range(n): while first < n and x[first] - x[i] <= k: first += 1 right[i] = first - i second = n - 1 for i in range(n - 1, -1, -1): while second >= 0 and x[i] - x[second] <= k: second -= 1 left[i] = i - second mx, ans = 0, 0 for i in range(n): ans = max(ans, mx + right[i]) mx = max(mx, left[i]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n, k = list(map(int, input().split())) x = list(map(int, input().split())) y = list(map(int, input().split())) x.sort() take = n - 1 suffix = [0] * n for i in range(n - 1, -1, -1): while x[i] + k < x[take]: take -= 1 suffix[i] = take - i + 1 mx = 0 for i in range(n - 1, -1, -1): mx = max(suffix[i], mx) suffix[i] = mx ans = 1 take = 0 for i in range(n - 1): while x[i] - k > x[take]: take += 1 temp = i - take + 1 ans = max(ans, temp + suffix[i + 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n, k = map(int, stdin.readline().split()) x = list(map(int, stdin.readline().split())) y = stdin.readline() if n == 1: print(1) continue endi = [1] * n starti = [1] * n x.sort() curri = 0 i = 1 while i < n: if x[i] - x[curri] < k: endi[i] = i - curri + 1 i += 1 elif x[i] - x[curri] == k: endi[i] = i - curri + 1 i += 1 else: curri += 1 for i in range(1, n): endi[i] = max(endi[i], endi[i - 1]) curri = n - 1 i = n - 2 while i >= 0: if x[curri] - x[i] < k: starti[i] = curri - i + 1 i -= 1 elif x[curri] - x[i] == k: starti[i] = curri - i + 1 i -= 1 else: curri -= 1 for i in range(n - 2, -1, -1): starti[i] = max(starti[i], starti[i + 1]) maxi = x.count(x[0]) for i in range(n - 1): s = endi[i] + starti[i + 1] if s > maxi: maxi = s print(maxi)
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 IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ points on a plane. The $i$-th point has coordinates $(x_i, y_i)$. You have two horizontal platforms, both of length $k$. Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same $y$-coordinate) and have integer borders. If the left border of the platform is $(x, y)$ then the right border is $(x + k, y)$ and all points between borders (including borders) belong to the platform. Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same $y$-coordinate. When you place both platforms on a plane, all points start falling down decreasing their $y$-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost. Your task is to find the maximum number of points you can save if you place both platforms optimally. You have to answer $t$ independent test cases. For better understanding, please read the Note section below to see a picture for the first test case. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 2 \cdot 10^5$; $1 \le k \le 10^9$) β€” the number of points and the length of each platform, respectively. The second line of the test case contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$), where $x_i$ is $x$-coordinate of the $i$-th point. The third line of the input contains $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$), where $y_i$ is $y$-coordinate of the $i$-th point. All points are distinct (there is no pair $1 \le i < j \le n$ such that $x_i = x_j$ and $y_i = y_j$). It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). -----Output----- For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally. -----Example----- Input 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 Output 6 1 5 10 -----Note----- The picture corresponding to the first test case of the example: [Image] Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points $(1, -1)$ and $(2, -1)$ and the second one between points $(4, 3)$ and $(5, 3)$. Vectors represent how the points will fall down. As you can see, the only point we can't save is the point $(3, 7)$ so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point $(5, 3)$ doesn't fall at all because it is already on the platform.
for t in range(int(input())): n, k = map(int, input().split()) xs = sorted(list(map(int, input().split()))) input() best = 0 i = j = 0 while j < n: while j < n and xs[j] <= xs[i] + k: j += 1 if best < j - i: best = j - i best_start = i best_end = j i += 1 i = j = 0 best2 = 0 while j < best_start: while j < best_start and xs[j] <= xs[i] + k: j += 1 if best2 < j - i: best2 = j - i i += 1 i = j = best_end while j < n: while j < n and xs[j] <= xs[i] + k: j += 1 if best2 < j - i: best2 = j - i i += 1 best += best2 j = best_start while j > -1 and xs[j] + k >= xs[best_start]: j -= 1 last = best_start - j goodies = [(best_start, last)] if j == -1: j = 0 for i in range(best_start + 1, best_end - 1): while xs[j] + k < xs[i]: j += 1 if i - j + 1 > last: last = i - j + 1 goodies.append((i, last)) j = best_end - 1 best2 = 0 while j < n and xs[j] + k >= xs[best_end - 1]: j += 1 if j == n: j = n - 1 for i in range(best_end - 1, best_start, -1): while xs[i] + k < xs[j]: j -= 1 if i == goodies[-1][0]: goodies.pop() if j - i + 1 + goodies[-1][1] > best2: best2 = j - i + 1 + goodies[-1][1] print(max(best, best2))
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) while t != 0: n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) one = [] two = [] for i in range(n): if b[i] == 1: one.append(a[i]) else: two.append(a[i]) one.sort() two.sort() ans = 0 while len(one) > 0 and len(two) > 0 and m > 0: if one[-1] >= m: ans += 1 m -= one[-1] one.pop() break elif m <= two[-1]: ans += 2 m -= two[-1] two.pop() break elif len(one) == 1: ans += 2 m -= two[-1] two.pop() elif two[-1] > one[-1] + one[-2]: ans += 2 m -= two[-1] two.pop() else: m -= one[-1] ans += 1 one.pop() if m <= 0: print(ans) else: while len(one) > 0 and m > 0: ans += 1 m -= one[-1] one.pop() if m <= 0: print(ans) else: while len(two) > 0 and m > 0: ans += 2 m -= two[-1] two.pop() if m <= 0: print(ans) else: print(-1) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys N = int(200000.0 + 5) sys.setrecursionlimit(N) def charming(): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = list(zip(a, b)) c.sort(key=lambda x: x[0] / x[1], reverse=True) res = 0 last = 0 sum = 0 for i in range(n): if sum >= m: break sum += c[i][0] res += c[i][1] last = i if sum < m: print(-1) return if c[last][1] == 1: print(res) return for i in range(last + 1, n): if c[i][1] == 1 and sum - c[last][0] + c[i][0] >= m: print(res - 1) return for i in range(last): if c[i][1] == 1 and sum - c[i][0] >= m: print(res - 1) return print(res) for t in range(int(input())): charming()
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR 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 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 VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) Q = sum(a) - m if Q < 0: print(-1) else: l1 = sorted([x for i, x in enumerate(a) if b[i] == 1]) l2 = sorted([x for i, x in enumerate(a) if b[i] == 2]) for i in range(1, len(l1)): l1[i] += l1[i - 1] for i in range(1, len(l2)): l2[i] += l2[i - 1] opt = 0 p = 0 j = len(l1) - 1 for i in range(len(l2) + 1): c = 2 * i if p > Q: break while j >= 0 and l1[j] > Q - p: j -= 1 c += j + 1 if c > opt: opt = c if i < len(l2): p = l2[i] print(sum(b) - opt)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR WHILE VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def clean(apps, free, app_mem, points): if sum(app_mem) < free: return -1 elif sum(app_mem) == free: return sum(points) else: s2, s1 = 0, 0 mem1, mem2 = [], [] for i in range(apps): if points[i] == 1: mem1.append(app_mem[i]) else: mem2.append(app_mem[i]) s2 += app_mem[i] mem1.sort(reverse=True), mem2.sort(reverse=True) ans = float("inf") k = len(mem2) - 1 for j in range(len(mem1) + 1): while k >= 0 and s2 - mem2[k] + s1 >= free: s2 -= mem2[k] k -= 1 if s1 + s2 >= free: ans = min(ans, 2 * (k + 1) + j) if j < len(mem1): s1 += mem1[j] return ans for test in range(int(input())): n, m = map(int, input().split()) memory = list(map(int, input().split())) pts = list(map(int, input().split())) print(clean(n, m, memory, pts))
FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR 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 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 VAR VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, m = map(int, input().rsplit()) gen_a = map(int, input().rsplit()) gen_b = map(int, input().rsplit()) a_1, a_2 = [0], [0] total_m = 0 for i, j in zip(gen_a, gen_b): if j == 1: a_1.append(i) else: a_2.append(i) total_m += i if total_m < m: print(-1) continue a_1 = sorted(a_1) a_2 = sorted(a_2) sum_m, sum_r = 0, 0 l, r = len(a_1) - 1, len(a_2) - 1 while l + r > 0: if a_1[l] >= m - sum_m: sum_m += a_1[l] sum_r += 1 break if a_2[r] >= m - sum_m: sum_m += a_2[r] sum_r += 2 break if r <= 0 and l > 0: sum_m += a_1[l] sum_r += 1 l -= 1 elif l <= 0 and r > 0: sum_m += a_2[r] sum_r += 2 r -= 1 elif a_1[l] + a_1[l - 1] > a_2[r]: sum_m += a_1[l] sum_r += 1 l -= 1 else: sum_m += a_2[r] sum_r += 2 r -= 1 if sum_m >= m: break print(sum_r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = [] y = [] for i in range(n): if b[i] == 1: x.append(a[i]) else: y.append(a[i]) x.sort(reverse=True) y.sort(reverse=True) t, cost = 0, 0 p2 = -1 ans = 210000000000 while p2 + 1 < len(y) and t < m: p2 += 1 t += y[p2] cost += 2 if t >= m: ans = min(ans, cost) for i in range(len(x)): t += x[i] cost += 1 while p2 >= 0 and t - y[p2] >= m: t -= y[p2] p2 -= 1 cost -= 2 if t >= m: ans = min(ans, cost) if ans >= 210000000000: print(-1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
from sys import stderr def dbp(*args, **kwargs): pass def get_int_list(): return [int(i) for i in input().strip().split()] def do_thing(): N, freeme = get_int_list() mlist = get_int_list() clist = get_int_list() if sum(mlist) < freeme: return -1 c1list = [] c2list = [] rsum = 0 for i in range(N): if clist[i] == 1: c1list.append(mlist[i]) else: t = mlist[i] c2list.append(t) rsum += t c1list.sort(reverse=True) c2list.sort(reverse=True) dbp("target:", freeme) dbp(c1list) dbp(c2list) lsum = 0 r = len(c2list) bcc = float("inf") for l in range(len(c1list) + 1): dbp("before:", l, lsum, r, rsum, bcc) while r > 0 and lsum + rsum - c2list[r - 1] >= freeme: r -= 1 rsum -= c2list[r] if lsum + rsum >= freeme: cost = 2 * r + l dbp(l, lsum, r, rsum, "goodcost:", cost) bcc = min(cost, bcc) if l < len(c1list): lsum += c1list[l] dbp("after:", l, lsum, r, rsum, bcc) return bcc if bcc != float("inf") else -1 maxcc = int(input().strip()) for cc in range(maxcc): print(do_thing())
FUNC_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR VAR WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n, k = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] c = [] ones = [0] for i in range(n): c.append((a[i] / b[i], b[i])) if b[i] == 1: ones.append(a[i]) ones.sort() ones = ones[::-1] if max(ones) >= k: print(1) continue if len(ones) == n + 1: s = 0 if sum(ones) < k: print(-1) continue for i in range(n): s += ones[i] if s >= k: print(i + 1) break continue c.sort() c = c[::-1] d = [] temp = [c[0]] for i in range(n - 1): if c[i][0] == c[i + 1][0]: temp.append(c[i + 1]) else: d.extend(temp[::-1]) temp = [c[i + 1]] d.extend(temp[::-1]) c = d ans = 0 temp = 0 flag = False temp_list = [] x = 0 for i in range(n): if ones[x] >= k - temp: ans += 1 flag = True break ans += c[i][1] temp += c[i][0] * c[i][1] if c[i][1] == 1: x += 1 temp_list.append(c[i]) if temp >= k: flag = True break if not flag: print(-1) continue temp_list = temp_list[::-1] for i in range(len(temp_list)): if temp - c[i][0] * c[i][1] >= k: temp -= c[i][0] ans -= c[i][1] print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def search(arr, key): l = 0 r = len(arr) - 1 while l <= r: m = (l + r) // 2 if arr[m] >= key: r = m - 1 else: l = m + 1 return l t = int(input()) while t > 0: t -= 1 n, m = map(int, input().strip().split()) a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) one = [] two = [] for i in range(n): if b[i] == 1: one.append(a[i]) else: two.append(a[i]) one.sort(reverse=True) two.sort(reverse=True) for i in range(1, len(one)): one[i] += one[i - 1] for i in range(1, len(two)): two[i] += two[i - 1] if len(two) == 0: if one[len(one) - 1] >= m: print(search(one, m) + 1) else: print(-1) elif len(one) == 0: if two[len(two) - 1] >= m: print(search(two, m) * 2 + 2) else: print(-1) elif one[len(one) - 1] + two[len(two) - 1] < m: print(-1) else: ans = 1000000000000000000 if one[len(one) - 1] >= m: ans = search(one, m) + 1 if two[len(two) - 1] >= m: ans = min(ans, search(two, m) * 2 + 2) for i in range(len(one)): if one[i] + two[len(two) - 1] >= m: tmp = i + 1 if one[i] < m: tmp += search(two, m - one[i]) * 2 + 2 else: ans = min(ans, tmp) break ans = min(ans, tmp) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a1 = [10000000000] a2 = [10000000000] for i in range(n): if b[i] == 1: a1.append(a[i]) else: a2.append(a[i]) a1.sort(reverse=True) a2.sort(reverse=True) a1[0] = 0 a2[0] = 0 for i in range(1, len(a1)): a1[i] += a1[i - 1] am = False c = float("inf") for i in range(len(a2)): if i >= 1: a2[i] += a2[i - 1] l = -1 r = len(a1) h = m - a2[i] while r > l + 1: mid = (l + r) // 2 if h > a1[mid]: l = mid else: r = mid if r != len(a1): am = True c = min(c, 2 * i + r) if am: print(c) else: print(-1)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
R = lambda: list(map(int, input().split())) def bs(a, k): l = 0 r = len(a) - 1 while l < r: m = l + (r - l) // 2 if a[m] < k: l = m + 1 else: r = m m = (l + r) // 2 return m for _ in range(int(input())): n, m = R() a = R() b = R() if sum(a) < m: print(-1) continue reg, imp = [], [] for i in range(n): if b[i] == 1: reg.append(a[i]) else: imp.append(a[i]) reg = sorted(reg)[::-1] imp = sorted(imp)[::-1] pi = [0] for i in range(len(imp)): pi.append(pi[-1] + imp[i]) mr = 0 mi = bs(pi, m) ans = 10**9 if pi[mi] >= m: ans = mi * 2 for i in range(len(reg)): mr += reg[i] lef = m - mr mi = bs(pi, lef) if pi[mi] + mr >= m: ans = min(ans, mi * 2 + i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def solve(): inp = input().rstrip().split(" ") n, m = int(inp[0]), int(inp[1]) a_arr = input().rstrip().split(" ") b_arr = input().rstrip().split(" ") sum_reg = 0 sum_imp = 0 reg_apps = [] imp_apps = [] for i in range(n): if b_arr[i] == "1": reg_apps.append(int(a_arr[i])) sum_reg += int(a_arr[i]) else: imp_apps.append(int(a_arr[i])) sum_imp += int(a_arr[i]) reg_apps.sort() imp_apps.sort() if sum_reg + sum_imp < m: print(-1) return tot_removed = 0 min_points_lost = 0 while sum_reg < m - tot_removed: tot_removed += imp_apps.pop() min_points_lost += 2 while tot_removed < m and len(reg_apps) + len(imp_apps) > 0: if len(reg_apps) == 0: tot_removed += imp_apps.pop() min_points_lost += 2 elif len(imp_apps) == 0: tot_removed += reg_apps.pop() min_points_lost += 1 elif reg_apps[-1] >= imp_apps[-1]: tot_removed += reg_apps.pop() min_points_lost += 1 elif tot_removed + reg_apps[-1] >= m: tot_removed += reg_apps.pop() min_points_lost += 1 elif len(reg_apps) > 1 and reg_apps[-1] + reg_apps[-2] > imp_apps[-1]: tot_removed += reg_apps.pop() tot_removed += reg_apps.pop() min_points_lost += 2 else: tot_removed += imp_apps.pop() min_points_lost += 2 print(min_points_lost) def main(): T = int(input()) for c in range(T): solve() main()
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
from itertools import repeat def line(): return map(int, input().split()) def num(): return int(input()) def nfunc(f, n, *args, **kwargs): return (f(*args, **kwargs) for _ in repeat(None, n)) t = num() for _ in repeat(None, t): n, m = line() mems = list(line()) uses = list(line()) norms = sorted(mem for mem, use in zip(mems, uses) if use == 1)[::-1] imps = sorted(mem for mem, use in zip(mems, uses) if use == 2)[::-1] nn, ni = len(norms), len(imps) ans = 0 i, j = 0, 0 while m > 0: if i < nn - 1 and j < ni: if m > norms[i] and norms[i] + norms[i + 1] < imps[j]: ans += 2 m -= imps[j] j += 1 else: ans += 1 m -= norms[i] i += 1 elif i == nn - 1 and j < ni: if m > norms[i] and norms[i] < imps[j]: ans += 2 m -= imps[j] j += 1 else: ans += 1 m -= norms[i] i += 1 elif i < nn: ans += 1 m -= norms[i] i += 1 elif j < ni: ans += 2 m -= imps[j] j += 1 else: break if m > 0: print(-1) else: print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NONE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NONE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def dell(mlst, imp, m): n = len(mlst) cc = [] for i in range(n): cc.append((mlst[i] / imp[i], i)) cc.sort() sm = lose = 0 i = place2 = place = place1 = -1 for i in range(n - 1, -1, -1): place = cc[i][1] sm += mlst[place] lose += imp[place] if imp[place] == 2: place2 = place else: place1 = place if sm >= m: break if sm < m: return -1 if place == place2: if place1 != -1 and sm - mlst[place1] >= m: return lose - 1 i -= 1 while i >= 0 and imp[cc[i][1]] == 2: i -= 1 if i >= 0 and sm - mlst[place2] + mlst[cc[i][1]] >= m: return lose - 1 return lose t = int(input()) for u in range(t): mm = input().split() ml = list(map(int, input().split())) imp = list(map(int, input().split())) print(dell(ml, imp, int(mm[1])))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def bisect(b, m): l, h, ind = 0, len(b) - 1, -1 while l <= h: mid = (l + h) // 2 if b[mid] >= m: ind = mid h = mid - 1 else: l = mid + 1 return ind def answer(m): c1, c2 = [0], [] for i in range(n): if b[i] == 1: c1.append(a[i]) else: c2.append(a[i]) c1.sort(reverse=True) c2.sort(reverse=True) c1.insert(0, 0) i, j, ans = 0, 0, 0 for i in range(1, len(c2)): c2[i] += c2[i - 1] ans = 10**8 for i in range(len(c1)): m -= c1[i] if m <= 0: ans = min(ans, i) break val = bisect(c2, m) if val != -1: ans = min(ans, i + (val + 1) * 2) if ans == 10**8: return -1 else: return ans for T in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) print(answer(m))
FUNC_DEF ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR LIST NUMBER LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP NUMBER NUMBER RETURN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
from sys import stdin input = stdin.buffer.readline t = int(input()) for i in range(t): n, m = map(int, input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] ones = [] twos = [] for i in range(n): if b[i] == 1: ones.append(a[i]) else: twos.append(a[i]) ones.sort(reverse=True) twos.sort(reverse=True) x = 0 for i in range(len(ones)): x = x + ones[i] ones[i] = x x = 0 for i in range(len(twos)): x = x + twos[i] twos[i] = x s = x ans = 10**8 ones = [0] + ones for i in range(len(ones)): x = m - ones[i] if x <= 0: ans = min(ans, i) continue if x > s: continue start = 0 end = len(twos) - 1 pos = -1 while start <= end: mid = (start + end) // 2 if twos[mid] >= x: pos = mid end = mid - 1 else: start = mid + 1 ans = min(ans, i + (pos + 1) * 2) if ans == 10**8: print(-1) else: print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR 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 IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def readInt(): return int(input()) def readInts(): return [int(x) for x in input().split()] def readBin(): return [int(x) for x in readString()] def readString(): return input().rstrip() def readCase(): return readString() def solve(n, m, usage, convenience): normal = [] high = [] max_loss = 2 * n + 1 for x in range(n): if convenience[x] == 1: normal.append(usage[x]) else: high.append(usage[x]) normal.sort(reverse=True) high.sort(reverse=True) loss = 0 normalPos = 0 highPos = 0 while m > 0 and normalPos < len(normal): m -= normal[normalPos] normalPos += 1 loss += 1 best_loss = max_loss if m <= 0: best_loss = loss normalPos -= 1 while highPos < len(high): m -= high[highPos] highPos += 1 loss += 2 while normalPos >= 0: if m + normal[normalPos] <= 0: m += normal[normalPos] normalPos -= 1 loss -= 1 else: break if m <= 0 and loss < best_loss: best_loss = loss if best_loss == max_loss: return -1 else: return best_loss cases = readInt() for case in range(cases): n, m = readInts() usage = readInts() convenience = readInts() print(solve(n, m, usage, convenience))
FUNC_DEF RETURN FUNC_CALL VAR 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 VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for t in range(int(input())): n, m = [int(i) for i in input().split()] d = [int(i) for i in input().split()] dff = [int(i) for i in input().split()] if sum(d) < m: print(-1) continue d1 = [d[i] for i in range(n) if dff[i] == 1] d2 = [d[i] for i in range(n) if dff[i] == 2] ld1 = len(d1) ld2 = len(d2) d1.sort(reverse=True) d2.sort(reverse=True) p1 = 0 p2 = 0 ans = 0 while m > 0: if p1 == ld1 - 1: while m > d1[p1]: ans += 2 m -= d2[p2] p2 += 1 if m <= 0: break else: ans += 1 m -= d1[p1] break opt1 = 0 if p1 >= ld1 - 1 else d1[p1] + d1[p1 + 1] opt2 = 0 if p2 == ld2 else d2[p2] if opt2 > opt1: if opt2 > m: break ans += 2 p2 += 1 m -= opt2 else: if opt1 > m: break ans += 1 m -= d1[p1] p1 += 1 if m > 0: opt0 = 0 if p1 == ld1 else d1[p1] if opt0 >= m: ans += 1 else: ans += 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
from sys import stdin for _ in range(int(input())): n, m = map(int, stdin.readline().strip().split()) a = list(map(int, stdin.readline().strip().split())) b = list(map(int, stdin.readline().strip().split())) s = sum(a) sb = sum(b) if s < m: print(-1) elif s == m: print(sb) else: two, one = [], [] for i in range(n): if b[i] == 1: one.append(a[i]) else: two.append(a[i]) one.sort(reverse=True) two.sort(reverse=True) lo, lt = len(one), len(two) s1, s2 = [0] * lo, [0] * lt for i in range(lo): if i == 0: s1[i] = one[i] else: s1[i] = s1[i - 1] + one[i] for i in range(lt): if i == 0: s2[i] = two[0] else: s2[i] = s2[i - 1] + two[i] lost = sb if lt == 0: for i in range(lo): if s1[i] >= m: lost = i + 1 break else: for i in range(lo): if s1[i] < m: val = s1[i] if val + s2[-1] < m: continue temp = i + 1 for j in range(lt): temp += 2 if val + s2[j] >= m: break lost = min(lost, temp) else: lost = min(lost, i + 1) break for i in range(lt): if s2[i] >= m: lost = min(lost, 2 * (i + 1)) break print(lost)
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 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
class app: a = 0 b = 0 def __init__(self, a, b): self.a = a self.b = b def cmp(app): return app.a def solve(): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr = [] for i in range(n): arr.append(app(a[i], b[i])) arr = sorted(arr, key=cmp) sum = 0 msum = 0 twos = [] ones = [] for i in range(n - 1, -1, -1): if sum < m: sum += arr[i].a msum += arr[i].b if arr[i].b == 2: twos.append(arr[i].a) elif arr[i].b == 1: ones.append(arr[i].a) ind = 0 while ind < len(ones) and twos: if sum - twos[-1] + ones[ind] >= m: sum = sum - twos[-1] + ones[ind] msum -= 1 ind += 1 twos.pop() elif ind + 1 < len(ones) and sum - twos[-1] + ones[ind] + ones[ind + 1] >= m: sum = sum - twos[-1] + ones[ind] + ones[ind + 1] ind += 2 twos.pop() else: break if sum >= m: print(msum) else: print(-1) def main(): t = int(input()) for i in range(t): solve() main()
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) a, b = list(map(int, input().split())), list(map(int, input().split())) imp = sorted([a[i] for i in range(n) if b[i] == 2]) not_imp = sorted([a[i] for i in range(n) if b[i] == 1]) res = 0 while (imp and not_imp) and m > 0: if imp[-1] > sum(not_imp[-2:]) and not_imp[-1] < m: m -= imp.pop() res += 2 else: m -= not_imp.pop() res += 1 if m > 0: while imp and m > 0: m -= imp.pop() res += 2 while not_imp and m > 0: m -= not_imp.pop() res += 1 if m > 0: print(-1) else: 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 VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER WHILE VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for j in range(t): n, m = tuple(map(int, input().split(" "))) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) one = [] two = [] for i in range(n): if b[i] == 1: one.append(a[i]) else: two.append(a[i]) two.sort(key=lambda x: -x) one.sort(key=lambda x: -x) sum1, sum2 = 0, sum(two) r = len(two) INF = 9999999999999 ans = INF for i in range(len(one) + 1): while r > 0 and sum1 + sum2 - two[r - 1] >= m: sum2 -= two[r - 1] r -= 1 if sum1 + sum2 >= m: ans = min(ans, i + 2 * r) if i < len(one): sum1 += one[i] print("-1" if ans == INF else str(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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n, m = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) A1 = [] A2 = [] for i in range(n): if B[i] == 1: A1.append(A[i]) else: A2.append(A[i]) A1.sort(reverse=True) A2.sort(reverse=True) S1 = [0] for a in A1: S1.append(S1[-1] + a) S2 = [0] for a in A2: S2.append(S2[-1] + a) ANS = 1 << 60 ind2 = len(S2) - 1 for i in range(len(S1)): s1 = S1[i] while s1 + S2[ind2] >= m: ANS = min(ANS, i + ind2 * 2) if ind2 > 0: ind2 -= 1 else: break if ANS == 1 << 60: print(-1) else: print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
cases = int(input()) for _ in range(cases): n, m = [int(i) for i in input().strip().split()] mem = [int(i) for i in input().strip().split()] points = [int(i) for i in input().strip().split()] ones = [(mem[i], points[i]) for i in range(n) if points[i] == 1] twos = [(mem[i], points[i]) for i in range(n) if points[i] == 2] ones.sort() twos.sort() ans = 0 while m > 0 and (ones or twos): if not twos or ones and m - ones[-1][0] <= 0: m -= ones.pop()[0] ans += 1 elif not ones: m -= twos.pop()[0] ans += 2 else: oneSum = sum(x[0] for x in ones[-2:]) twoSum = twos[-1][0] if oneSum >= twoSum: m -= ones.pop()[0] ans += 1 else: m -= twos.pop()[0] ans += 2 print(ans if m <= 0 else -1)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = [] y = [] for i in range(len(a)): if b[i] == 1: x.append(a[i]) else: y.append(a[i]) x = sorted(x, reverse=True) y = sorted(y, reverse=True) i = 0 j = 0 ans = 0 lis = [] while i < len(x) and j < len(y) and m > 0: if x[i] >= m: ans += 1 m -= x[i] lis.append(x[i]) i += 1 elif x[i] > y[j]: ans += 1 m -= x[i] lis.append(x[i]) i += 1 elif y[j] >= m: ans += 2 m -= y[j] j += 1 elif 2 * x[i] > y[j]: ans += 1 m -= x[i] lis.append(x[i]) i += 1 else: ans += 2 m -= y[j] j += 1 while i < len(x) and m > 0: ans += 1 m -= x[i] i += 1 while j < len(y) and m > 0: ans += 2 m -= y[j] j += 1 if m > 0: print(-1) else: while lis != [] and lis[-1] + m <= 0: ans -= 1 m += lis[-1] lis.pop() 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR LIST BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
rn = lambda: int(input()) rns = lambda: map(int, input().split()) rl = lambda: list(map(int, input().split())) rs = lambda: input() YN = lambda x: print("YES") if x else print("NO") mod = lambda x: x % (10**9 + 7) def d(a): d = {} for i in a: if i not in d: d[i] = 0 d[i] += 1 return d for _ in range(rn()): n, m = rns() a = rl() b = rl() if sum(a) < m: print(-1) else: ones = [] twos = [] for i in range(n): if b[i] == 1: ones.append(a[i]) else: twos.append(a[i]) ones.sort(reverse=True) twos.sort(reverse=True) i = 0 j = 0 acc = 0 while acc < m and i < len(ones): acc += ones[i] i += 1 while acc < m and j < len(twos): acc += twos[j] j += 1 ans = i + 2 * j while i > 0: i -= 1 acc -= ones[i] while acc < m and j < len(twos): acc += twos[j] j += 1 if acc >= m: ans = min(ans, i + 2 * j) else: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR NUMBER VAR VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for _ in range(int(input())): n, m = map(int, input().split()) memories = list(map(int, input().split())) convenience = list(map(int, input().split())) X, Y = [], [] for a, b in zip(memories, convenience): if b == 1: X.append(a) else: Y.append(a) X.sort(reverse=True) Y.sort() i, j = 0, 0 suma = sum(Y) puntos = float("inf") while i <= len(X): while j < len(Y) and suma - Y[j] >= m: suma -= Y[j] j += 1 if suma >= m: puntos = min(puntos, i + 2 * (len(Y) - j)) if i < len(X): suma += X[i] i += 1 if suma < m: print(-1) continue print(puntos)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
t = int(input()) for i in range(t): n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = list(map(int, input().split())) if sum(a) < m: print(-1) else: one_arr = [] two_arr = [] for app, score in zip(a, b): if score == 1: one_arr.append(app) else: two_arr.append(app) one_arr.sort() two_arr.sort() counter = 0 while m > 0: if len(one_arr) == 0: m -= two_arr[-1] del two_arr[-1] counter += 2 elif len(two_arr) == 0: m -= one_arr[-1] del one_arr[-1] counter += 1 elif m <= one_arr[-1]: m -= one_arr[-1] counter += 1 del one_arr[-1] elif len(one_arr) > 1: if one_arr[-1] + one_arr[-2] > two_arr[-1]: m -= one_arr[-1] counter += 1 del one_arr[-1] else: m -= two_arr[-1] counter += 2 del two_arr[-1] else: m -= two_arr[-1] counter += 2 del two_arr[-1] print(counter)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
in_length = int(input()) final_ouput = [] for i in range(in_length): [num_apps, mem_needed] = input().split() mem_used = [int(el) for el in input().split()] conv_points = input().split() big_num = 5000000000 out = big_num low_apps = [] high_apps = [] for j in range(int(num_apps)): if conv_points[j] == "1": low_apps.append(mem_used[j]) else: high_apps.append(mem_used[j]) low_apps = sorted(low_apps) high_apps = sorted(high_apps) low_apps.reverse() high_apps.reverse() acc_sum_low = 0 acc_sum_high = sum(high_apps) high_length = len(high_apps) for j in range(len(low_apps) + 1): while high_length > 0 and acc_sum_low + acc_sum_high - high_apps[ high_length - 1 ] >= int(mem_needed): high_length -= 1 acc_sum_high -= high_apps[high_length] if acc_sum_low + acc_sum_high >= int(mem_needed): out = min(out, 2 * high_length + j) if j != len(low_apps): acc_sum_low += low_apps[j] if out == big_num: final_ouput.append(-1) else: final_ouput.append(out) for out in final_ouput: print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
for t in range(int(input())): n, m = [int(k) for k in input().split()] a = [int(k) for k in input().split()] b = [int(k) for k in input().split()] if m > sum(a): print(-1) continue imp = [] non = [] for i in range(n): if b[i] == 2: imp += [a[i]] else: non += [a[i]] ans = 0 imp.sort(reverse=True) non.sort(reverse=True) il = len(imp) nl = len(non) ii = 0 ni = 0 while m > 0: if ni < nl: m -= non[ni] ni += 1 ans += 1 else: m -= imp[ii] ii += 1 ans += 2 fin = ans ni -= 1 while ni >= 0: m += non[ni] ans -= 1 ni -= 1 while m > 0 and ii < il: m -= imp[ii] ii += 1 ans += 2 if m > 0: break if ans < fin: fin = ans print(fin)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR LIST VAR VAR VAR LIST VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
def solve(): n, m = input().split() n = int(n) m = int(m) mem = input().split() unit = input().split() regular = [] important = [] for i in range(n): mem[i] = int(mem[i]) unit[i] = int(unit[i]) if unit[i] == 1: regular.append(mem[i]) else: important.append(mem[i]) regular.sort(reverse=True) important.sort(reverse=True) sum_regular = 0 sum_important = sum(important) index_important = len(important) res = 1000000000.0 + 1 for i in range(len(regular) + 1): while ( index_important > 0 and sum_regular + sum_important - important[index_important - 1] >= m ): index_important -= 1 sum_important -= important[index_important] if sum_regular + sum_important >= m: res = min(res, 2 * index_important + i) if i != len(regular): sum_regular += regular[i] if res != 1000000000.0 + 1: return res else: return -1 no_tests = int(input()) results = [] for i in range(no_tests): results.append(solve()) for result in results: print(result)
FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys input = sys.stdin.readline def solution(n, m, a, b): l_1 = [] l_2 = [] diff_memory = 0 sol = 10**10 sum_a = sum(a) if sum_a < m: print(-1) return l_1 = [ai for ai, bi in zip(a, b) if bi == 1] l_2_slave = [ai for ai, bi in zip(a, b) if bi == 2] l_1.sort(reverse=True) l_2_slave.sort(reverse=True) l_2 = [] for i in range(-1, 2 * len(l_2_slave) - 1): if i % 2 == 1: l_2.append(0) else: l_2.append(l_2_slave[i // 2]) partial_l1 = [0] for elem in l_1: partial_l1.append(partial_l1[-1] + elem) partial_l2 = [0] for elem in l_2: partial_l2.append(partial_l2[-1] + elem) i1 = len(partial_l1) - 1 for i2 in range(len(partial_l2)): while i1 >= 0: if partial_l1[i1] + partial_l2[i2] >= m: sol = min(sol, i1 + i2) i1 -= 1 else: break print(sol) T = int(input()) for t in range(T): n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) solution(n, m, a, b)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Polycarp often uses his smartphone. He has already installed $n$ applications on it. Application with number $i$ takes up $a_i$ units of memory. Polycarp wants to free at least $m$ units of memory (by removing some applications). Of course, some applications are more important to Polycarp than others. He came up with the following scoring system β€” he assigned an integer $b_i$ to each application: $b_i = 1$ β€” regular application; $b_i = 2$ β€” important application. According to this rating system, his phone has $b_1 + b_2 + \ldots + b_n$ convenience points. Polycarp believes that if he removes applications with numbers $i_1, i_2, \ldots, i_k$, then he will free $a_{i_1} + a_{i_2} + \ldots + a_{i_k}$ units of memory and lose $b_{i_1} + b_{i_2} + \ldots + b_{i_k}$ convenience points. For example, if $n=5$, $m=7$, $a=[5, 3, 2, 1, 4]$, $b=[2, 1, 1, 2, 1]$, then Polycarp can uninstall the following application sets (not all options are listed below): applications with numbers $1, 4$ and $5$. In this case, it will free $a_1+a_4+a_5=10$ units of memory and lose $b_1+b_4+b_5=5$ convenience points; applications with numbers $1$ and $3$. In this case, it will free $a_1+a_3=7$ units of memory and lose $b_1+b_3=3$ convenience points. applications with numbers $2$ and $5$. In this case, it will free $a_2+a_5=7$ memory units and lose $b_2+b_5=2$ convenience points. Help Polycarp, choose a set of applications, such that if removing them will free at least $m$ units of memory and lose the minimum number of convenience points, or indicate that such a set does not exist. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Then $t$ test cases follow. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10^9$) β€” the number of applications on Polycarp's phone and the number of memory units to be freed. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the number of memory units used by applications. The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 2$) β€” the convenience points of each application. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output on a separate line: -1, if there is no set of applications, removing which will free at least $m$ units of memory; the minimum number of convenience points that Polycarp will lose if such a set exists. -----Examples----- Input 5 5 7 5 3 2 1 4 2 1 1 2 1 1 3 2 1 5 10 2 3 2 3 2 1 2 1 2 1 4 10 5 1 3 4 1 2 1 2 4 5 3 2 1 2 2 1 2 1 Output 2 -1 6 4 3 -----Note----- In the first test case, it is optimal to remove applications with numbers $2$ and $5$, freeing $7$ units of memory. $b_2+b_5=2$. In the second test case, by removing the only application, Polycarp will be able to clear only $2$ of memory units out of the $3$ needed. In the third test case, it is optimal to remove applications with numbers $1$, $2$, $3$ and $4$, freeing $10$ units of memory. $b_1+b_2+b_3+b_4=6$. In the fourth test case, it is optimal to remove applications with numbers $1$, $3$ and $4$, freeing $12$ units of memory. $b_1+b_3+b_4=4$. In the fifth test case, it is optimal to remove applications with numbers $1$ and $2$, freeing $5$ units of memory. $b_1+b_2=3$.
import sys t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) reg, imp = [], [] for i in range(n): if b[i] == 1: reg.append(a[i]) else: imp.append(a[i]) reg.sort(reverse=True) imp.sort(reverse=True) reg_len, imp_len = len(reg), len(imp) r = imp_len ans = sys.maxsize curSumA, curSumB = 0, sum(imp) for i in range(reg_len + 1): while r > 0 and curSumA + curSumB - imp[r - 1] >= k: r -= 1 curSumB -= imp[r] if curSumA + curSumB >= k: ans = min(ans, 2 * r + i) if i != reg_len: curSumA += reg[i] print(-1 if ans == sys.maxsize else ans)
IMPORT 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR