P2979 [USACO10JAN]奶酪塔Cheese Towers(完全背包,递推)
题目描述
Farmer John wants to save some blocks of his cows' delicious Wisconsin cheese varieties in his cellar for the coming winter. He has room for one tower of cheese in his cellar, and that tower's height can be at most T (1 <= T <= 1,000). The cows have provided him with a virtually unlimited number of blocks of each kind of N (1 <= N <= 100) different types of cheese (conveniently numbered 1..N). He'd like to store (subject to the constraints of height) the most
valuable set of blocks he possibly can. The cows will sell the rest to support the orphan calves association.
Each block of the i-th type of cheese has some value V_i (1 <= V_i <= 1,000,000) and some height H_i (5 <= H_i <= T), which is always a multiple of 5.
Cheese compresses. A block of cheese that has height greater than or equal to K (1 <= K <= T) is considered 'large' and will crush any and all of the cheese blocks (even other large ones) located below it in the tower. A crushed block of cheese doesn't lose any value, but its height reduces to just 4/5 of its old height. Because the height of a block of cheese is always a multiple of 5, the height of a crushed block of cheese will always be an integer. A block of cheese is either crushed or not crushed; having multiple large blocks above it does not crush it more. Only tall blocks of cheese crush other blocks; aggregate height of a tower does not affect whether a block is crushed or not.
What is the total value of the best cheese tower FJ can construct?
Consider, for example, a cheese tower whose maximum height can be 53 to be build from three types of cheese blocks. Large blocks are those that are greater than or equal to 25. Below is a chart of the values and heights of the various cheese blocks he stacks:
Type Value Height
1 100 25
2 20 5
3 40 10
FJ constructs the following tower:
Type Height Value
top -> [1] 25 100
[2] 4 20 <- crushed by [1] above
[3] 8 40 <- crushed by [1] above
[3] 8 40 <- crushed by [1] above
bottom -> [3] 8 40 <- crushed by [1] above
The topmost cheese block is so large that the blocks below it are crushed. The total height is:
25 + 4 + 8 + 8 + 8 = 53
The total height does not exceed 53 and thus is 'legal'. The total value is:
100 + 20 + 40 + 40 + 40 = 240.
This is the best tower for this particular set of cheese blocks.
要建一个奶酪塔,高度最大为T。他有N块奶酪。第i块高度为Hi(一定是5的倍数),价值为Vi。一块高度>=K的奶酪被称为大奶酪,一个奶酪如果在它上方有大奶酪(多块只算一次),它的高度就会变成原来的4/5.。 很显然John想让他的奶酪他价值和最大。求这个最大值。
输入输出格式
输入格式:
* Line 1: Three space-separated integers: N, T, and K
* Lines 2..N+1: Line i+1 contains two space separated integers: V_i and H_i
输出格式:
* Line 1: The value of the best tower FJ can build
输入输出样例
3 53 25
100 25
20 5
40 10
240
题意:
完全背包,如果当前奶酪的顶上还有符合条件的大奶酪时,当前奶酪的体积变成原来体积的$\frac{4}{5}$。
那么根据题目的意思,对于当前第i种奶酪,在他之前分为已经放了大奶酪和没放大奶酪两种情况来考虑,设f[j][0]表示当前体积为j,且之前没有放大奶酪;f[j][1]表示当前体积为j,且之前已经了放大奶酪
- 如果之前放了大奶酪,那么可以从f[j][1]转化到f[j+h[i]/5*4][1]
- 如果之前没有放大奶酪,且当前这个奶酪也不是大奶酪,那么可以从f[j][0]转化到f[j+h[i]][0]
- 如果之前没有放大奶酪,而当前这个奶酪恰好是大奶酪,那么可以从f[j][0]转化到f[j+h[i]][1]
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int f[1010][2];//体积,之前是否有大奶酪
int h[110],v[110];
int main() {
// freopen("C:/Users/Xzq/Desktop/p1.txt","r",stdin);
int n,t,k;
scanf("%d %d %d",&n,&t,&k);
for(int i=1; i<=n; i++) scanf("%d %d",&v[i],&h[i]);
memset(f,-1,sizeof(f));
//f[j][0],体积为j,之前没有大奶酪
//f[j][1],体积为j,之前有大奶酪
f[0][0]=0;//初始条件 for(int j=0;j<=t;j++){
for(int i=1;i<=n;i++){
//不是大奶酪,之前也没有大奶酪
if(j+h[i]<=t&&f[j][0]!=-1) f[j+h[i]][0]=max(f[j+h[i]][0],f[j][0]+v[i]);
//不是大奶酪,之前有大奶酪
if(j+h[i]/5*4<=t&&f[j][1]!=-1) f[j+h[i]/5*4][1]=max(f[j+h[i]/5*4][1],f[j][1]+v[i]);
//之前没有大奶酪,即将放上大奶酪
if(h[i]>=k&&f[j][0]!=-1&&h[i]+j<=t) f[h[i]+j][1]=max(f[h[i]+j][1],f[j][0]+v[i]);
}
} int ans=0;
for(int i=1;i<=t;i++) ans=max(ans,max(f[i][0],f[i][1])); printf("%d\n",ans);
return 0;
}
P2979 [USACO10JAN]奶酪塔Cheese Towers(完全背包,递推)的更多相关文章
- P2979 [USACO10JAN]奶酪塔Cheese Towers
P2979 [USACO10JAN]奶酪塔Cheese Towers 背包dp 不过多了一个大奶酪可以压扁其他奶酪的 一开始写了个暴力82分.贪心的选择 然后发现,有如下两种规律 要么最优都是小奶酪, ...
- 洛谷 P2979 [USACO10JAN]奶酪塔Cheese Towers
P2979 [USACO10JAN]奶酪塔Cheese Towers 题目描述 Farmer John wants to save some blocks of his cows' delicious ...
- UVA11137 Ingenuous Cubrency 完全背包 递推式子
做数论都做傻了,这道题目 有推荐,当时的分类放在了递推里面,然后我就不停的去推啊推啊,后来推出来了,可是小一点的数 输出答案都没问题,大一点的数 输出答案就是错的,实在是不知道为什么,后来又不停的看, ...
- POJ1958 Strange Towers of Hanoi [递推]
题目传送门 Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3117 Ac ...
- BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...
- 【POJ】2229 Sumsets(递推)
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 20315 Accepted: 7930 Descrip ...
- BZOJ2021: [Usaco2010 Jan]Cheese Towers
2021: [Usaco2010 Jan]Cheese Towers Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 184 Solved: 107[Su ...
- P1759 通天之潜水(不详细,勿看)(动态规划递推,组合背包,洛谷)
题目链接:点击进入 题目分析: 简单的组合背包模板题,但是递推的同时要刷新这种情况使用了哪些物品 ac代码: #include<bits/stdc++.h> using namespace ...
- P2347 砝码称重(动态规划递推,背包,洛谷)
题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...
随机推荐
- Jmeter之登录接口参数化实战
为了纪念我走过的坑(为什么有些简单的问题就是绊住我了,还是不够细啊) Jmeter之接口登录参数化实战 因为想要在登录时使用不同的数据进行测试,所以我选择了将数据进行参数化.因为涉及到新建一个接口的功 ...
- 是的,你没看错!Python可以实现自动化办公
是的,你没看错!Python可以实现自动化办公 公众号[伤心的辣条],如今越来越多的人加入到学习Python的队伍当中,尤其是对于很多职场人来说,不管你是程序员还是非程序员,Python已经为很多职场 ...
- 工具-Redis-与Python一起使用(99.6.3)
@ 目录 1.安装 2.使用 以下为对应的方法 3.使用string为例子 关于作者 1.安装 pip install redis 2.使用 pip install redis from redis ...
- P2240 【深基12.例1】部分背包问题
P2240 [深基12.例1]部分背包问题 题目描述 阿里巴巴走进了装满宝藏的藏宝洞.藏宝洞里面有 N(N \le 100)N(N≤100) 堆金币,第 ii 堆金币的总重量和总价值分别是 m_i,v ...
- ASP.NET Core 3.1使用log4net/nlog/Serilog记录日志
Serilog中的结构化日志支持非常好,而且配置简便.我能够比其他任何人更轻松地启动和运行Seirlog.Serilog中的日志可以发送到很多目的地.Serilog称这些东西为"接收器&qu ...
- MethodHandleVS反射
Method Handle与反射 如无特殊说明,本文所有代码均基于JDK1.8.0_221 Method Handle入门 反射我们都知道,为我们提供了运行时对类的成员方法访问的手段,极大地提高了Ja ...
- Unity UI适配 之 GridLayoutGroup组件下的内容适配(进度条适配)
好久没有更新博客了,蓝廋啊. 今天写一写关于GripLayoutGroup组件的屏幕适配问题,以在ARPG游戏中常用的经验条适配来举例子,以此来加深自己的记忆,以便在下次需要制作该功能时能够快速完成. ...
- easyui中清空table列表中数据
方法一 var item = $('#filegrid').datagrid('getRows');//获取类表中全部数据if (item) { for (var i = item.length - ...
- JS实现JSON数组合并和去重
var a=[{"id":"1001","name":"张三","age":"18&quo ...
- 听说又有兄弟因为用YYYY-MM-dd被锤了...
还记得去年分享过一篇日期格式化使用 YYYY-MM-dd 的潜在问题的文章不? 历史又重演了... 事故现场 我们来写个单元测试,重现一下这个问题. 测试逻辑: 创建两个日期格式化,一个是出问题的YY ...