zoukankan      html  css  js  c++  java
  • USACO 5.3 Milk Measuring

    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+记搜
     1 /*
     2 ID: ivorysi
     3 LANG: C++
     4 PROG: milk4
     5 */
     6 #include <iostream>
     7 #include <cstdio>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <queue>
    11 #include <set>
    12 #include <vector>
    13 #include <string.h>
    14 #include <cmath>
    15 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
    16 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
    17 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
    18 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
    19 #define inf 0x3f3f3f3f
    20 //#define ivorysi
    21 #define mo 97797977
    22 #define hash 974711
    23 #define base 47
    24 #define pss pair<string,string>
    25 #define MAXN 5000
    26 #define fi first
    27 #define se second
    28 #define pii pair<int,int>
    29 #define esp 1e-8
    30 typedef long long ll;
    31 using namespace std;
    32 int q,p;
    33 int f[20005];
    34 int ld,used[105],w[105];
    35 int flag=0,cnt;
    36 bool calc(int tmp) {
    37     if(f[tmp]!=-1) return f[tmp];
    38     f[tmp]=0;
    39     siji(i,1,p) {
    40         if(used[i] && tmp%w[i]==0) return f[tmp]=1; 
    41     }
    42     siji(i,1,p) {
    43         if(used[i] && tmp>=w[i]) {
    44             f[tmp]=(f[tmp] || calc(tmp-w[i]));
    45             if(f[tmp]==1) return f[tmp];
    46         }
    47     }
    48     return f[tmp];
    49 }
    50 void dfs(int d,int pr) {
    51     if(flag) return;
    52     if(d==ld){ 
    53         siji(i,1,q) f[i]=-1;
    54         f[0]=1;
    55         if(calc(q)) {
    56             flag=1;
    57             printf("%d ",ld);
    58             siji(i,1,p) {
    59                 if(used[i]) {
    60                     ++cnt;
    61                     printf("%d%c",w[i]," 
    "[cnt==ld]);
    62                 }
    63             }
    64         }
    65         return;
    66     }
    67     siji(i,pr+1,p) {
    68         used[i]=1;
    69         dfs(d+1,i);
    70         if(flag) return;
    71         used[i]=0;
    72     }
    73 }
    74 void solve() {
    75     scanf("%d%d",&q,&p);
    76     siji(i,1,p) {
    77         scanf("%d",&w[i]);
    78     }
    79     sort(w+1,w+p+1);
    80     f[0]=1;
    81     siji(i,1,p) {
    82         ld=i;
    83         dfs(0,0);
    84         if(flag) break;
    85     }
    86 } 
    87 int main(int argc, char const *argv[])
    88 {
    89 #ifdef ivorysi
    90     freopen("milk4.in","r",stdin);
    91     freopen("milk4.out","w",stdout);
    92 #else
    93     freopen("f1.in","r",stdin);
    94 #endif
    95     solve();
    96     return 0;
    97 }
     
  • 相关阅读:
    IDL变量和数组使用
    IDL程序部署
    silverlight连接数据库的四种方法
    ENVI 5.0安装教程
    ArcGIS Server发布地图时提示打包失败解决方法
    ArcGIS Server服务未发布成功,怎么删除?
    Win8磁盘100% Win8磁盘占用100%解决办法终结版(3)
    攻防世界 reverse 进阶 -gametime
    攻防世界 reverse 进阶 16-zorropub
    攻防世界 reverse 进阶 15-Reversing-x64Elf-100
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6391907.html
Copyright © 2011-2022 走看看