java 购物单
描述
王强今天很开心,公司发给N元的年终奖。王强决定把年终奖用于购物,他把想买的物品分为两类:主件与附件,附件是从属于某个主件的,下表就是一些主件与附件的例子:
主件 | 附件 |
电脑 | 打印机,扫描仪 |
书柜 | 图书 |
书桌 | 台灯,文具 |
工作椅 | 无 |
如果要买归类为附件的物品,必须先买该附件所属的主件,且每件物品只能购买一次。每个主件可以有 0 个、 1 个或 2 个附件。附件不再有从属于自己的附件。王强想买的东西很多,为了不超出预算,他把每件物品规定了一个重要度,分为 5 等:用整数 1 ~ 5 表示,第 5 等最重要。他还从因特网上查到了每件物品的价格(都是 10 元的整数倍)。他希望在不超过 N 元(可以等于 N 元)的前提下,使每件物品的价格与重要度的乘积的总和最大。
设第 j 件物品的价格为 v[j] ,重要度为 w[j] ,共选中了 k 件物品,编号依次为 j 1 , j 2 ,……, j k ,则所求的总和为:
v[j 1 ]*w[j 1 ]+v[j 2 ]*w[j 2 ]+ … +v[j k ]*w[j k ] 。(其中 * 为乘号)
请你帮助王强设计一个满足要求的购物单。
输入描述:
输入的第 1 行,为两个正整数,用一个空格隔开:N m
(其中 N ( N<32000 )表示总钱数, m (m <60 )为希望购买物品的个数。)
从第 2 行到第 m+1 行,第 j 行给出了编号为 j-1 的物品的基本数据,每行有 3 个非负整数 v p q
(其中 v 表示该物品的价格( v<10000 ), p 表示该物品的重要度( 1 ~ 5 ), q 表示该物品是主件还是附件。如果 q=0 ,表示该物品为主件,如果 q>0 ,表示该物品为附件, q 是所属主件的编号)
输出描述:
输出文件只有一个正整数,为不超过总钱数的物品的价格与重要度乘积的总和的最大值( <200000 )。
示例1
输入:
1000 5 800 2 0 400 5 1 300 5 1 400 3 0 500 2 0
输出:
2200
示例2
输入:
50 5 20 3 5 20 3 5 10 3 0 10 2 0 10 1 0
输出:
130
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 5 public class Main { 6 7 static int price[][]; 8 static int value[][]; 9 static int bag[][]; 10 public static void main(String[] args) { 11 Scanner in = new Scanner(System.in); 12 String[] temp = in.nextLine().split(" "); 13 int crash = Integer.parseInt(temp[0]); 14 int count = Integer.parseInt(temp[1]); 15 price = new int[count+1][3];//价格 16 value = new int[count+1][3];//重要度 17 int i=1; 18 while(i<=count) { 19 temp = in.nextLine().split(" "); 20 if(temp.length == 3) { 21 int a = Integer.parseInt(temp[0]); 22 int b = Integer.parseInt(temp[1]); 23 int c = Integer.parseInt(temp[2]); 24 if(c == 0) { 25 //主件 26 price[i][0] = a; 27 value[i][0] = a*b; 28 }else { 29 //附件 30 if(price[c][1] ==0) { 31 price[c][1] = a; 32 value[c][1] = a*b; 33 }else { 34 price[c][2] = a; 35 value[c][2] = a*b; 36 } 37 } 38 } 39 i++; 40 } 41 in.close(); 42 bag = new int[count+1][crash+1]; 43 int res = 0; 44 res = findMax(count,crash); 45 System.out.println(res); 46 47 } 48 private static int findMax(int count, int crash) { 49 if(count ==0 || crash == 0) { 50 return 0; 51 } 52 if(bag[count][crash] != 0) { 53 return bag[count][crash]; 54 } 55 56 int result = 0; 57 if(crash<price[count][0]) { 58 //钱不够 59 result = findMax(count-1,crash); 60 }else { 61 int unpick = findMax(count-1,crash); 62 result = findMax(count-1,crash-price[count][0]) + value[count][0]; 63 if(crash>=price[count][0]+price[count][1]) { 64 int b = findMax(count-1,crash-price[count][0]-price[count][1]) + value[count][0] + value[count][1]; 65 result = Math.max(result, b); 66 } 67 if(crash>=price[count][0]+price[count][2]) { 68 int c = findMax(count-1,crash-price[count][0]-price[count][2]) + value[count][0] + value[count][2]; 69 result = Math.max(result, c); 70 } 71 if(crash>=price[count][0]+price[count][1]+price[count][2]) { 72 int d = findMax(count-1,crash-price[count][0]-price[count][1]-price[count][2]) + value[count][0] + value[count][1] + value[count][2]; 73 result = Math.max(result, d); 74 } 75 result = Math.max(result, unpick); 76 } 77 bag[count][crash] = result; 78 return result; 79 } 80 }