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 ...
随机推荐
- 【Cesium 颜狗初步】fabric 材质定义与自定义着色器实践
fabric 材质定义:着色器实践 1. 示例代码 贴到沙盒里就可以运行: var viewer = new Cesium.Viewer("cesiumContainer"); v ...
- 手把手教你:将 ClickHouse 集群迁至云上
前言 随着云上 ClickHouse 服务完善,越来越多的用户将自建 ClickHouse 服务迁移至云上.对于不同数据规模,我们选择不同的方案: 对于数据量比较小的表,通常小于10GB 情况下,可以 ...
- CDH集群spark-shell执行过程分析
目的 刚入门spark,安装的是CDH的版本,版本号spark-core_2.11-2.4.0-cdh6.2.1,部署了cdh客户端(非集群节点),本文主要以spark-shell为例子,对在cdh客 ...
- 刚入坑之C#《方法》解说
说好的用一周时间学方法,我都快耽误成两周了.原因就是跟着传智播客的课程做了个飞行棋项目,想要梳理其中的方法却把自己绕晕了.那接下来我先说一下我学到方法的内容,在最后献上飞行器项目的代码,当然是传智播客 ...
- [RoarCTF 2019]Easy Calc
[RoarCTF 2019]Easy Calc 题目 题目打开是这样的 查看源码 .ajax是指通过http请求加载远程数据. 可以发现有一个calc.php,输入的算式会被传入到这个php文件里,尝 ...
- Synchronized用法原理和锁优化升级过程(面试)
简介 多线程一直是面试中的重点和难点,无论你现在处于啥级别段位,对synchronized关键字的学习避免不了,这是我的心得体会.下面咱们以面试的思维来对synchronized做一个系统的描述,如果 ...
- 学习tomcat-如何建立连接,处理请求
tomcat如何建立连接,处理请求 学习探讨tomcat如何建立网络连接协议,并处理客户端过来的请求 建立http网络连接,指定通信协议 tomcat在创建时,会创建连接对象,负责处理客户端的请求,基 ...
- SecureCRT的下载、安装和Putty 的使用 SSH连接工具
SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.SecureCRT支持SSH,同时支持Telnet和rlog ...
- 根据租户id获取部门树状结构有父子结构的数据list
/** * 根据租户id获取部门树状结构 * @param tenantId * @return */ @GetMapping("getDeptTreeList") public ...
- java 反射随记
记录一下有关 Class 对象的相关方法: 1.获取 Class 对象的三个方法: ⑴ 使用 Class.forName("全限定类名") ,参数是该类的全限定类名,可拓展性强: ...