第四周1001题解
2015-08-23 22:33
357 查看
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. 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 , 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. Followed by T cases , each case three lines , the first line contain two integer 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. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 2[sup]31[/sup]).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
Author
Teddy
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. 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 , 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. Followed by T cases , each case three lines , the first line contain two integer 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. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 2[sup]31[/sup]).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
Author
Teddy
import java.util.Scanner; public class Main1001 { // public static int value[]; // public static int volume[]; // public static int res[][]; // //public static int getDp(int N,int V){//递归dp // if(N==0) // return 0; // res [1] = value +getDp(N-1,V-volume ); // res [0] =getDp(N-1,V); // return Math.max(res [1], res [0]); //} public static void main(String args[]) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int x = 0; x < T; x++) { int N = sc.nextInt();// 宝石数量 int V = sc.nextInt();// 背包容量 int []value = new int ;// 宝石的价值 int []volume = new int ;// 宝石的体积 int [][]res = new int[1001][1001]; for(int i = 0;i<N;i++){ value[i] = sc.nextInt(); } for(int i = 0;i<N;i++){ volume[i] = sc.nextInt(); } for(int i=0; i<=N; ++i){ for(int j=0; j<=V; ++j){ res[i][j] = i==0 ? 0 : res[i-1][j];//假设不取 if(i>0 && j>=volume[i-1]){ if(res[i][j]<res[i-1][j-volume[i-1]]+value[i-1]) res[i][j] = res[i-1][j-volume[i-1]]+value[i-1]; } } } // System.out.println(getDp(N-1, V)); System.out.println(res [V]); } } }
相关文章推荐
- Python编写Hadoop MapReduce程序
- POJ 2195 Going Home(二分图最大权值匹配) KM
- 查看ubuntu系统的版本号和内核版本号
- Linux默认shell—bash shell的优点
- OpenCV249 for python278 最简配置方案
- const关键字修饰指针的疑惑点
- 第四周1002题解
- 学习JS(6)
- C++实现线程池类
- SAS能自己编写函数么?
- 翻转子串(Java)
- TCP/IP——内外网IP+子网掩码作用+PING(网络总结)
- 第一百四十一/二/三天 how can I 坚持
- Tomcat的Server.xml虚拟主机和虚拟目录的配置
- python的深拷贝和浅拷贝
- html事例
- DB2 执行Sql 语句中时的with ur
- linux帮助相关命令man、info、help
- 循环链表的特性以及判断单链表是否有环(九)
- Android Studio上使用github