背包问题 POJ1742 Coins

Coins

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 37762   Accepted: 12830

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

水题涨自信……

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,ans;
 7 int a[110],c[110],used[100010];
 8 bool f[100010];
 9 int main(){
10     while(scanf("%d%d",&n,&m)&&n&&m){
11         memset(f,0,sizeof(f));
12         ans=0;
13         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
14         for(int i=1;i<=n;i++) scanf("%d",&c[i]);
15         f[0]=1;
16         for(int i=1;i<=n;i++){
17             memset(used,0,sizeof(used));
18             for(int j=a[i];j<=m;j++){
19                 if(!f[j]&&f[j-a[i]]&&used[j-a[i]]<c[i]){
20                     f[j]=1;
21                     used[j]=used[j-a[i]]+1;
22                     ans++;
23                 }
24             }
25         }
26         printf("%d\n",ans);
27     }
28     return 0;
29 }
时间: 2024-08-07 21:19:09

背包问题 POJ1742 Coins的相关文章

POJ1742 Coins(男人八题之一)

前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示用前i种硬币能否取到金额j,ture表示可以,false表示不行. 则有 DP[i][j] = DP[i - 1][j] | DP[i - 1][j - k * Ai], 0 ≤ k ≤ Ci, j - k * Ai ≥ 0 这是一个O(N3)的算法,考虑到数据范围1 ≤ N ≤ 100, M ≤

poj1742 Coins【多重背包】【贪心】

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

POJ-1742 Coins

Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 37959 Accepted: 12882 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 coi

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

POJ1742 coins 动态规划之多重部分和问题

原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表.他希望买的时候不用找零,问有多少种价格能满足这一点. 这个问题实际上是一个多重部分和的问题:假设有n种物品,每种物品的价值为v[i],数量为c[i],随意选取这些物品,能否使它们的价值之和恰好为m.使用动态规划的思想来求解这类问题: 定义dp数组,dp[i][j]的值代表前i种物品随意选取,价值之

poj1742 Coins

思路: 多重背包变形. 楼天城男人八题的第六题. http://www.cnblogs.com/dramstadt/p/3439725.html 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 bool ok[100005]; 6 int used[100005]; 7 int v[105]; 8 int c[105]; 9 int n

poj1742 Coins(多重背包+单调队列优化)

/* 这题卡常数.... 二进制优化或者单调队列会被卡 必须+上个特判才能过QAQ 单调队列维护之前的钱数有几个能拼出来的 循环的时候以钱数为步长 如果队列超过c[i]就说明队头的不能再用了 拿出来 时刻维护sum表示之前的+v[i]能凑出j来的有几种 注意先进队在更新f */ #include<iostream> #include<cstdio> #include<cstring> #define maxm 100010 #define maxn 110 using

POJ1742 Coins 多重背包+贪心

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

322. Coin Change

ref: https://leetcode.com/problems/coin-change/ 就是完全背包问题,可以再复习一遍背包问题. 01背包: for 每一个item for amount...cost[item] f[v] = Max{f[v], f[v-cost[item]] + weight[item]} 因为01背包每个item只能用一次,所以amount要从后往前算,因为比较的时候用的是f[v]和f[v-cost[item]],第二个那里是减号,所以退着算,后面的结果前面没有使