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

  1. 3 10
  2. 1 2 4 2 1 1
  3. 2 5
  4. 1 4 2 1
  5. 0 0

Sample Output

  1. 8
  2. 4

Source

 

poj1742 动态规划 经典多重背包

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

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

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

  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. int n,m,ans,w[],v[],d[],s[];
  5. int main()
  6. {
  7. while(scanf("%d%d",&n,&m)==&&n&&m){
  8. int i,j;ans=;
  9. for(i=;i<n;i++) scanf("%d",w+i);
  10. for(i=;i<n;i++) scanf("%d",v+i);
  11. memset(d,,sizeof(d));
  12. for(d[i=]=;i<n;i++){
  13. for(j=;j<=m;j++) s[j]=;
  14. for(j=w[i];j<=m;j++)
  15. if(!d[j]&&d[j-w[i]]&&s[j-w[i]]<v[i])
  16. d[j]=,s[j]=s[j-w[i]]+;
  17. }
  18. for(i=;i<=m;i++)
  19. if(d[i])
  20. ans++;
  21. printf("%d\n",ans);
  22. }
  23. return ;
  24. }

  1. #include<cstdio>
  2. int n,m,res;
  3. int use[],c[],f[];
  4. int main()
  5. {
  6. f[]=;
  7. while(scanf("%d%d",&n,&m)==&&n&&m){
  8. res=;
  9. for(int i=;i<=m;i++) f[i]=;
  10. for(int i=;i<n;i++) scanf("%d",&c[i]);
  11. for(int i=,s;i<n;i++){
  12. for(int j=;j<=m;j++) use[j]=;
  13. scanf("%d",&s);
  14. for(int j=c[i];j<=m;j++){
  15. if(!f[j]&&f[j-c[i]]&&use[j-c[i]]<s){//贪心过程
  16. use[j]=use[j-c[i]]+;
  17. f[j]=;
  18. res++;
  19. }
  20. }
  21. }
  22. printf("%d\n",res);
  23. }
  24. return ;
  25. }

对比上下两张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. Rails generate的时候不生成assets和test

    我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...

  2. 语言那点事,crt

    C语言标准(不管是ANSI 还是ISO)包含2部分,一部分是语言本身的标准,另一部分是C标准函数库.C标准函数库规定了函数的原型和功能,但是并没限定这些函数要怎么实现.所谓满足标准C规定的C编译器,不 ...

  3. Python爬取豆瓣《复仇者联盟3》评论并生成乖萌的格鲁特

    代码地址如下:http://www.demodashi.com/demo/13257.html 1. 需求说明 本项目基于Python爬虫,爬取豆瓣电影上关于复仇者联盟3的所有影评,并保存至本地文件. ...

  4. oracle备份还原数据库

    首先到对应bin目录下打开cmd(windows),linux用相应终端链接:eg:D:\app\sun\product\11.2.0\dbhome_1\BIN 用户备份:exp u_tdms/p_t ...

  5. 用Jmeter测试RabbitMQ

    1.下载AMQP插件 github上面有源码,可以通过ant+ivy在本地进行打包(下载IDEA实践成功) https://github.com/jlavallee/JMeter-Rabbit-AMQ ...

  6. LuaStudio编辑调试软件

    该编辑调试器最大特点就是能够注入到宿主程序内对lua脚本进行调试.还能够设置断点观察变量的值,功能很强大. 如今已有的编辑器有Notepad++, Editplus, luaforwindows, S ...

  7. 深入研究mysql中group by与order by取分类最新时间内容

    鉴于项目的需要,就从网上找到该文章,文章分析得很详细也很易懂,在android里,(不知道是不是现在水平的限制,总之我还没找到在用 ContentProvider时可以使用子查询),主要方法是用SQL ...

  8. Mac 上的传奇效率神器 Alfred 3

    下载地址:https://www.alfredapp.com/ 第三方教程:https://www.jianshu.com/p/e9f3352c785f 一.内置快捷键介绍 1.默认快捷呼出热键是: ...

  9. mysql 函数substring_index() 截取字符串

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

  10. php的pear包管理

    1.安装:  $ sudo wget http://pear.php.net/go-pear.phar  $ sudo php go-pear.har 2.查看pear下安装的包:  $ pear l ...