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 ...
随机推荐
- C#9.0新特性详解系列之六:增强的模式匹配
自C#7.0以来,模式匹配就作为C#的一项重要的新特性在不断地演化,这个借鉴于其小弟F#的函数式编程的概念,使得C#的本领越来越多,C#9.0就对模式匹配这一功能做了进一步的增强. 为了更为深入和全面 ...
- Go语言(1)——程序结构
程序结构 基础部分仅仅列举和其他语言不一样的地方(C语言为例). 声明 Go语言有四个主要声明:var.const.type.func,类似于C语言中的变量,常量,结构体和函数. package ma ...
- kali2020 装不上docker
问题描述 如图,不支持i386体系架构 所以百度了一下啥是i386,以及docker支持哪些体系架构 官网的截图如下: 然后我发现我一直以为我装的是64位debian,实际上我装的是32位的 补充一下 ...
- Phthon几个特殊的函数
Python有几个相对特殊的函数,他们并不会提高工作效率,但是会使代码优雅简洁,其中包括lambda, map, reduce, filter, yeild. 第一:lambda,贴些代码体会. 1 ...
- 容器编排系统K8s之PV、PVC、SC资源
前文我们聊到了k8s中给Pod添加存储卷相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14180752.html:今天我们来聊一下持久存储卷相关话题 ...
- x264编码demo定制修改介绍
x264编码器,提供了两个demo来验证编码功能:一个是大而全的x264.c,另外一个是简洁版的example.c. 其中,前者demo,可以配置很多编码参数,但太冗长繁杂,对初学者不太友好. 后者 ...
- Liunx运维(六)-文件备份与压缩命令
文档目录: 一.tar:打包备份 二.gzip:压缩或解压文件 三.zip:打包和压缩文件 四.unzip:解压zip文件 五.scp:远程文件复制 六.rsync:文件同步工具 ---------- ...
- jrebel 启动失败的处理
jrebel 启动失败的处理 今天使用 jrebel 启动项目的时候,突然啥日志都没有,只有一句Disconnected from the target VM, address: '127.0.0.1 ...
- vue结合element-ui table本地分页
<template> <el-table :data="tableData1.slice((start1-1)*length1,start1*length1)" ...
- javap使用
在反编译前你当然需要先编译这个类了进入当前目录下:javac -g SynchronizedTest.java(使用-g参数是因为要得到下面javap -l时的输出需要使用此选项) 编译完成后,我们在 ...