Description
Rahul and Ankit are the only two waiters in Royal Restaurant. Today, the restaurant received N orders. The amount of tips may differ when handled by different waiters, if Rahul takes the ith order, he would be tipped Ai rupees and if Ankit takes this order, the tip would be Bi rupees.In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than X orders and Ankit cannot take more than Y orders. It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders.
Input
• The first line contains one integer, number of test cases.
• The second line contains three integers N, X, Y.
• The third line contains N integers. The ith integer represents Ai.
• The fourth line contains N integers. The ith integer represents Bi.
Output
Print a single integer representing the maximum tip money they would receive.
Sample Input 1
1
5 3 3
1 2 3 4 5
5 4 3 2 1
Sample Output 1
21
考察动态规划(套路型初始化二维数组、初始化边界值、填充二维数组、返回二维数组右下角元素)
# 第一个人小费数组、第二个人小费数组、第一个人能处理订单数、第二个人能处理订单数
def solution(tip_1, tip_2, x, y, num):
# 初始化x+1行y+1列的动态规划数组
init = [[0 for i in range(y + 1)] for j in range(x + 1)]
# 初始化边界值,即只让一个人收小费
for m in range(1, y+1):
init[0][m] = init[0][m - 1] + tip_2[m - 1]
for n in range(1, x + 1):
init[n][0] = init[n - 1][0] + tip_1[n - 1]
# 填充二维数组
for p in range(1, x + 1):
for q in range(1, y + 1):
if p + q <= num:
init[p][q] = max(init[p][q - 1] + tip_2[p + q - 1], init[p - 1][q] + tip_1[p + q - 1])
else:
init[p][q] = max(init[p - 1][q], init[p][q - 1])
return init[p][q]
if __name__ == '__main__':
num_examples = int(input())
for i in range(num_examples):
l1 = list(map(int, input().strip().split()))
num, x, y = l1[0], l1[1], l1[2]
tip_1 = list(map(int, input().strip().split()))
tip_2 = list(map(int, input().strip().split()))
result = solution(tip_1, tip_2, x, y, num)
print(result)
来源:CSDN
作者:cy求求你让我过吧
链接:https://blog.csdn.net/qq_26496077/article/details/103244503