提交 284d37fd 编写于 作者: qq_36480062's avatar qq_36480062

commit

上级 061ef5bc
......@@ -10,9 +10,10 @@ public class Main {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
pq.add(a);
pq.add(a);
}
int ans = 0, c = 0;
long ans = 0;
int c = 0;
if (pq.size() == 1)
ans += pq.poll();
while (pq.size() > 1) {
......
......@@ -7,16 +7,15 @@ import java.util.Arrays;
*/
public class bag {
public static void main(String[] args) {
// w(3);
System.out.println(dp());
System.out.println(dpByOne());
// System.out.println(dpByWan());
// System.out.println(dp());
// System.out.println(dpByOne());
System.out.println(dpByWan());
}
public static int n = 4;//物品数量
public static int W = 10;//背包容量
public static int[] w = {2, 1, 3, 2};//重量
public static int[] v = {3, 2, 4, 2};//价值
public static int W = 9;//背包容量
public static int[] w = {2, 2, 3, 2};//重量
public static int[] v = {3, 3, 4, 2};//价值
/**
* 01背包问题
......@@ -47,7 +46,7 @@ public class bag {
public static int dp() {
int[][] dp = new int[n][W + 1];
//初始化dp的第一行,第一行可选物品只有1个,能装上就装
for (int i = 0; i < W + 1; i++) {
for (int i = 0; i <= W; i++) {
if (i >= w[0]) {// 每种容量-0号物品
dp[0][i] = v[0];
} else {
......@@ -55,7 +54,7 @@ public class bag {
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < W + 1; j++) {
for (int j = 0; j <= W; j++) {
if (j >= w[i]) {//要的起
//选择当前物品,即i号物品,剩余容量。
int i1 = v[i] + dp[i - 1][j - w[i]];
......@@ -77,7 +76,7 @@ public class bag {
* 每次倒序更新,不然数据不能重用。
* 思路:定义一个数组作为辅助空间,初始为n+1,根据dp公式倒序更新
*
* @return
* @return 最大价值
*/
public static int dpByOne() {
int[] dp = new int[W + 1];
......@@ -92,16 +91,16 @@ public class bag {
}
/**
* 完全背包,但未经测试不知对错,肯定是错的
* 完全背包
* 单个商品可选多个
*
* @return
* @return 最大价值
*/
public static int dpByWan() {
int[] dp = new int[W + 1];
System.out.println(Arrays.toString(dp));
for (int i = 0; i < n; i++) {
for (int j = w[i + 1]; j <= n; j++) {
dp[j] = Math.max(dp[j], v[i + 1] + dp[j - w[i + 1]]);
for (int j = w[i]; j <= W; j++) {
dp[j] = Math.max(dp[j], v[i] + dp[j - w[i]]);
}
System.out.println(Arrays.toString(dp));
}
......@@ -112,14 +111,4 @@ public class bag {
}
public static void w(int n) {
int c[] = {1, 2, 3};
int dp[] = new int[n + 1];
for (int i =1; i <= n; i++) {
for (int j =0; j < 3; j++) {
dp[j] = dp[j] + dp[j - c[i]];
}
}
System.out.println(dp[n]);
}
}
......@@ -20,7 +20,7 @@ import java.util.Arrays;
* 递推方程
* 有了状态,也知道了问题之间的联系,其实递推方程也出来了,就是
* dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
* 有了这些,这道题还没完,我们还要考虑状态数组的初始化问题,对于上边界和左边界的点,因为它们只能从一个方向过来,需要单独考虑,比如上边界的点只能从左边这一个方向过来,左边界的点只能从上边这一个方向过来,它们的不同路径个数其实就只有 1,提前处理就好。
* 考虑状态数组的初始化问题,对于上边界和左边界的点,因为它们只能从一个方向过来,需要单独考虑,比如上边界的点只能从左边这一个方向过来,左边界的点只能从上边这一个方向过来,它们的不同路径个数其实就只有 1,提前处理就好。
*/
public class 机器人走方格 {
public static void main(String[] args) {
......
......@@ -28,7 +28,7 @@ import java.util.Scanner;
public class 装箱问题 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int v = sc.nextInt();//箱子容量
int v = sc.nextInt();//箱子容量消除
int n = sc.nextInt();//物品数量
int tiji[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
......
......@@ -5,8 +5,9 @@ import java.util.PriorityQueue;
/**
* POJ 3253
* Rence Repair
* https://blog.csdn.net/qq_40421671/article/details/83274031
* 农夫约翰为了修理栅栏,要将一块很长的木板分割成N块。
* 准备切成的木板的长度为L1、L2、……、Ln. 未切割木板的长度恰好为
* 准备切成的木板的长度为L1、L2、……、Ln. 未切割木板的长度恰好为
* 切割木板的长度和。每次切断木板时,需要的开销为这块木板的长度。
* 例如,长度为21的木板切割成5、8、8的三块木板。
* 长为21的木板切割成13、8时,开销为21.
......@@ -19,15 +20,15 @@ import java.util.PriorityQueue;
* N=3, L={8, 5, 8}
* 输出样例:
* 34
* 利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可
* ,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可
* 本题虽然利用了Huffman思想,但是直接用HuffmanTree做会超时,可以用优先队列做
* 因为朴素的HuffmanTree思想是:
* (1)先把输入的所有元素升序排序,再选取最小的两个元素,把他们的和值累加到总费用
* (2)把这两个最小元素出队,他们的和值入队,重新排列所有元素,重复(1),直至队列中元素个数<=1,
* 则累计的费用就是最小费用
* 则累计的费用就是最小费用
*/
public class 切木板 {
public static void main(String[] args) {
public static void main(String[] args) {
int[] arr = {8, 5, 8, 4, 3};
System.out.println(Zui(3, arr));
}
......@@ -47,3 +48,45 @@ public class 切木板 {
return ans;
}
}
/**
* #include <iostream>
* #include <algorithm>
* #include <queue>
* <p>
* using namespace std;
* <p>
* int n;
* int l[20001];
* <p>
* int main() {
* <p>
* while(~(scanf("%d", &n))) {
* <p>
* priority_queue<int, vector<int>, greater<int> > q;
* <p>
* for(int i=0; i<n; i++) {
* cin >> l[i];
* q.push(l[i]);
* }
* <p>
* long long ans = 0;
* while(q.size() > 1) {
* <p>
* int m1, m2;
* m1 = q.top();
* q.pop();
* m2 = q.top();
* q.pop();
* <p>
* int t = m1 + m2;
* ans += t;
* <p>
* q.push(t);
* }
* <p>
* cout << ans << endl;
* }
* <p>
* return 0;
* }
**/
\ No newline at end of file
package dp;
public class 多重部分和 {
public static void main(String[] args) {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册