POJ1742 Coins[多重背包可行性]

Coins

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 34814   Accepted: 11828

Description

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

Source

[email protected]

---------------------------

Orz楼爷

多重背包可行性,用O(NV)做法

滚动数组掉N那一维,否则MLE

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=105,V=1e5+5;
int n,m,f[V],c[N],v[N],ans=0;

void mpAble(){
    memset(f,-1,sizeof(f));
    f[0]=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=m;j++){
            if(f[j]>=0) f[j]=c[i];
            else f[j]=-1;
        }
        for(int j=0;j<=m-v[i];j++)
            if(f[j]>=0)
                f[j+v[i]]=max(f[j+v[i]],f[j]-1);
    }
}
int main(int argc, const char * argv[]) {
    while(cin>>n>>m){
        if(n==0&&m==0) break;
        for(int i=1;i<=n;i++) scanf("%d",&v[i]);
        for(int i=1;i<=n;i++) scanf("%d",&c[i]);
        mpAble();
        for(int i=m;i>=1;i--)if(f[i]>=0) ans++;
        cout<<ans<<"\n";ans=0;

    }
    return 0;
}
时间: 2024-10-18 16:34:44

POJ1742 Coins[多重背包可行性]的相关文章

POJ1742 Coins 多重背包+贪心

Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最优性”,这是一个特殊之处. 从这里找优化 在“最优性”的问题中,$f[j]$从$f[j]$或$f[j-a[i]]$中转移而来:而在这样的“可行性”问题中,其实只要$f[j]$可行,我们就可以不用考虑$f[j-a[i]$了,也可以反过来说. 于是我们可以考虑一种贪心策略,设$used[j]$表示$f[

POJ 1742 Coins (多重背包)

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 28448   Accepted: 9645 Description 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

POJ1276Cash Machine[多重背包可行性]

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32971   Accepted: 11950 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The m

hdu2844 &amp; poj1742 Coin ---多重背包--两种方法

题意:你有N种硬币,每种价值A[i],每种数量C[i],问.在不超过M的情况下,我们用这些硬币,付款有多少种情况.也就是:1,2,3,4,5,....,M这么多种情况下,你能用你的硬币不找钱,付款多少种情况. 例如: 你有一种硬币,价值2,个数2,那么 你是不能付款 3元的..你只能付款2,或者4元.. OK,题意差不多就是这样啦. 那么这里有两种方式! 分析: 那么这里我们可以用多重背包来解决,我们把价值和重量看成一样的w[i] = A[i]:用M作为背包.那么dp 过后,我们就可以知道 dp

POJ 1742 Coins 多重背包单调队列优化

http://poj.org/problem?id=1742 题意: 很多硬币,有价值和数量,给出一个上限,问上限内有多少种钱数可以由这些硬币组成. 分析: 好像是楼教主男人八题之一.然后学多重背包单调队列优化时看了别人的程序..所以后来写了就1A了=.= 前一篇小小总结了一下多重背包单调队列优化(http://www.cnblogs.com/james47/p/3894772.html),这里就不写了. 1 #include<cstdio> 2 #include<cstring>

POJ3260——The Fewest Coins(多重背包+完全背包)

The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very efficient man, he always pays for his goods in such a way that the smallest number of coins changes hands, i.e., the number of coins he uses to pay plus

HDU 2844 Coins 多重背包(二进制优化)

点击打开链接 Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167    Accepted Submission(s): 3327 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dolla

hdu2844 coins 多重背包

1 #include<stdio.h> 2 #include<string.h> 3 int a[102],c[102],dp[100005]; 4 int max(int a,int b) 5 { 6 return a>b?a:b; 7 } 8 void CompletePack(int v,int w,int m) //完全背包 9 { 10 for(int j=v;j<=m;j++) 11 dp[j]=max(dp[j],dp[j-v]+w); 12 } 13 v

POJ 3260 The Fewest Coins(多重背包+全然背包)

http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币,相应面值为val[1],val[2]-val[n]. 然后他身上每种货币有num[i]个. John必须付给售货员>=m的金钱, 然后售货员会用最少的货币数量找钱给John. 问你John的交易过程中, 他给售货员的货币数目+售货员找钱给他的货币数目 的和最小值是多少? 分析: 本题与POJ 1252类型: http://blog.csdn.net/u013480600