Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 33998   Accepted: 11554

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

 

poj1742 动态规划 经典多重背包

有n种不同面值的硬币,面值各为A1,A2,A3。。AN,数量各为C1,C2,C3,,,,,CN。给定数m,问这些硬币能组成小于等于m的数中的哪些数,输出这些数的数目。

其实这个vn算法的编码很简单,关键是如何证明这种贪心是正确的,这个比较复杂,,我还在理解中!!!!!!!!

---用单调队列优化的DP已超出了NOIP的范围,

#include<cstdio>
#include<cstring>
using namespace std;
int n,m,ans,w[],v[],d[],s[];
int main()
{
while(scanf("%d%d",&n,&m)==&&n&&m){
int i,j;ans=;
for(i=;i<n;i++) scanf("%d",w+i);
for(i=;i<n;i++) scanf("%d",v+i);
memset(d,,sizeof(d));
for(d[i=]=;i<n;i++){
for(j=;j<=m;j++) s[j]=;
for(j=w[i];j<=m;j++)
if(!d[j]&&d[j-w[i]]&&s[j-w[i]]<v[i])
d[j]=,s[j]=s[j-w[i]]+;
}
for(i=;i<=m;i++)
if(d[i])
ans++;
printf("%d\n",ans);
}
return ;
}

#include<cstdio>
int n,m,res;
int use[],c[],f[];
int main()
{
f[]=;
while(scanf("%d%d",&n,&m)==&&n&&m){
res=;
for(int i=;i<=m;i++) f[i]=;
for(int i=;i<n;i++) scanf("%d",&c[i]);
for(int i=,s;i<n;i++){
for(int j=;j<=m;j++) use[j]=;
scanf("%d",&s);
for(int j=c[i];j<=m;j++){
if(!f[j]&&f[j-c[i]]&&use[j-c[i]]<s){//贪心过程
use[j]=use[j-c[i]]+;
f[j]=;
res++;
}
}
}
printf("%d\n",res);
}
return ;
}

对比上下两张AC图

太想节省空间,空间没节省了,时间还更长了。

读入完成再处理比边读边处理快!!!

poj1742的更多相关文章

  1. 【poj1742】 Coins

    http://poj.org/problem?id=1742 (题目链接) 题意 给出n钟纸币,每种纸币面值为a[i],数量为c[i],问能够成多少数值小于等于m的数. Solution 先想到了容斥 ...

  2. POJ1742 Coins(男人八题之一)

    前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...

  3. 《挑战程序设计竞赛》2.3 动态规划-优化递推 POJ1742 3046 3181

    POJ1742 http://poj.org/problem?id=1742 题意 有n种面额的硬币,面额个数分别为Ai.Ci,求最多能搭配出几种不超过m的金额? 思路 据说这是传说中的男人8题呢,对 ...

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

    原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...

  5. POJ1742 Coins[多重背包可行性]

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

  6. [poj1742]coin

    题意:多重背包的w=v特殊情况 分析:此题如果用单调队列O(nv)会被无耻卡常数…… 然后鉴于此题W=V,所以只存在背包恰好为i的是否存在,即bool型的f[i]是否为1 即往背包染色上面考虑 如果是 ...

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

    /* 这题卡常数.... 二进制优化或者单调队列会被卡 必须+上个特判才能过QAQ 单调队列维护之前的钱数有几个能拼出来的 循环的时候以钱数为步长 如果队列超过c[i]就说明队头的不能再用了 拿出来 ...

  8. POJ1742:Coins(多重背包)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  9. poj1742 多重背包的可行性问题

    http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...

随机推荐

  1. IOS Appstore价格表

  2. PJOI PKU Campus 2011 B:A Problem about Tree LCA 求随意点x为根的y的父节点

    题目链接:点击打开链接 题意:给定n个点 m个询问 以下n-1行给定一棵树 m个询问 x y 问把树转成以x为根 y的父节点是谁 第一种情况lca==y那就是x的第 dep[x] - dep[y] - ...

  3. [C++设计模式] command 命令模式

    在软件系统中,"行为请求者"与"行为实现者"通常呈现一种"紧耦合". 但在某些场合,比方要对行为进行"记录.撤销/重做.事务&qu ...

  4. Linux下默认的目录介绍

    目录/文件 用途 来源 / /处于Linux文件系统树形结构的最顶端,它是Linux文件系统的入口,所有的目录.文件.设备都在/之下. - /bin 该目录存放着系统最常用的最重要的命令,相当于DOS ...

  5. asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(二)—— easyui的简单实用

    下面开始在UserManager.Web中利用easyUI构建web. 1. 先删除自带的controllers.models和views(里面的shared和web.config可以保存)下面的文件 ...

  6. 在php中如何用 union all统计总条数?

    网上使用union all 查询记录总条数的参考资料比较少,所以记录下来,以便有同样需求的人使用. $rs_num = Db::query("select sum(a.b) as num f ...

  7. Fiddler4插件开发实践

    Fiddler4 是一款 巴拉巴拉..... 连接在这:http://www.telerik.com/fiddler 开发文档在这:http://docs.telerik.com/fiddler/Ex ...

  8. RabbitMQ实战:理解消息通信

    RabbitMQ是一个开源的消息代理和队列服务器,可以通过基本协议在完全不同的应用之间共享数据,可以将作业排队以便让分布式服务进行处理. 本篇介绍下消息通信,首先介绍基础概念,将这些概念映射到AMQP ...

  9. Hive 练习 简单任务处理

    1.2018年4月份的用户数.订单量.销量.GMV (不局限与这些统计量,你也可以自己想一些) -- -- -- 2018年4月份的用户数量 select count(a.user_id) as us ...

  10. [原创]如何让freeswitch转发客户端自定义的INFO消息

    如何让freeswitch转发客户端自定义的INFO消息 英文概述: this article is about how to configure freeswitch to forward self ...