zoukankan      html  css  js  c++  java
  • POJ_1742_Coins_(动态规划,多重部分和)

    描述


    http://poj.org/problem?id=1742

    n种不同面额的硬币 ai ,每种各 mi 个,判断可以从这些数字值中选出若干使它们组成的面额恰好为 k 的 k 的个数.

    原型:

    n种不同大小的数字 ai ,每种各 mi 个,判断是否可以从这些数字之中选出若干使它们的和恰好为 k .

    Coins
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 33732   Accepted: 11453

    Description

    People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
    You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

    Input

    The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

    Output

    For each test case output the answer on a single line.

    Sample Input

    3 10
    1 2 4 2 1 1
    2 5
    1 4 2 1
    0 0
    

    Sample Output

    8
    4
    

    Source

    分析


    图文详解:

    http://www.hankcs.com/program/cpp/poj-1742-coins.html

    用f[j]表示在第i次循环时,用前i-1中硬币组成面额j,第i-1中硬币还剩多少个,如果不能组成面额j,则f[j]=-1.

    最后统计f[j]>=0(1<=j<=m)的个数即可.

    注意:

    1.起始时f[0]=0,表示刚开始可以组成面额0,且之前啥也不剩.

    2.之后每次j要从0开始循环,要更新组成面额0(也就是啥都不组成)之后,第i-1种硬币还剩多少个.

    3.bind1st表示一元什么什么...也可以写成 " bind2nd(greater_equal<int>(),0) ",这东西貌似在<set>头文件里.

    ps.前几天才看过书上的多重部分和问题,这题简直就是模板,然而几乎忘得一干二净了,走马观花真是啥用都没有= =只有真正想明白了才算掌握了一点吧.

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<set>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=105,maxm=100005;
     8 int n,m;
     9 int a[maxn],c[maxn],f[maxm];
    10 
    11 void solve()
    12 {
    13     memset(f,-1,sizeof(f));
    14     f[0]=0;
    15     for(int i=1;i<=n;i++)
    16     {
    17         for(int j=0;j<=m;j++)
    18         {
    19             if(f[j]>=0)
    20             {
    21                 f[j]=c[i];
    22             }
    23             else if(a[i]>j||f[j-a[i]]<=0)
    24             {
    25                 f[j]=-1;
    26             }
    27             else
    28             {
    29                 f[j]=f[j-a[i]]-1;
    30             }
    31         }
    32     }
    33     int ans=count_if(f+1,f+m+1,bind1st(less_equal<int>(),0));//统计满足f[j]>=0的个数
    34     printf("%d
    ",ans);
    35 }    
    36 
    37 void init()
    38 {
    39     while(scanf("%d%d",&n,&m)==2&&(n!=0||m!=0))
    40     {
    41         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    42         for(int i=1;i<=n;i++) scanf("%d",&c[i]);
    43         solve();
    44     }
    45 }
    46 
    47 int main()
    48 {
    49 #ifndef ONLINE_JUDGE
    50     freopen("coin.in","r",stdin);
    51     freopen("coin.out","w",stdout);
    52 #endif
    53     init();
    54 #ifndef ONLINE_JUDEG
    55     fclose(stdin);
    56     fclose(stdout);
    57     system("coin.out");
    58 #endif
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    CSS样式表
    lianxi!
    传值
    lei!
    3.10
    if else&& stwitch break
    if else 语句
    2016.3.6
    进制转换
    PHP 面向对象的三大特征
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5428732.html
Copyright © 2011-2022 走看看