公式
D - フルーツセレクション / Fruit Selection 解説
by
D - フルーツセレクション / Fruit Selection 解説
by
sounansya
まず、利益が正ではない果物はできるだけ並べない方が良いです。具体的には、\(1\) つでも利益が正となる果物がある場合は利益が正でない果物を考えなくて良いです。もし利益が正でない果物しかない場合、その中で利益が最大となる果物を \(1\) つ並べるとして良いです。
利益が正となる果物しかない場合は、果物を置いて損することがないので果物を売値でソートした後尺取り法を行えば良いです。
import sys
input = sys.stdin.readline
n, d = map(int, input().split())
max_val = -(10**9)
a = []
for i in range(n):
c, p = map(int, input().split())
if p > c:
a.append((p, p - c))
max_val = max(max_val, p - c)
if len(a) == 0:
print(max_val)
exit()
a.sort()
m = len(a)
ans = 0
now = 0
j = 0
for i in range(m):
while j < m and a[j][0] - a[i][0] <= d:
now += a[j][1]
j += 1
ans = max(ans, now)
now -= a[i][1]
print(ans)
投稿日時:
最終更新:
