OpenJudge - 01042:Gone Fishing
唉,又臭又长的英文……大概意思就是说,有一条单向道路,一个人沿着这条路到达各个湖泊(只能前进,不能后退),每到一个湖泊都可以选择钓鱼或者不钓(若时间到了,则直接离开),钓鱼也可以选择在那里的时长,在某个湖泊钓到鱼,之后在同一个湖泊的钓鱼数量会减少,直至为0。已知道路之间的时长,在相同时间内初始时刻各个湖泊能钓到的鱼的数量,每钓一次鱼减少的相应数量,以及这个人有多少时间。
理清楚题目意思之后,其实就是一个贪心+优先队列。
我们规定在相应时间内,这个人只能走前个湖泊,并求出能钓到的鱼的最大数量,这个步骤用优先队列解决。
之后,我们遍历每一种情况(即进行遍历),进行比较后即可得到最大值。
:我不太理解为什么第三十行那里写 while left:就会超时……知道的麻烦评论告诉我,谢谢!
上代码!
from queue import PriorityQueue
class Node:
def __init__(self, idx, fish, down):
self.idx = idx
self.fish = fish
self.down = down
def __lt__(self, other):
if self.fish == other.fish:
return self.idx < other.idx
return -self.fish < -other.fish
while True:
n = int(input())
if n == 0:
break
h = int(input())*12
f = list(map(int, input().split()))
d = list(map(int, input().split()))
t = [0]+(list(map(int, input().split())) if n > 1 else [])
ans = -1
result = [0]
for i in range(n):
now = 0
queue = PriorityQueue()
for j in range(i+1):
queue.put(Node(j, f[j], d[j]))
tmp = [0 for _ in range(n)]
left = h-sum(t[:i+1])
while left > 0:
node = queue.get()
tmp[node.idx] += 1
now += node.fish
if node.fish > node.down:
queue.put(Node(node.idx, node.fish-node.down, node.down))
else:
queue.put(Node(node.idx, 0, node.down))
left -= 1
if now > ans:
ans = now
result = tmp
print(', '.join(map(str, [v*5 for v in result])))
print(f'Number of fish expected: {ans}')
print()