题目一
// 分组背包(模版)
// 给定一个正数m表示背包的容量,有n个货物可供挑选
// 每个货物有自己的体积(容量消耗)、价值(获得收益)、组号(分组)
// 同一个组的物品只能挑选1件,所有挑选物品的体积总和不能超过背包容量
// 怎么挑选货物能达到价值最大,返回最大的价值
// 测试链接 : https://www.luogu.com.cn/problem/P1757
// 请同学们务必参考如下代码中关于输入、输出的处理
// 这是输入输出处理效率很高的写法
// 提交以下的所有代码,并把主类名改成”Main”,可以直接通过
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| public class Code01_PartitionedKnapsack {
public static int MAXN = 1001;
public static int MAXM = 1001;
public static int[][] arr = new int[MAXN][3];
public static int[] dp = new int[MAXM];
public static int m, n;
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer in = new StreamTokenizer(br); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); while (in.nextToken() != StreamTokenizer.TT_EOF) { m = (int) in.nval; in.nextToken(); n = (int) in.nval; for (int i = 1; i <= n; i++) { in.nextToken(); arr[i][0] = (int) in.nval; in.nextToken(); arr[i][1] = (int) in.nval; in.nextToken(); arr[i][2] = (int) in.nval; } Arrays.sort(arr, 1, n + 1, (a, b) -> a[2] - b[2]); out.println(compute1()); } out.flush(); out.close(); br.close(); }
public static int compute1() { int teams = 1; for (int i = 2; i <= n; i++) { if (arr[i - 1][2] != arr[i][2]) { teams++; } } int[][] dp = new int[teams + 1][m + 1]; for (int start = 1, end = 2, i = 1; start <= n; i++) { while (end <= n && arr[end][2] == arr[start][2]) { end++; } for (int j = 0; j <= m; j++) { dp[i][j] = dp[i - 1][j]; for (int k = start; k < end; k++) { if (j - arr[k][0] >= 0) { dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - arr[k][0]] + arr[k][1]); } } } start = end++; } return dp[teams][m]; }
|
题目二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package class074;
import java.util.List;
public class Code02_MaximumValueOfKcoinsFromPiles {
public static int maxValueOfCoins1(List<List<Integer>> piles, int m) { int n = piles.size(); int[][] dp = new int[n + 1][m + 1]; for (int i = 1; i <= n; i++) { List<Integer> team = piles.get(i - 1); int t = Math.min(team.size(), m); int[] preSum = new int[t + 1]; for (int j = 0, sum = 0; j < t; j++) { sum += team.get(j); preSum[j + 1] = sum; } for (int j = 0; j <= m; j++) { dp[i][j] = dp[i - 1][j]; for (int k = 1; k <= Math.min(t, j); k++) { dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - k] + preSum[k]); } } } return dp[n][m]; }
|
题目三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| package class074;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Arrays;
public class Code03_UnboundedKnapsack {
public static int MAXM = 10001;
public static int MAXT = 10000001;
public static int[] cost = new int[MAXM];
public static int[] val = new int[MAXM];
public static long[] dp = new long[MAXT];
public static int t, m;
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer in = new StreamTokenizer(br); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); while (in.nextToken() != StreamTokenizer.TT_EOF) { t = (int) in.nval; in.nextToken(); m = (int) in.nval; for (int i = 1; i <= m; i++) { in.nextToken(); cost[i] = (int) in.nval; in.nextToken(); val[i] = (int) in.nval; } out.println(compute2()); } out.flush(); out.close(); br.close(); }
public static long compute1() { int[][] dp = new int[m + 1][t + 1]; for (int i = 1; i <= m; i++) { for (int j = 0; j <= t; j++) { dp[i][j] = dp[i - 1][j]; if (j - cost[i] >= 0) { dp[i][j] = Math.max(dp[i][j], dp[i][j - cost[i]] + val[i]); } } } return dp[m][t]; }
public static long compute2() { Arrays.fill(dp, 1, t + 1, 0); for (int i = 1; i <= m; i++) { for (int j = cost[i]; j <= t; j++) { dp[j] = Math.max(dp[j], dp[j - cost[i]] + val[i]); } } return dp[t]; }
}
|
题目四
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package class074;
public static boolean isMatch3(String str, String pat) { char[] s = str.toCharArray(); char[] p = pat.toCharArray(); int n = s.length; int m = p.length; boolean[][] dp = new boolean[n + 1][m + 1]; dp[n][m] = true; for (int j = m - 1; j >= 0; j--) { dp[n][j] = j + 1 < m && p[j + 1] == '*' && dp[n][j + 2]; } for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { if (j + 1 == m || p[j + 1] != '*') { dp[i][j] = (s[i] == p[j] || p[j] == '.') && dp[i + 1][j + 1]; } else { dp[i][j] = dp[i][j + 2] || ((s[i] == p[j] || p[j] == '.') && dp[i + 1][j]); } } } return dp[0][0]; }
|