zoukankan      html  css  js  c++  java
  • POJ 1742 Coins(dp多重背包)

    题意:给定n种硬币的价值和数量,问能组成1~m中多少种面值。

    分析:

    1、dp[j]表示当前用了前i种硬币的情况下,可以组成面值j。

    2、eg:

    3 10

    1 3 4 2 3 1

    (1)使用第1种硬币,可以组成的面值0 1 2,eg:当前cnt[2]表示组成面值2使用了两(cnt[2])个第一种硬币。

    (2)在使用第一种硬币基础上,使用第二种硬币,可组成0 1 2 3 6 9,eg:当前cnt[6]表示组成面值6使用了两(cnt[6])个第二种硬币,依此类推。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-10;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100 + 10;
    const int MAXT = 100000 + 10;
    using namespace std;
    int value[MAXN];
    int num[MAXN];
    int dp[MAXT];
    int cnt[MAXT];
    int main(){
        int n, m;
        while(scanf("%d%d", &n, &m) == 2){
            if(!n && !m) return 0;
            memset(dp, 0, sizeof dp);
            for(int i = 0; i < n; ++i){
                scanf("%d", &value[i]);
            }
            for(int i = 0; i < n; ++i){
                scanf("%d", &num[i]);
            }
            dp[0] = 1;
            int ans = 0;
            for(int i = 0; i < n; ++i){
                memset(cnt, 0, sizeof cnt);
                for(int j = value[i]; j <= m; ++j){
                    if(!dp[j] && dp[j - value[i]] && cnt[j - value[i]] < num[i]){
                        dp[j] = 1;
                        cnt[j] = cnt[j - value[i]] + 1;
                        ++ans;
                    }
                }
            }
            printf("%d\n", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Use MVS Dsbame convensions. windows下ftp.exe客户端上传错误
    Sqlserver 2005:数据库快照
    Oracle:使用ASM自动存储管理, 严重推荐
    Thunderbird 邮件客户端:windows 和 ubuntu 或 liunx 下共用的方法
    Oracle:Oracle 10 RAC 安装群集件的准备工作
    SSH
    STL
    ASP生成静态Html文件技术杂谈
    Nessus:网络和主机漏洞评估程序安装试用
    table 的 id 属性不被 document.getElementById支持
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6525647.html
Copyright © 2011-2022 走看看