Codeforces Round #667 (Div. 3)
比赛传送门
A - Yet Another Two Integers Problem
Code
def main():
T = eval(input())
for cas in range(T):
a, b = map(int, input().split())
print("{}".format((abs(a-b)+9)//10))
main()
B - Minimum Product
Code
def fun(a, b, x, y, n):
sub = min(a-x, n)
a -= sub
n -= sub
sub = min(b-y, n)
b -= sub
n -= sub
return a*b
def main():
T = eval(input())
for cas in range(T):
a, b, x, y, n = map(int, input().split())
minn = 100000000000000000005
minn = min(minn, fun(a, b, x, y, n))
minn = min(minn, fun(b, a, y, x, n))
print(minn)
main()
C - Yet Another Array Restoration
Code
def main():
T = eval(input())
for cas in range(T):
n, x, y = map(int, input().split())
cnt = 1
while True:
if (y-x)%cnt==0 and (y-x)//cnt<=n-1:
counter = 0
tmp = x
while tmp <= y:
print("{} ".format(tmp), end='')
tmp +=cnt
counter += 1
tmp = x - cnt
while counter<n and tmp>0:
print("{} ".format(tmp), end='')
counter += 1
tmp -= cnt
tmp = y + cnt
while counter<n:
print("{} ".format(tmp), end='')
counter += 1
tmp += cnt
break
cnt += 1
print()
main()
D - Decrease the Sum of Digits
Code
def fun(tmp):
ret = 0
while tmp > 0:
ret += tmp % 10
tmp //= 10
return ret
def main():
T = eval(input())
for cas in range(T):
n, s = map(int, input().split())
tmp = 1
ans = 0
rdy = 0
while n > 0:
num = fun(n) + rdy
bit = n % 10
if num > s and bit != 0:
ans += (10-bit)*tmp
n += 10
tmp *= 10
n //= 10
print(ans)
main()
E - Two Platforms
Problem Description
There are n points on a plane. The i-th point has coordinates (xi,yi). 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≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and k (1≤n≤2⋅105; 1≤k≤109) — the number of points and the length of each platform, respectively. The second line of the test case contains n integers x1,x2,…,xn (1≤xi≤109), where xi is x-coordinate of the i-th point. The third line of the input contains n integers y1,y2,…,yn (1≤yi≤109), where yi is y-coordinate of the i-th point. All points are distinct (there is no pair 1≤i<j≤n such that xi=xj and yi=yj).
It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).
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:
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.
题意
给定n个点和两个长度为k的木板,问怎么放置这两个木板可以接住最多的小球。
这两个木板只能平行x轴放置,且只能放在整数点上。
思路
官方
Firstly, we obviously don’t need y-coordinates at all because we can place both platforms at y=−∞. Let’s sort all x-coordinates in non-decreasing order.
Calculate for each point i two values li and ri, where li is the number of points to the left from the point i (including i) that are not further than k from the i-th point (i.e. the number of such points j that |xi−xj|≤k). And ri is the number of points to the right from the point i (including i) that are not further than k from the i-th point. Both these parts can be done in O(n) using two pointers.
Then let’s build suffix maximum array on r and prefix maximum array on l. For l, just iterate over all i from 2 to n and do li:=max(li,li−1). For r, just iterate over all i from n−1 to 1 and do ri:=max(ri,ri+1).
The question is: what? What did we do? We did the following thing: the answer always can be represented as two non-intersecting segments of length k such that at least one endpoint of each segment is some input point (except the case n=1). Now, let’s fix this border between segments. Iterate over all i from 1 to n−1 and update the answer with max(li,ri+1). So we took some segment that starts at some point to the left from i (including i) and goes to the left and took some segment that starts further than i+1 (including i+1) and goes to the right. With this model, we considered all optimal answers that can exist.
Time complexity: O(nlogn).
总结
因为小球向下掉,所以y坐标对结果没有贡献。所以我们只需要考虑x坐标。
我们可以先对x坐标排序,然后分别求某点i左侧的以i为边界可以挡住的点数和右侧的以i为边界可以挡住的点数,然后分别对这两个数组求前缀最大值和后缀最大值。然后遍历所有的点,当前点的前缀最大值加后缀最大值就是当前点的最大值。将所有的点的最大值求最大值即可。
Code
def main():
t = eval(input())
for cas in range(t):
n, k = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
x.sort()
r = [0 for _ in range(n+5)]
l = [0 for _ in range(n+5)]
tmp = 0
for i in range(n):
while x[i] - x[tmp] > k:
tmp += 1
l[i] = i - tmp + 1
if i > 0:
l[i] = max(l[i], l[i-1])
tmp = n-1
for i in range(n-1, -1, -1):
while x[tmp] - x[i] > k:
tmp -= 1
r[i] = tmp - i + 1
if i + 1 < n:
r[i] = max(r[i], r[i+1])
ret = 1
for i in range(n-1):
ret = max(ret, r[i+1] + l[i])
print(ret)
main()