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. Redis配置及使用

    1.参考资料 在线命令说明: http://doc.redisfans.com/ redis安装列表: https://github.com/rgl/redis/downloads Redis工具使用 ...

  2. Nginx模块Lua-Nginx-Module学习笔记(一)Nginx Lua API 接口详解

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一.介绍 各种* _by_lua,* _by_lua_block和* _by_lua_file配置指令用 ...

  3. Java获取精确到毫秒的时间戳

    import java.util.Date; public class Timestamp { /** 获取精确到毫秒的时间戳 * @param date * @return **/ public s ...

  4. Spring Cloud(十一)声名式服务调用:Feign的使用 (上)

    一.写在前边 最近开发任务比较忙,下班也开始锻炼了,这个系列的文章就放了很久,看github我提交的Feign的入门程序已经好久了,今天正好得空,这就更上一贴,准备分几部分写 注意:之前几个项目中,笔 ...

  5. Twitter.com在用哪些Javascript框架?

    我一直在研究 twitter.com 使用的一些 UI 框架.下面是这些框架的清单(大部分是 Javascript 框架).如果你发现有些框架 Twitter 已经没在使用了,请随时告诉我! 测试 J ...

  6. ActiveMQ监听消息并进行转发,监听不同的mq服务器和不同的队列

    工作中刚接触mq消息业务,其实也就是监听一下别的项目发送的消息然后进行对应的转发,但是监听的mq会有多个,而且转发的地址也可能有多个,这里就使用spring集成的方式!记录一下实现方式: 监听多个mq ...

  7. redis安装(linux)

    一.redis安装步骤 1.yum install gcc  如果你机器已经安装了编译环境请忽略,否则在使用make编译源码时会报错. 报错信息:make: *** [adlist.o]  2.使用w ...

  8. C# 日文网址转punnycode

    Uri uri = new Uri(url); IdnMapping idn = new IdnMapping();url= url.Replace(uri.Host, idn.GetAscii(ur ...

  9. 手动实现图片预览-放大缩小全屏支持IE9以上

    #{extends '/Index/index.html' /} #{set title:'意见反馈' /} <script src="/public/mgr/javascripts/ ...

  10. C#连接MySQL 操作步骤

    1.工具安装: 安装 MySQL For Windows,这个不多说,上官网下载: 安装mysql-connector-net,这个是MySQL数据库.NET开发驱动,因为C#是.NET架构的,所以需 ...