发布于2019-12-07 22:53 阅读(1482) 评论(0) 点赞(19) 收藏(0)
Given a set of n jobs where each job i has a deadline and profit associated to it. Each job takes 1 unit of time to complete and only one job can be scheduled at a time. We earn the profit if and only if the job is completed by its deadline. The task is to find the maximum profit and the number of jobs done.
The first line of input contains an integer T denoting the number of test cases.Each test case consist of an integer N denoting the number of jobs and the next line consist of Job id, Deadline and the Profit associated to that Job.
Constraints:1<=T<=100,1<=N<=100,1<=Deadline<=100,1<=Profit<=500
Output the number of jobs done and the maximum profit.
2
4
1 4 20 2 1 10 3 1 40 4 1 30
5
1 2 100 2 1 19 3 2 27 4 1 25 5 1 15
2 60
2 127
题目解析:
错误思路:
如果每次都选择最大价值的任务话,那么就会有一些任务选不到,比如说第一次选择了截至时间为2的任务,那么第二次 就无法选择截至时间为1的任务(已经结束了)
eg 2 100 1 90 如果选择100,第二次就无法选择90
如果每次都选择该时间内最大价值的任务,这样结果一定是每个时间由一个任务组成,那么就会漏解
eg: 2 20 , 2 21, 1 10 ,1 11 如果选择11 21 就会漏掉20 21
由题意可知,对于每个截至时间为N的工作我们最多选N次,
所以我们可以想到如下办法,
第一次选择截至时间为1中最大的1个数
第二次选择截至时间为2中最大的两个数,把第一次的最大值也算上,更新比较留下2个最大的
第三次选择截至时间为3中最大的三个数,把第二次的最大值也算上,更新比较留下3个最大的
依次类推,这样就能获得最佳的答案
但这样未免太复杂,并且如果截至时间为x中没有x个数,就会漏解,但根据此思路,有如下办法
解题思路:
# 时间与收益
# 贪婪算法:每次得到最优解的一部分
# 需要一个一维标记数组,来标记各个时间点是否已有任务,存放收益
# 按收益排序,每次取出最大收益,判断把任务编号放入数组中
def timeAndProfit(arr):
max_arr = []
max_deadline = -1
for i in range(0, len(arr), 3):
if arr[i + 1] > max_deadline:
max_deadline = arr[i + 1] # 获取最大截至日期
max_arr.append([arr[i + 1], arr[i + 2]]) # 构建[日期,收益]列表
max_arr.sort(reverse=True, key = lambda x: x[1]) # 按收益排序
dp = [0] * max_deadline # 标记数组,存放结果
count = 0
for tmp in max_arr:
deadline = tmp[0] - 1 # 对应标记数组下标
while deadline >= 0 and dp[deadline] != 0:
deadline -= 1
if deadline != -1:
dp[deadline] = tmp[1]
count += 1
return count, sum(dp)
if __name__ == '__main__':
for _ in range(int(input())):
N = int(input())
arr = list(map(int, input().split()))
count, result = timeAndProfit(arr)
print(count, result)
作者:dkjf787
链接:https://www.pythonheidong.com/blog/article/170257/c5f9386931ae72d967f0/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!