zoukankan      html  css  js  c++  java
  • Coins HDU

    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.

    InputThe 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.OutputFor 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

    看到英问题就头疼!!!
    这个题目看懂题目,理解题意是我觉得最难的一步!(英语贼烂)
    其实这个题目意思就是在这个[1,m]这个区间里面有多少个背包可以装满。
    (题意浓缩起来只有这么点,提炼出来真心费力)
    这个题目第二难的一部就是进行二进制优化。
    (由于本菜鸟刚学DP确实这个对我而言是一个长期理解的过程)
    二进制优化:
    任何一个数都可以由2的N次方来表示,例如4可以有1,2来表示;
    8可以有1,2,4来表示;11可以有1,2,4,8来表示。
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 #define maxn 100010
     7 int dp[maxn],a[105],b[105];
     8 int main()
     9 {
    10     int n,m;
    11     while(scanf("%d%d",&n,&m)!=EOF){
    12         if (n==0 && m==0) break;
    13         memset(dp,0,sizeof(dp));
    14         for (int i=1 ;i<=n ;i++)
    15             scanf("%d",&a[i]);
    16         for (int i=1 ;i<=n ;i++)
    17             scanf("%d",&b[i]);
    18         for (int i=1 ;i<=n ;i++){
    19             if (a[i]*b[i]>m) {
    20                 for (int k=0;k<=m ;k++){
    21                     if (k>=a[i]) dp[k]=max(dp[k],dp[k-a[i]]+a[i]);
    22                 }
    23             }else {
    24                 for (int j=1 ;j<=b[i] ;j=j*2){
    25                     for (int k=m ; k>=a[i]*j ;k--){
    26                         dp[k]=max(dp[k],dp[k-a[i]*j]+a[i]*j);
    27                     }
    28                     b[i]-=j;
    29                 }
    30                 if (b[i]>0) {
    31                      for (int k=m ;k>=a[i]*b[i] ;k--) {
    32                          dp[k]=max(dp[k],dp[k-a[i]*b[i]]+a[i]*b[i]);
    33                      }
    34                 }
    35             }
    36         }
    37         int sum=0;
    38         for (int i=1 ;i<=m ;i++){
    39             if (dp[i]==i) sum++;
    40         }
    41         printf("%d
    ",sum);
    42     }
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    apk反编译
    Eclipse Android项目中如如第三方library文件
    layout_gravity属性和gravity属性区别(转载)
    android weight 属性正解(转载)
    010_01Servlet Request&Response
    009_02sendRedirect() forward() include()
    009_01Servlet基础简介
    008_02HTTP基础知识
    008_01WEB基础知识
    【实用】让代码变的更加简洁
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8505041.html
Copyright © 2011-2022 走看看