题目描述
- Sidney想去Gandtom家玩。但Sidney家和Gandtom家之间是高低不平、坑坑洼洼的土路。所以他需要用他的背包装几袋稀的泥,在路上铺平一些干的土,使路变成平整的泥土,才能到Gandtom家见到Gandtom。
已知现在有n种稀的泥,第i种稀的泥的质量为wi,体积为vi。Sidney的包能装体积不超过V的稀的泥。Sidney出门时携带的稀的泥的质量应该尽可能的大。在此前提下,携带的稀的泥的体积也应该尽可能的大。
试求Sidney最多能携带多少质量的稀的泥与此时的最大体积上路。 - Input
第一行有一个整数T,表示组数。
每组数据第一行有两个正整数n、V(0<n,V<=103) 。
每组数据第二行有个n正整数,第i个数为wi(0<wi<=106)。
每组数据第三行有个n正整数,第i个数为vi(0<vi<=103) - Output
每组样例第一行输出两个整数。表示Sidney最多能携带多少质量的稀的泥与此时的最大体积上路。
Sample Input
2
5 3
1 2 3 4 5
1 1 1 1 1
3 7
1 2 1
3 5 3
Sample Output
12 3
2 6
题目解析
- 该题用贪心不好保证两个都最大,dp数组每个单元格表示,有j种水泥和i个背包容量的最优装载情况,在dp数组中可以通过dfs寻找每次“选择”的水泥(不过时间复杂度有些高)
代码
Scanner scanner=new Scanner(System.in);
int T=scanner.nextInt();
for (int i = 0; i < T; i++) {
int n=scanner.nextInt();
Shuini[] samples=new Shuini[n];
int V=scanner.nextInt();
for (int j = 0; j < n; j++) {
samples[j]=new Shuini(scanner.nextInt(),0);
}
for (int j = 0; j < n; j++) {
samples[j].vi=scanner.nextInt();
samples[j].getUnitValue();
}
//贪心算法(无法保证w,v同时最大)
//solve(samples,V);
//dp动规划
solve1(samples,V);
}
}
private static</

5439

被折叠的 条评论
为什么被折叠?



