洛谷P1417 烹调方案【dp】
题目:https://www.luogu.org/problemnew/show/P1417
题意:
一道菜有$a,b,c$三个值。烧一道菜的时间是$c$。得到的价值是,$a-t*b$其中$t$是菜完成的时间。
问用总时间t可以烧多少菜使得总价值最大。
思路:
很容易可以想到背包,一道菜做或是不做。
即$dp[t][i] = max(dp[t][i-1], dp[t-c_i][i-1]+a_i-t*b_i)$
但是由于$t$会影响到菜的价值,也就是说菜的顺序也是有影响的。所以并不是简单的背包。
考虑两道菜$x,y$,分别考虑先做$x$和先做$y$的情况,所得到的价值分别是:
$a_x-t*b_x+a_y-(t+c_x)*b_y$和$a_y-t*b_y+a_x-(t+c_y)*b_x$,可以发现这里可以贪心。
也就是如果$c_x*b_y > c_y*b_x$那么x在前可以得到更大的价值。按着个排个序再进行背包。
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<iostream> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, t;
const int maxn = ;
const int maxt = ;
struct node{
LL a, b, c;
}food[maxn];
LL dp[maxt][maxn]; bool cmp(node a, node b)
{
return a.c * b.b < b.c * a.b;
} int main()
{
scanf("%d%d", &t, &n);
for(int i = ; i <= n; i++){
scanf("%lld", &food[i].a);
}
for(int i = ; i <= n; i++){
scanf("%lld", &food[i].b);
}
for(int i = ; i <= n; i++){
scanf("%lld", &food[i].c);
}
sort(food + , food + + n, cmp); for(int i = ; i <= n; i++){
for(int j = t; j >= ; j--){
if(j >= food[i].c)dp[j][i] = max(dp[j][i - ], dp[j - food[i].c][i - ] + food[i].a - (j) * food[i].b);
else dp[j][i] = dp[j][i - ];
}
}
LL ans = ;
for(int j = ; j <= t; j++){
ans = max(ans, dp[j][n]);
}
printf("%lld\n", ans); return ;
}
洛谷P1417 烹调方案【dp】的更多相关文章
- [洛谷P1417 烹调方案]贪心+dp
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3211Dream City Time Limit: 1 Second ...
- 洛谷1417 烹调方案 dp 贪心
洛谷 1417 dp 传送门 挺有趣的一道dp题目,看上去接近于0/1背包,但是考虑到取每个点时间不同会对最后结果产生影响,因此需要进行预处理 对于物品x和物品y,当时间为p时,先加x后加y的收益为 ...
- 洛谷P1417 烹调方案
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- 洛谷 P1417 烹调方案
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- 洛谷 P1417 烹调方案 题解
题面 这道题是一道典型的排序dp a[i]−b[i]∗(t+c[i])+a[j]−b[j]∗(t+c[i]+c[j]) a[j]−b[j]∗(t+c[j])+a[i]−b[i]∗(t+c[i]+c[j ...
- 洛谷 P1417烹调方案
题目大意: 一共有n件食材,每件食材有三个属性,ai,bi和ci,如果在t时刻完成第i样食材则得到ai-t*bi的美味指数,用第i件食材做饭要花去ci的时间. 求最大美味指数之和. 分析: 显然的0/ ...
- 洛谷 P1417 烹调方案 (01背包拓展)
一看到这道题就是01背包 但是我注意到价值和当前的时间有关. 没有想太多,直接写,0分 然后发现输入方式不对-- 改了之后只有25分 我知道wa是因为时间会影响价值,但不知道怎么做. 后来看了题解,发 ...
- luogu P1417 烹调方案 |dp
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- P1417 烹调方案 /// DP(假设 简化公式 排序)
题目大意: https://www.luogu.org/problemnew/show/P1417 题解 看第一份方法的公式 排序后01背包 #include <bits/stdc++.h> ...
随机推荐
- 《C程序设计语言》学习笔记
1. 在C程序中,如果字符串过长而需要跨行时,要在换行时加上“\”. printf("Hello, world"); // error printf("Hello, wo ...
- 利用css 画各种三角形
#triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: ...
- Oracle数据库连接工具的使用(一)
一.企业管理器(OEM) 1.介绍 Oracle企业管理器(Oracle Enterprise Manager)简称OEM,是一个基于Java的框架系统,该系统集成了多个组件,为用户提供了一个功能强大 ...
- IDEA插件之alibaba编程规范
1.做什么 这是阿里巴巴的编码规范插件,规范内容可以查阅 https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(华山版).pdf 2.File ...
- 在Linux环境下的对启动服务进行停止或在运行
下面我以elasticsearch服务为例进行: 第一种: 1.前台运行: 运行结果 2.ctrl+c停止运行 第二种:后端运行 1.后端运行的命令./elasticsearch -d 这种启动后 ...
- HDU 4417-Super Mario-线段树+离线
Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability ...
- print() 默认是打印完字符串会自动添加一个换行符
可以使用end=" " 参数告诉 print() 用空格代替换行 for i in range(1,10): ... print(i,end=' ') ... 1 2 3 4 5 ...
- hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...
- Django rest-framework框架-CBV原理
jdango中间件:class Test(View): def dispatch(self, request, *args, **kwargs): #第一种方法 func = getattr(self ...
- react portals 插槽 实现简易弹窗
Portal 提供了一种将子节点渲染到存在于父节点以外的DOM节点的优秀方案: 尽管 portal 可以被放置在 DOM 树中的任何地方,但在任何其他方面,其行为和普通的 React 子节点行为一致. ...