POJ 1742 多重背包(单调队列的优化)的可行性问题OR多重部分和问题

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

题意:给定n种面值的硬币面值分别为wi个数为ci,问用这些硬币可以组成1~m之间的多少面值

这一道题目和挑战上的多重部分和问题一样,那本书上也有处理多重部分和的代码及解析,先附上一位博主对多重部分和的整理

下面就是用多重部分和的思路来解决的

,用时还可以

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define init(a,b) memset(a,b,sizeof a)
const int INF=0x3f3f3f3f;
const int N=100000+50;
int dp[N];
int num[200];
int val[2000];
int main(){
    int n,m;
   // freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF,m,n)
    {
        init(num,0);init(dp,-1);init(val,0);
        rep(i,1,n)
        scanf("%d",&val[i]);
        rep(i,1,n)
        scanf("%d",&num[i]);
        dp[0]=0;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=m;j++)
        {
            if(dp[j]>=0)
                dp[j]=num[i];
            else if(j<val[i]||dp[j-val[i]]<=0)
            dp[j]=-1;
                else dp[j]=dp[j-val[i]]-1;
        }
//        rep(k,0,m)
//        printf("%d  ",dp[k]);
//        printf("\n");
        }
        int ans=0;
        rep(i,1,m) if(dp[i]>=0) ans++;
        printf("%d\n",ans);
    }
}

很明显能够看出来这是一道多重背包的题目,那肯定就是想用多重背包的板子去解决这一道题目。但是!!!会TLE,但是可以以将多重背包的板子dp数组进行优化,将int类型(32位)的改为bool类型(8位),那么就在交一下:

,差一点TLE

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
const int N= 1e5+10;
const int INF = 0x3f3f3f3f;
bool dp[N];//此时dp的下标表示i,就是对应的钱
int a[200];
int num[200];
int n;
int m;
int ans;
void ZeroOne(int y){
    for(int i=m;i>=y;i--)
        if(!dp[i]&&dp[i-y])
            dp[i]=1,ans++;
}
void Compelrt(int y){
    for(int i=y;i<=m;i++)
        if(!dp[i]&&dp[i-y])
        dp[i]=1,ans++;
}
void Multiple(int x,int y)
{
    if(x*y>=m){
        Compelrt(x);
        return ;
    }else{
        for(int i=1;i<y;){
            ZeroOne(x*i);
            y-=i;
            i<<=1;
        }
        ZeroOne(y*x);
    }
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&n,&m)!=EOF,n+m){
            memset(dp,0,sizeof dp);
            ans=0;dp[0]=1;//当钱的数目是0,那肯定是存在这一种情况的,存在情况那就赋值为1
        rep(i,1,n) scanf("%d",&a[i]);
        rep(i,1,n) scanf("%d",&num[i]);
        rep(i,1,n)
         Multiple(a[i],num[i]);
        printf("%d\n",ans);
    }
}

也可以用完全背包的思路去做,只不过需要加一个条件限制--个数限制

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define rep(i,a,n) for (int i=a;i<=n;i++)
const int N= 1e5+10;
const int INF = 0x3f3f3f3f;
int dp[N];
int sum[N];
int a[200];
int num[200];
int n;
int m;
int ans;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    while(scanf("%d%d",&n,&m)!=EOF,n+m){
            memset(dp,0,sizeof dp);
            ans=0;dp[0]=1;
        rep(i,1,n) scanf("%d",&a[i]);
        rep(i,1,n) scanf("%d",&num[i]);
        rep(i,1,n)
         {
             memset(sum,0,sizeof sum);
             for(int j=a[i];j<=m;j++)
             if(!dp[j]&&dp[j-a[i]]&&sum[j-a[i]]<num[i]){
                ans++;
                dp[j]=1;
                sum[j]=sum[j-a[i]]+1;//sum[j]表示当前物品填充j大小包,至少使用多少个;
             }
         }
        printf("%d\n",ans);
    }
}

如果想要实现多重背包的操作,那怎么办,优化,可以用多重背包的单调队列的优化

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/83661800