题意:
第一行给出 骷髅数目,背包容量;
第二行给出每一个骷髅的价值;
第三行给出每一个骷髅的体积。
很显然这题是背包题。
注意:i<=v;
图片解释过程:
java代码:
import java.util.Scanner;
public class Main {
/*
* t表示测试事件的个数
* n骨头的个数
* v每个骨头的容量
* bos-bones骨头
* vol-volumes容器
* dp-dynamic_programming动态规划
*/
static int t, n, v;
static int[] bos = new int[1001];
static int[] vol = new int[1001];
static int[] dp = new int[1001];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
v = sc.nextInt();
for (int i = 1; i <= n; i++) {
bos[i] = sc.nextInt();
}
for (int i = 1; i <= n; i++) {
vol[i] = sc.nextInt();
}
for (int i = 0; i <= v; i++) {// 注意i <= v
dp[i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = v; j >= vol[i]; j--) {
dp[j] = max(dp[j], dp[j - vol[i]] + bos[i]);
}
}
System.out.println(dp[v]);
}
}
/*
* a表示不放入背包,
* b表示放入背包
*/
public static int max(int a, int b) {
return a > b ? a : b;
}
}
Bone Collector
骨头搜集者
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 39870 Accepted Submission(s): 16533
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”.
许多年以前,在Teddy的家乡有一类叫收集骷髅的人。
This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
这些人喜欢收集骷髅,比如说狗的,奶牛的,而且他们也进入过墓穴。
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously ,
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously ,
骨头爱好者有一个V容量的背包,显然,他会在墓穴中收集骷髅,
different bone has different value and different volume, now given the each bone’s value along his trip ,
不同的骷髅有不同的价值和不同的体积,现在给出他旅行中每一个骷髅的价值。
can you calculate out the maximum of the total value the bone collector can get ?
你能计算出骷髅收集者所有骷髅的总价值吗?
Input
The first line contain a integer T , the number of cases.
输入的第一行包含一个正整数T,表示测试事件的个数。
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )
接下来有T个测试事件,每一个测试事件有三行数据,第一行包含两个整形数据N,V,(N <= 1000 , V <= 1000 ),
representing the number of bones and the volume of his bag.
表示骷髅的数量和背包的容量。
And the second line contain N integers representing the value of each bone.
第二行包含N个整数,表示骷髅的价值。
The third line contain N integers representing the volume of each bone.
第三行包含N个整数,表示每一骷髅的体积。
Output
One integer per line representing the maximum of the total value (this number will be less than 2
31).
在新的一行输出背包能装下最大的价值,这个数将小于 2
31。
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
Author
Teddy