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. react-native-scrollable-tab-view 实现 TabBar

    1.创建组件 src/components/CustomTabBar/index.js /** * 自定义选项卡 */ import React, {Component} from 'react'; ...

  2. (一)Oracle学习笔记—— 表和表空间

    1. 表空间 一个数据库可以有多个表空间,一个表空间里可以有多个表.表空间就是存多个表的物理空间:可以指定表空间的大小位置等.  1.1 创建表空间语句 create tablespace ts3 d ...

  3. es迁移索引数据合并

    es集群迁移,大规模迁移过程中,比如我们以当天时间做索引,在新的es集群会存在和老的es集群一样的索引文件名,这个时候用snapshot恢复数据会出现冲突问题.这里我们可以用reindex api来解 ...

  4. 从零開始学Java之线程具体解释(1):原理、创建

    Java线程:概念与原理 一.操作系统中线程和进程的概念 如今的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中执行的应用程序.每一个进程都有自己独立的一块内存空间.一个进 ...

  5. Linux命令常用命令

    查看主机IP ifconfig 切换目录 cd cd /home cd /path cd ../path cd 退到home目录 cd .. 退到上层目录 cd / 退到根目录  ls -l 列出数据 ...

  6. Python内置函数之ascii()

    ascii()返回一个字符串对象. ascii()的参数只能有一个. 如果参数中有非ascii字符,会用 \u,\U,\x 来替代. ascii()和Python2中repr()等效 下面看看例子: ...

  7. Painting Fence

    Painting Fence Time Limit:1000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u Sub ...

  8. 封装ShareSDK中的分享功能封以及对类似第三方功能封装的心得【原创】

    本篇的主题有三个: 1.封装思想的介绍 2.我的封装代码 3.我在封装sharesdk(采用的是简洁版本)分享功能是碰到的问题,以及解决方法. PS:其实这个我之前封装过一次,不过最近在重构项目时发现 ...

  9. Qt5 CMake cross compile

    cmake_minimum_required(VERSION 2.8) if (${ARM}) set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCES ...

  10. Android项目使用Dagger2进行依赖注入

    原文链接:http://code.tutsplus.com/tutorials/dependency-injection-with-dagger-2-on-android–cms-23345 依赖注入 ...