poj 1742 Coins dp 多重背包 优化

题目

题目链接:http://poj.org/problem?id=1742

题目来源:《挑战》练习题,楼天城男人八题。

简要题意: n 种硬币,第i种有 Ci 个,每个价值 Ai [1,m] 有多少值能被取到。

数据范围: 1n100;m105;1Ai105;1Ci1000

题解

中规中矩的多重背包,有两种比较基本的做法。

第一种是使用二进制分解+完全背包。

第二种是新开数组记录来记录当前物品使用的情况。

实现

第二种方法的复杂度更低,可以使用优化手段进一步降低。

优化到1s左右之后觉得很难再优化了,看到有200ms的记录,只能颤抖了。

二进制分解代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 105;
const int M = 1e5+5;

bool dp[M];
int a[N];
int b[N];

void completePack(int c, int m) {
    for (int i = c; i <= m; i++) {
        dp[i] |= dp[i-c];
    }
}

void zeroOnePack(int c, int m) {
    for (int i = m; i >= c; i--) {
        dp[i] |= dp[i-c];
    }
}
int main()
{
    dp[0] = true;
    int n, m, v, cnt;
    while (scanf("%d%d", &n, &m) == 2 && n) {
        for (int i = 0; i < n; i++) scanf("%d", a+i);
        for (int i = 0; i < n; i++) scanf("%d", b+i);

        for (int i = 0; i < n; i++) {
            v = a[i], cnt = b[i];
            if (v*cnt >= m) {
                completePack(v, m);
            } else {
                for (int j = 1; j < cnt; cnt -= j, j <<= 1) {
                    zeroOnePack(j*v, m);
                }
                zeroOnePack(cnt*v, m);
            }
        }

        int ans = 0;
        for (int i = 1; i <= m; i++) {
            ans += dp[i];
            dp[i] = false;
        }
        printf("%d\n", ans);
    }
    return 0;
}

模剩余优化

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <bitset>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 105;
const int M = 1e5+5;

int a[N];
int b[N];
int cnt[M];
int use[M];
bool dp[M];

int main()
{
    dp[0] = true;
    int n, m;
    while (scanf("%d%d", &n, &m) == 2 && n) {
        for (int i = 0; i < n; i++) scanf("%d", a+i);
        for (int i = 0; i < n; i++) scanf("%d", b+i);

        int ans = 0, from;
        for (int i = 0; i < n; i++) {
            for (int j = a[i]; j <= m; j++) {
                if (!dp[j] && dp[from = j-a[i]] && (use[from] != a[i] || cnt[from] < b[i])) {
                    dp[j] = true;
                    cnt[j] = use[from] != a[i] ? 1 : cnt[from]+1;
                    use[j] = a[i];
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
        for (int i = 1; i <= m; i++) {
            dp[i] = use[i] = cnt[i] = 0;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值