C++——【USACO 5.3.1】——Milk Measuring

来源:互联网 发布:python灰帽子 编辑:程序博客网 时间:2024/05/20 19:17

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 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 holds

SAMPLE 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;}


原创粉丝点击