贝壳第三题健身房最大锻炼值代码,有没有大佬来交流下思路?

rt,有大佬来讨论下么,我这里用堆来解决的这个问题,具体代码如下,有大佬要交流思路的可以私聊我。
import sys
import heapq

data = []
for a in sys.stdin.readlines():
    data.append([int(x) for x in a.strip().split()])

N = data[0][0]
X = data[1]
P = data[2]

E = [2*X[i]+P[i] for i in range(len(X))]
heap1 = []
heap2 = []
for i, x in enumerate(E):
    heapq.heappush(heap1, (-1*x, i))

ptr = -1
res = []

while len(res) != N:
    if ptr == -1:
        y, idx = heapq.heappop(heap1)
        res.append(-1*y)
        if idx > ptr:
            ptr = idx
        for i, x in enumerate(heap1):
            if x[1] < ptr:
                heapq.heappush(heap2, (-1*P[x[1]], x[1]))
                heap1[i] = 0
        if 0 in heap1:
            heap1.remove(0)
        tmp = []
        for x in heap1:
            if x != 0:
                heapq.heappush(tmp, x)
        heap1 = tmp
    else:
        if len(heap2) == 0 or (len(heap1) != 0 and -1*heap1[0][0] - 2*X[ptr] > -1*heap2[0][0]):
            y, idx = heapq.heappop(heap1)
            res.append(-1*y - 2*X[ptr] + res[-1])
            if idx > ptr:
                ptr = idx
                #还要将heap1中的所有比ptr位置之前的都要放到ptr2中去
                tmp = []
                for i, x in enumerate(heap1):
                    if x[1] < ptr:
                        heapq.heappush(heap2, x)
                        heap1[i] = 0
                for x in heap1:
                    if x != 0:
                        heapq.heappush(tmp, x)
                heap1 = tmp
        else:
            y, idx = heapq.heappop(heap2)
            res.append(-1*y + res[-1])

for x in res:
    print(x)

#贝壳找房##笔试题目#
全部评论
我没有做这个笔试,看到同学发的我大概说下我的思路,就是dp dp[i][j]代表前i个点选择j种训练方式。内容是一个列表,两个元素[最多消耗数,最后的选择的锻炼方式所在的点]。dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j-1][0]+2*(i-dp[i-1][j-1][1])+nums[i]),max里面第一项是不选择当前i这个点,第二项是选择i这个点,选择了就得根据前面dp[i-1][j-1]的最后一个点的位置算出距离以及自己本身消耗的能量。
点赞 回复 分享
发布于 2019-08-27 07:52

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务