订单问题

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
 
解题思路:
这道题目里服务员A有自己的指标X,B有自己的指标Y,同时订单数量为N。有三种状态,可以考虑用三维dp来做。
设dp[N][X][Y],dp[i][x][y]表示有1-i个订单,A只有x指标,B只有y个指标时获得的最大收益。
对于订单i,只有三种情况,A拿,B拿,A和B都不拿。对应状态转移方程为
dp[i][x][y] = Math.max(dp[i-1][x][y], dp[i-1][x-1][y]+A[i], dp[i-1][x][y-1]+B[i])
这里比较麻烦的是初始状态,在i=0时很明显dp[0][x][y] = 0(对任意xy),在x=0并且y=0时也很明显dp[i][0][0]=0(对任意i)。
对x=0的情况,表示A完全不拿,只有N个订单和服务员B。对于第i个订单,B可以拿也可以不拿,可以得到dp[i][0][y] = Math.max(dp[i-1][0][y-1]+B[i],dp[i-1][0][y])。
同理,对y=0的情况。只有N个订单和服务员A,可以得到dp[i][x][0] = Math.max(dp[i-1][x-1][0]+A[i], dp[i-1][x][0])。
 
完整代码如下:
 1 import java.util.*;
 2 
 3 public class Main { // 注意类名必须为Main
 4     public static void main(String[] args) {
 5         Scanner scan = new Scanner(System.in);
 6         int number = scan.nextInt();
 7         scan.nextLine();
 8         // number个测试样例
 9         for (int k = 0; k < number; k++){
10             // 输入数据
11             int N = scan.nextInt();
12             int X = scan.nextInt();
13             int Y = scan.nextInt();
14             int[] A = new int[N+1];
15             int[] B = new int[N+1];
16             for (int i = 1; i <= N; i++) {
17                 A[i] = scan.nextInt();
18             }
19             for (int i = 1; i <= N; i++) {
20                 B[i] = scan.nextInt();
21             }
22             // dp[i][x][y]表示有1-i个任务,A有x个指标,B有y个指标时获得的最大收益
23             int[][][] dp = new int[N+1][X+1][Y+1];
24             // 1.初始化i=0的情况
25             for (int x = 0; x <= X; x++)
26                 for (int y = 0; y <= Y; y++)
27                     dp[0][x][y] = 0;
28             // 2.x=0 y=0
29             for (int i = 1; i <= N; i++)
30                 dp[i][0][0] = 0;
31             // 3.初始化x=0的情况
32             for (int i = 1; i <= N; i++)
33                 for (int y = 1; y <= Y; y++)
34                     dp[i][0][y] = Math.max(dp[i-1][0][y-1]+B[i], dp[i-1][0][y]);    // B[i]有拿与不拿两种情况
35             // 4.初始化y=0的情况
36             for (int i = 1; i <= N; i++)
37                 for (int x = 1; x <= X; x++)
38                     dp[i][x][0] = Math.max(dp[i-1][x-1][0]+A[i], dp[i-1][x][0]);    // A[i]有拿与不拿两种情况
39             // 计算dp[][][]剩余情况,对于第i个订单,有A,B都不拿,A拿,B拿三种情况
40             for (int i = 1; i <= N; i++)
41                 for (int x = 1; x <= X; x++)
42                     for (int y = 1; y <= Y; y++) {
43                         dp[i][x][y] = Math.max(dp[i-1][x][y], Math.max(dp[i-1][x-1][y]+A[i], dp[i-1][x][y-1]+B[i]));
44                     }
45 
46             System.out.println(dp[N][X][Y]);
47         }
48     }
49 }

 

posted @ 2021-04-05 00:01  凝冰物语  阅读(111)  评论(0编辑  收藏  举报