zoukankan      html  css  js  c++  java
  • 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)

    Problem Description

    Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse 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
    解题思路:这题用二进制解法在poj却超时了,而杭电却不会(单调队列就别想了-->TLE);考虑多重部分和问题,时间复杂度是O(nW)。
    hdu 2844 #AC代码(343ms):有坑,测试数据种W有为负数的,这就是一直RE的原因=_=...简单判断一下即可。
     1 #include<algorithm>
     2 #include<string.h>
     3 #include<cstdio>
     4 #include<iostream>
     5 using namespace std;
     6 int n,W,val[110],cnt[110],dp[100010];
     7 int main(){
     8     while(~scanf("%d%d",&n,&W)&&(n|W)){
     9         for(int i=1;i<=n;++i)scanf("%d",&val[i]);
    10         for(int i=1;i<=n;++i)scanf("%d",&cnt[i]);
    11         if(W<=0){puts("0");continue;}
    12         memset(dp,-1,sizeof(dp));dp[0]=0;//注意初始化dp[0]为0
    13         for(int i=1;i<=n;++i){
    14             for(int j=0;j<=W;++j){
    15                 if(dp[j]>=0)dp[j]=cnt[i];
    16                 else if(j<val[i]||dp[j-val[i]]<=0)dp[j]=-1;//面额太大或者或者在配更小的数时数量已经用光了
    17                 else dp[j]=dp[j-val[i]]-1;
    18             }
    19         }
    20         int ans=count_if(dp+1,dp+W+1,bind2nd(greater_equal<int>(),0));//统计不小于0的个数
    21         printf("%d
    ",ans);
    22     }
    23     return 0;
    24 }

    poj 1742 #AC代码(1813ms):中规中矩,W不会出现负数。

     1 #include<algorithm>
     2 #include<string.h>
     3 #include<cstdio>
     4 #include<iostream>
     5 using namespace std;
     6 int n,W,val[110],cnt[110],dp[100010];
     7 int main(){
     8     while(~scanf("%d%d",&n,&W)&&(n|W)){
     9         memset(dp,-1,sizeof(dp));dp[0]=0;
    10         for(int i=1;i<=n;++i)scanf("%d",&val[i]);
    11         for(int i=1;i<=n;++i)scanf("%d",&cnt[i]);
    12         for(int i=1;i<=n;++i){
    13             for(int j=0;j<=W;++j){
    14                 if(dp[j]>=0)dp[j]=cnt[i];
    15                 else if(j<val[i]||dp[j-val[i]]<=0)dp[j]=-1;
    16                 else dp[j]=dp[j-val[i]]-1;
    17             }
    18         }
    19         int ans=count_if(dp+1,dp+W+1,bind2nd(greater_equal<int>(),0));
    20         printf("%d
    ",ans);
    21     }
    22     return 0;
    23 }
  • 相关阅读:
    浏览器滚动条高度的获取与设置
    aspx页面 按钮不响应回车键
    HTML5 canvas 圆盘抽奖
    spark 解决大文件造成的分区数据量过大的问题
    简单http文件服务器 (Python)
    调试分析工具 (C/C++)
    案例学习——网站高并发处理相关技术
    一致性哈希
    Linux 环境下程序不间断运行
    案例分析——BAT业务https化经历
  • 原文地址:https://www.cnblogs.com/acgoto/p/9524164.html
Copyright © 2011-2022 走看看