usaco5.3.1 Milk Measuring

来源:互联网 发布:手机淘宝新品上架模块 编辑:程序博客网 时间:2024/06/06 07:37

一 原题

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


二 分析

完全背包,不过需要从一个大集合中选定使用哪些元素作为背包集合。使用迭代加深的方法枚举背包集合,对于一个背包集合,DP判断是否可行。


三 代码

运行结果:

USER: Qi Shen [maxkibb3]TASK: milk4LANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.011 secs, 4204 KB]   Test 2: TEST OK [0.000 secs, 4204 KB]   Test 3: TEST OK [0.000 secs, 4204 KB]   Test 4: TEST OK [0.000 secs, 4204 KB]   Test 5: TEST OK [0.000 secs, 4204 KB]   Test 6: TEST OK [0.000 secs, 4204 KB]   Test 7: TEST OK [0.205 secs, 4204 KB]   Test 8: TEST OK [0.130 secs, 4204 KB]   Test 9: TEST OK [0.054 secs, 4204 KB]   Test 10: TEST OK [0.497 secs, 4204 KB]All tests OK.

YOUR PROGRAM ('milk4') WORKED FIRST TIME! That's fantastic-- and a rare thing. Please accept these special automatedcongratulations.


AC代码:

/*ID:maxkibb3LANG:C++PROB:milk4*/#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int MAXQ = 20005;const int MAXP = 105;int Q, P;int C[MAXP];vector<int> Ans;bool Vis[MAXP];bool Dp[MAXQ];void init() {    scanf("%d%d", &Q, &P);    for(int i = 0; i < P; i++)        scanf("%d", &C[i]);    sort(C, C + P);}bool check() {    memset(Dp, 0, sizeof(Dp));    int s = 0, ssize = Ans.size();    for(int i = 0; i < ssize; i++)        s += Ans[i];    if(s > Q) return false;    Dp[s] = true;    for(int i = s + 1; i <= Q; i++) {        for(int j = 0; j < ssize; j++) {            Dp[i] = Dp[i] || Dp[i - Ans[j]];            if(Dp[i]) break;        }    }    return Dp[Q];}bool dfsid(int _num) {    if(_num == 0) {        bool ret = check();        if(ret) {            printf("%d", (int)Ans.size());            for(int i = 0; i < Ans.size(); i++)                printf(" %d", Ans[i]);            printf("\n");        }        return ret;    }    for(int i = 0; i < P; i++) {        if(Vis[i]) continue;        Vis[i] = true;        Ans.push_back(C[i]);        if(dfsid(_num - 1)) return true;        Ans.pop_back();        Vis[i] = false;    }    return false;}void solve() {    for(int i = 1; i <= P; i++) {        memset(Vis, 0, sizeof(Vis));        if(dfsid(i)) break;    }}int main() {    freopen("milk4.in", "r", stdin);    freopen("milk4.out", "w", stdout);    init();    solve();    return 0;}


0 0