zoukankan      html  css  js  c++  java
  • Coins POJ

    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
    
    题解:dp[i+1][j]:=用前i种数加和得到j时第i种数最多能剩余多少个(不能加和得到i的情况下为-1)(摘自《挑战》,话说个人对这种定义方式很懵逼)按照上述定义递推关系,这样如果前i-1
    个数能加和得到j的话,第i个数就可以留下Mi个。此外,前i种数加和出j-Ai时第i种数还剩下K(K>0)的话,用这i种数加和j时第i种数就剩下K-1个。
    复杂度O(nK)
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=100005;
     8 
     9 int n,m;
    10 int va[105],vo[105];
    11 int dp[maxn];
    12 
    13 void solve()
    14 {   memset(dp,-1,sizeof(dp));
    15     dp[0]=0;
    16     for(int i=1;i<=n;i++){
    17         for(int j=0;j<=m;j++){
    18             if(dp[j]>=0) dp[j]=vo[i];
    19             else if(j<va[i]||dp[j-va[i]]<=0) dp[j]=-1;
    20             else dp[j]=dp[j-va[i]]-1;
    21         }
    22     }
    23     int ans=0;
    24     for(int i=1;i<=m;i++) if(dp[i]>=0) ans++;
    25     cout<<ans<<endl;
    26 }
    27 
    28 int main()
    29 {   while(~scanf("%d%d",&n,&m)){
    30         if(n==0&&m==0) break;
    31         for(int i=1;i<=n;i++) scanf("%d",&va[i]);
    32         for(int i=1;i<=n;i++) scanf("%d",&vo[i]);
    33         
    34         solve();
    35     }
    36     return 0;
    37 } 
  • 相关阅读:
    OpenStack对象存储管理手册(5) OpenStack对象存储系统管理3
    hdu1568Fibonacci
    智能手机中显示信号强度格数
    上篇日本人经营之道 一破坏对手的情绪乱其方寸
    基于Wince的计算器程序
    shell 字符串操作
    轻松记住大端小端的含义
    如何把手机变成你的救生设备
    PM是一个事实
    mysql 5.6 online ddl 测试
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7287416.html
Copyright © 2011-2022 走看看