USACO 5.3 Milk Measuring
Milk Measuring
Hal Burch
Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders.
Farmer John has always been frugal. He is at the cow hardware store where he must purchase a set of pails with which to measure out Q quarts of milk from his giant milk tank. Since the pails each cost the same amount, your task is to figure out a minimal set of pails Farmer John can purchase in order to fill a bottle with exactly Q quarts of milk. Additionally, since Farmer John has to carry the pails home, given two minimal sets of pails he should choose the "smaller" one as follows: Sort the sets in ascending order. Compare the first pail in each set and choose the set with the smallest pail. If the first pails match, compare the second pails and choose from among those, else continue until the two sets differ. Thus the set {3, 5, 7, 100} should be chosen over {3, 6, 7, 8}.
To measure out milk, FJ may completely fill a pail from the tank and pour it into the bottle. He can never remove milk from the bottle or pour milk anywhere except into the bottle. With a one-quart pail, FJ would need only one pail to create any number of quarts in a bottle. Other pail combinations are not so convenient.
Determine the optimally small number of pails to purchase, given the guarantee that at least one solution is possible for all contest input data.
PROGRAM NAME: milk4
INPUT FORMAT
Line 1: | The single integer Q |
Line 2: | A single integer P (1 <= P <= 100) which is the number of pails in the store |
Lines 3..P+2: | Each line contains a single integer pail_value (1 <= pail_value <= 10000), the number of quarts a pail holds |
SAMPLE INPUT (file milk4.in)
16
3
3
5
7
OUTPUT FORMAT
The output is a single line of space separated integers that contains:
- the minimum number of pails required to measure out the desired number of quarts, followed by:
- a sorted list (from smallest to largest) of the capacity of each of the required pails
SAMPLE OUTPUT (file milk4.out)
2 3 5 ——————————————————————————————————————————————————————题解
这道题是迭代深搜,但是以为是个记录状态的背包,死活没写出来
这道题记忆化搜索判断可行比纯dp要快,因为要用的状态都是跳跃幅度较大的,记忆化搜索反而用的状态比较少
思路和ax+by=1方程解然后不断得到新的差值为1的序列如果这个序列长度==最小的数,那么就回得到后面所有的数
http://www.cnblogs.com/ivorysi/p/6279637.html
所以这道题的个数不会太多,用DFSID+记搜
/*
ID: ivorysi
LANG: C++
PROG: milk4
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
//#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int q,p;
int f[];
int ld,used[],w[];
int flag=,cnt;
bool calc(int tmp) {
if(f[tmp]!=-) return f[tmp];
f[tmp]=;
siji(i,,p) {
if(used[i] && tmp%w[i]==) return f[tmp]=;
}
siji(i,,p) {
if(used[i] && tmp>=w[i]) {
f[tmp]=(f[tmp] || calc(tmp-w[i]));
if(f[tmp]==) return f[tmp];
}
}
return f[tmp];
}
void dfs(int d,int pr) {
if(flag) return;
if(d==ld){
siji(i,,q) f[i]=-;
f[]=;
if(calc(q)) {
flag=;
printf("%d ",ld);
siji(i,,p) {
if(used[i]) {
++cnt;
printf("%d%c",w[i]," \n"[cnt==ld]);
}
}
}
return;
}
siji(i,pr+,p) {
used[i]=;
dfs(d+,i);
if(flag) return;
used[i]=;
}
}
void solve() {
scanf("%d%d",&q,&p);
siji(i,,p) {
scanf("%d",&w[i]);
}
sort(w+,w+p+);
f[]=;
siji(i,,p) {
ld=i;
dfs(,);
if(flag) break;
}
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("milk4.in","r",stdin);
freopen("milk4.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.3 Milk Measuring的更多相关文章
- USACO Section 5.3 Milk Measuring (IDDFS+dp)
迭代加深搜索,从小到大枚举桶数的上限maxd:对每个maxd,枚举每个组合,判断是否能够倒出q:直到得到answer.判断的部分就用dp(完全背包). ------------------------ ...
- 洛谷 P2744 [USACO5.3]量取牛奶Milk Measuring
P2744 [USACO5.3]量取牛奶Milk Measuring 题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最 ...
- [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)
题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...
- [USACO5.3]量取牛奶Milk Measuring
https://daniu.luogu.org/problemnew/show/P2744 滚动数组压去第一维:前i种木桶 f[j] 量取体积j最少需要几种木桶 g[j] 体积j的最优解是否使用了第 ...
- luogu P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...
- 洛谷P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...
- 【洛谷2744 】【CJOJ1804】[USACO5.3]量取牛奶Milk Measuring
题面 Description 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少 ...
- [luoguP1494] 岳麓山上打水 && [luoguP2744] [USACO5.3]量取牛奶Milk Measuring
传送门 传送门 dfs选取集合,dp背包判断 虽然我觉的会TLE.. 但是的确是AC了 #include <cstdio> #include <cstring> #includ ...
- USACO 5.3 章节
相关讲解可在USACO上看原文,也可以搜索nocow找到翻译的! (nocow上有些微翻译是有问题的,如果想看nocow翻译的建议也对着英文看) 以下记录以下 自己之前未掌握的一些要点,以及按自己的括 ...
随机推荐
- day12 继承
设计原则:开闭原则:对于拓展open,对于修改close. 类与类的关系:1.is a(继承关系) 2.has a(组合关系) 继承的优点:1.代码的可重用性 2.父类的属性和方法用于子类 3.子类可 ...
- Java并发编程原理与实战十九:AQS 剖析
一.引言在JDK1.5之前,一般是靠synchronized关键字来实现线程对共享变量的互斥访问.synchronized是在字节码上加指令,依赖于底层操作系统的Mutex Lock实现.而从JDK1 ...
- Redis实战(一)CentOS 7上搭建redis-3.0.2
1.安装redis wget http://download.redis.io/releases/redis-3.0.2.tar.gz tar zxvf redis-3.0.2.tar.gz cd ...
- 悲催的IE6 七宗罪大吐槽(带解决方法)第一部分
一.奇数宽高 悲剧的IE6啊,为何有如此多bug,但用户市场又那么大,真让我们搞网站的纠结.今天就遇到了一个非常奇怪但又很细节的一个bug,一个外部的相对定位div,内部一个绝对定位的div(righ ...
- 基于受限玻尔兹曼机(RBM)的协同过滤
受限玻尔兹曼机是一种生成式随机神经网络(generative stochastic neural network), 详细介绍可见我的博文<受限玻尔兹曼机(RBM)简介>, 本文主要介绍R ...
- EF出错:Unable to convert MySQL date/time value to System.DateTime
环境: .Net 4.5 EF6 MySQL 错误提示: MySql.Data.Types.MySqlConversionException : Unable to convert MySQL dat ...
- 一个菜鸟的ASP.NET观光路线图
作为一个成长的"菜鸟".我的习惯是,每过一个阶段,都对自己的知识体系进行一次概括. 这篇博文是一个总结帖,我将把我的学到的东西,按照一定顺序串联在一起. ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- 【文件上传】文件上传的form表单提交方式和ajax异步上传方式对比
一.html 表单代码 …… <input type="file" class="file_one" name="offenderExcelFi ...
- 关于在函数中使用Array.prototype.slice.call而不是直接用slice
arguments是每个函数在运行的时候自动获得的一个近似数组的对象(除了length外没有其他属性),这个arguments对象其实并不是Array,所以没有slice方法. Array.proto ...