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的更多相关文章

  1. USACO Section 5.3 Milk Measuring (IDDFS+dp)

    迭代加深搜索,从小到大枚举桶数的上限maxd:对每个maxd,枚举每个组合,判断是否能够倒出q:直到得到answer.判断的部分就用dp(完全背包). ------------------------ ...

  2. 洛谷 P2744 [USACO5.3]量取牛奶Milk Measuring

    P2744 [USACO5.3]量取牛奶Milk Measuring 题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最 ...

  3. [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)

    题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...

  4. [USACO5.3]量取牛奶Milk Measuring

    https://daniu.luogu.org/problemnew/show/P2744 滚动数组压去第一维:前i种木桶 f[j] 量取体积j最少需要几种木桶 g[j]  体积j的最优解是否使用了第 ...

  5. luogu P2744 [USACO5.3]量取牛奶Milk Measuring

    题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...

  6. 洛谷P2744 [USACO5.3]量取牛奶Milk Measuring

    题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...

  7. 【洛谷2744 】【CJOJ1804】[USACO5.3]量取牛奶Milk Measuring

    题面 Description 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少 ...

  8. [luoguP1494] 岳麓山上打水 && [luoguP2744] [USACO5.3]量取牛奶Milk Measuring

    传送门 传送门 dfs选取集合,dp背包判断 虽然我觉的会TLE.. 但是的确是AC了 #include <cstdio> #include <cstring> #includ ...

  9. USACO 5.3 章节

    相关讲解可在USACO上看原文,也可以搜索nocow找到翻译的! (nocow上有些微翻译是有问题的,如果想看nocow翻译的建议也对着英文看) 以下记录以下 自己之前未掌握的一些要点,以及按自己的括 ...

随机推荐

  1. Java基础-Date类常用方法介绍

    Java基础-Date类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.毫秒值概念 我们在查阅Date类的API文档时,会发现这样的一句话:"The cl ...

  2. windows下用wubi快速安装ubuntu

    由于开发需要,我们可能要用到ubuntu,然而又不能完全抛弃windows,于是双系统是个不错选择. wubi是一个在windows下快速安装ubuntu双系统的工具,它包含在ubuntu 12及以前 ...

  3. js+css3实现旋转效果

    我的前面一张文章实现了用css3制作旋转的效果,现在呢,我换另外一种方法来实现.就是使用js结合css3的方法来实现的.下面我就先上图给大家看看效果吧 下面呢我先放上我的css代码,代码很简单: .o ...

  4. js检测上传文件大小

    前言: 项目中经常用到需要上传文件.照片等功能,同时需要限制所上传文件的大小.很多插件都会采用后台请求验证,前端Js校验比较少.本篇介绍一个前端JS便捷判断上传文件大小的方法. 代码很简单,关键就是怎 ...

  5. Spring IOC 容器

    <bean name="userBean" class="com.nuts.demo.spring.UserBean"> <property ...

  6. [vmware]另类解决vmware关闭win10死机或蓝屏问题

    升级win10后在使用虚拟机发生一个问题,本人的win10版本为win10 9879, 在使用vmware时,当关机会整个系统死机,在网上搜索后发现这是由于win10内核升级导致vmware不兼容,最 ...

  7. Druid.io启用SQL支持

    Druid.io的SQL功能虽然在试验阶段,但是也支持了大部分的功能,而且还可以通过 Avatica JDBC查看请求的json,有助于我们理解Druid.io的语法.Druid.io有个比较坑的是, ...

  8. pta 一

    7-1 打印沙漏 (20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数 ...

  9. BFS 两个重要性质

    对于进行广度优先搜索的队列中,应该始终满足两个性质:   性质1:若队首为第i层拓展到的节点,则队列中最多只能存在第i层和第i+1层的节点,不可能出现3层节点.   性质2:队列中的元素会严格按照层数 ...

  10. C# 反射获取和设置值

    /// <summary> /// 遍历泛型 /// </summary> /// <typeparam name="T"></typep ...