当前位置:网站首页>【每日一题】搬运工 (DFS / DP)
【每日一题】搬运工 (DFS / DP)
2022-07-06 08:59:00 【Easenyang】
题目描述
前些天,高一的新同学来了,他便像往常一样兜售他的书,经过一番口舌,同学们决定买他的书,但是陈老师桌上的书有三堆,每一堆都有厚厚的一叠,他要想个办法用最轻松的方式把书拿下来给同学们。但是你想逗一下陈老师,于是你设计一个最累的方式给他。若告诉你这三堆分别有i,j,k 本书,以及每堆从下到上书的质量,每次取书只能从任一堆的最上面取,那么请你设计一个方案,让他花最大的力气取下所有的书。
显然,每次取书陈老师的体力消耗都会加大,这里用体力系数代表,取下第一本书时,体力系数为 1,第二本书时体力系数为2,依次类推,而每次体力消耗值则为体力系数与书的重量之积。输入格式
第一行有三个整数,分别为三堆书的数量 i,j,k。
第二行至第四行分别为每堆由下至上的书本重量。
输出格式
输出最累方式的体力消耗总值。
输入样例
3 2 4
2 3 2
1 5
9 8 7 4
输出样例
257
题目链接:搬运工
思路1:(DFS)
可以去搜索每种取书的情况,记录各种情况的体力消耗,取最大值
代码:
import java.util.Scanner;
public class Main {
static int i, j, k;
static int[] num1,num2,num3;//存放三堆书的数组
static int res = 0; //最终结果
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
i = sc.nextInt(); j = sc.nextInt() ; k = sc.nextInt();
num1 = new int[i+1]; num2 = new int[j+1]; num3 = new int[k+1];
for (int a = 1; a <= i; a++) num1[a] = sc.nextInt();
for (int a = 1; a <= j; a++) num2[a] = sc.nextInt();
for (int a = 1; a <= k; a++) num3[a] = sc.nextInt();
dfs(0, 1);
System.out.println(res);
}
/** * @param temp 记录某种方式取书的体力消耗 * @param t 当前体力系数 */
public static void dfs(int temp, int t) {
if (i + j + k ==0) {
//都为0时表示取完了,更新结果
res = Math.max(res, temp);
return;
}
if (i > 0) {
//该堆有剩余,才能继续取本堆的
temp += t * num1[i--]; //取的情况
dfs(temp, t+1);
temp -= t * num1[++i]; //不取则回溯
}
if (j > 0) {
temp += t * num2[j--];
dfs(temp, t+1);
temp -= t * num2[++j];
}
if (k > 0) {
temp += t * num3[k--];
dfs(temp, t+1);
temp -= t * num3[++k];
}
}
}
思路2:(DP)
用dp[a][b][c]表示取三堆书的数目分别为a,b,c时,体力的最大消耗,它为dp[a-1][b][c] + num1[a] * t ,dp[a][b-1][c] + num2[b] * t 和 dp[a][b][c-1] + num3[c] * t的最大值其中 t 为当前体力系数
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt(), j = sc.nextInt(), k = sc.nextInt();
int sum = i + j + k; //书的总数
int[] num1 = new int[i+1], num2 = new int[j+1], num3 = new int[k+1];
for (int l = 1; l <= i; l++) num1[l] = sc.nextInt();//存放每堆数的每本书重量
for (int l = 1; l <= j; l++) num2[l] = sc.nextInt();
for (int l = 1; l <= k; l++) num3[l] = sc.nextInt();
int[][][] dp = new int[i+1][j+1][k+1];
for (int a = 0; a <= i; a++)
for (int b = 0; b <= j; b++)
for (int c = 0; c <= k; c++) {
//当前体力系数为书的总数减去a,b,c的值加 1
int t = sum - a - b - c + 1;
if (a > 0)
dp[a][b][c] = dp[a-1][b][c] + num1[a] * t;
if (b > 0)
dp[a][b][c] = Math.max(dp[a][b-1][c] + num2[b] * t, dp[a][b][c]);
if (c > 0)
dp[a][b][c] = Math.max(dp[a][b][c-1] + num3[c] * t, dp[a][b][c]);
}
System.out.println(dp[i][j][k]);
}
}
边栏推荐
- KDD 2022论文合集(持续更新中)
- Esp8266-rtos IOT development
- Computer graduation design PHP Zhiduo online learning platform
- Philosophical enlightenment from single point to distributed
- Using C language to complete a simple calculator (function pointer array and callback function)
- LeetCode:394. String decoding
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- BN折叠及其量化
- 多元聚类分析
- CSP first week of question brushing
猜你喜欢

Mise en œuvre de la quantification post - formation du bminf

Detailed explanation of dynamic planning

【剑指offer】序列化二叉树

LeetCode:221. 最大正方形

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

LeetCode:236. 二叉树的最近公共祖先

CUDA realizes focal_ loss

I-BERT

Navicat premium create MySQL create stored procedure
![[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)](/img/af/a44c2845c254e4f48abde013344c2b.png)
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
随机推荐
Intel distiller Toolkit - Quantitative implementation 1
Excellent software testers have these abilities
Pytest's collection use case rules and running specified use cases
BN folding and its quantification
多元聚类分析
LeetCode:236. 二叉树的最近公共祖先
Notes 01
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
超高效!Swagger-Yapi的秘密
MySQL uninstallation and installation methods
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
IJCAI2022论文合集(持续更新中)
Leetcode: Jianzhi offer 03 Duplicate numbers in array
Mongodb installation and basic operation
LeetCode:剑指 Offer 03. 数组中重复的数字
[oc]- < getting started with UI> -- common controls uibutton
Leetcode: Sword finger offer 48 The longest substring without repeated characters
KDD 2022 paper collection (under continuous update)
LeetCode:387. The first unique character in the string
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find