首页 > 其他 > 详细

【USACO 5.3.2】Milk Measuring

时间:2015-12-11 23:45:45      阅读:277      评论:0      收藏:0      [点我收藏+]
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

ANALYSIS

迭代加深搜索+DP检验答案。USACO官方标程好像用了一种很巧妙的DP,然而我并没有看完。
等我哪天弄懂了再写个DP吧。
/*
TASK:milk4
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int q, n, bv[105], need[105], maxd;
bool f[20005];

bool dp(int x)
{
    memset(f, false, sizeof(f));
    f[0] = true;
    for (int i = 0; i < maxd; ++i)
        for (int j = need[i]; j <= q; ++j)
            f[j] = f[j] || f[j - need[i]];
    return f[q];
}

bool dfsid(int s, int last)
{
    if (s == maxd)
    {
        if (dp(0))
        {
            printf("%d", maxd);
            for (int i = 0; i < maxd; ++i) printf(" %d", need[i]);
            printf("\n");
            return true;
        }
        return false;
    }
    for (int i = last; i < n; ++i)
    {
        need[s] = bv[i];
        if (dfsid(s + 1, i + 1)) return true;
    }
    return false;
}

int main()
{
    freopen("milk4.in", "r", stdin);
    freopen("milk4.out", "w", stdout);
    scanf("%d%d", &q, &n);
    for (int i = 0; i < n; ++i)
        scanf("%d", &bv[i]);
    sort(bv, bv + n);
    for (maxd = 1; maxd <= n; ++maxd)
        if (dfsid(0, 0)) break;
    return 0;
}

 

【USACO 5.3.2】Milk Measuring

原文:http://www.cnblogs.com/albert7xie/p/5040385.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!