C++——【USACO 5.3.1】——Milk Measuring
来源:互联网 发布:python灰帽子 编辑:程序博客网 时间:2024/05/20 19:17
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 QLine 2:A single integer P (1 <= P <= 100) which is the number of pails in the storeLines 3..P+2:Each line contains a single integer pail_value (1 <= pail_value <= 10000), the number of quarts a pail holdsSAMPLE INPUT (file milk4.in)
163357
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
牛奶测量
农民约翰必须测量他最好的牛奶Q(1 <= Q <= 20000)夸脱,并把它装在一个大瓶子里给顾客。他把那个瓶子装满了顾客所订的数量。
农民约翰一直很节俭。他在奶牛五金店买了一套桶,用来测量从他那巨大的牛奶罐里的牛奶的Q夸脱。因为每个桶的价格都是一样的,你的任务是找出一套最小的一套。此外,由于农夫约翰必须把桶带回家,他要选择套最小的桶,他应该选择“更小的”,如下:按升序排列。比较每组的第一桶,选择最小的桶。如果第一桶是匹配的,比较第二桶,从其中选择,否则继续下去,直到两套不同。因此,应该在 {3、5、7、100}和{3、6、7、8}中选择 {3、5、7、100}。
为了测量牛奶,FJ可以完全把桶里的牛奶倒进瓶子里。但他不能从瓶子里取出牛奶,也不能把牛奶倒进瓶子里。有了一夸脱桶,FJ就只需要一个桶就可以在瓶子里制造任意数量的夸脱。其他的组合并不那么方便。
确定最优的购买数量,保证至少有一个解决方案。
项目名称:milk4
输入格式
第一行: 整数Q
第2行: 整数P(1 <= P <= 100),这是牛奶桶数量
行3 . .P+ 2:每一行包含一个整数(1 <= pi <= 10000),一个桶的容量
示例输入(文件milk4.in)
16
3
3
5
7
输出格式
输出包含:
测量所需数量的夸脱所需的最少的桶数,其次是:
一个排序的列表(从最小到最大),每个所需的桶的容量
样例输出(文件milk4.out)
2 3 5
/*ID : mcdonne1LANG : C++TASK : milk4*/#pragma GCC optimize("O3")#include <iostream>#include <fstream>#include <cstring>#include <algorithm>using namespace std;signed q, p, ans = 0x7fff;signed a[101], b[101], c[101];void dfs (signed x, signed y, signed z) {if (x > p or z > ans) return;b[z] = a[x];if (y % a[x] == 0) {if (z < ans) {ans = z; memcpy (c, b, sizeof(b)); }else {for (signed i = 1; i <= z; i++) if (c[i] ^ b[i]) {if (b[i] < c[i]) {ans = z; memcpy (c, b, sizeof(b));}break;}}}for (signed i = 1; i <= y/a[x]; i++) dfs (x + 1, y - a[x] * i, z + 1);dfs (x + 1, y, z);}int main () {ifstream fin("milk4.in", ios::in);ofstream fout("milk4.out", ios::out);fin>>q>>p;for (signed i = 1; i <= p; i++) fin>>a[i];sort (a + 1, a + 1 + p);dfs (1, q, 1);fout<<ans;for (signed i = 1; i <= ans; i++) fout<<" "<<c[i];fout<<endl;return 0;}
- C++——【USACO 5.3.1】——Milk Measuring
- usaco 5.3.1 Milk Measuring
- usaco training 5.3.1 Milk Measuring 题解
- usaco 5.3 Milk Measuring(背包)
- usaco——milk
- [usaco]5.3.2 Milk Measuring 动态规划
- USACO 5.3 Milk Measuring dfsid+dp
- USACO Milk Measuring 解题报告
- USACO Section 5.3 Milk Measuring - DFSID+DP...
- USACO——Mixing Milk 混合牛奶
- Section 5.3 Milk Measuring
- 洛谷 1028——[USACO 1.3]混合牛奶 Mixing Milk
- Milk Measuring
- usaco 1.3.1 milk
- DFS+DP验证 UScow 5.3.1 Milk Measuring 量取牛奶
- usaco5.3.1 Milk Measuring
- USACO 1.3.1 Mixing Milk (milk)
- HDU—— 1070 Milk
- Druid连接Ms SQL Server报地址端口被占用错误
- text/plain 与 text/xml有什么区别
- AJAX学习笔记
- UML类图与类的关系详解(基础知识)
- this指针的用法
- C++——【USACO 5.3.1】——Milk Measuring
- Java编程思想——初始化与清理
- 《深入理解java虚拟机》学习笔记5——Java Class类文件结构
- java8
- java为了自己
- 爬虫系列
- golang flag标准库命令行参数解析
- grunt和seajs入门之--提取依赖、合并、压缩js文件
- ASM(active shape model)算法学习总结