题目:http://poj.org/problem?id=1276

多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了。

  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <algorithm>
  5. #include <math.h>
  6. using namespace std;
  7. int V,n,w[],cnt[],dp[];
  8. void com(int v)
  9. {
  10. for(int i=v; i<=V; i++)
  11. {
  12. dp[i]=max(dp[i],dp[i-v]+v);
  13. }
  14. return ;
  15. }
  16. void one(int v)
  17. {
  18. for(int i=V;i>=v;i--)
  19. {
  20. dp[i]=max(dp[i],dp[i-v]+v);
  21. }
  22. return ;
  23. }
  24. int main()
  25. {
  26. while(scanf("%d",&V)!=EOF)
  27. {
  28. scanf("%d",&n);
  29. for(int i=; i<n; i++)
  30. {
  31. scanf("%d%d",&cnt[i],&w[i]);
  32. }
  33. memset(dp,,sizeof(dp));
  34. for(int i=; i<n; i++)
  35. {
  36. if(cnt[i]==) continue;
  37. if(cnt[i]*w[i]>=V)
  38. {
  39. com(w[i]);
  40. }
  41. else
  42. {
  43. int t=;
  44. while(t<cnt[i])
  45. {
  46. one(w[i]*t);
  47. cnt[i]-=t;
  48. t<<=;
  49. }
  50. one(w[i]*cnt[i]);
  51. }
  52. }
  53. printf("%d\n",dp[V]);
  54. }
  55. return ;
  56. }

POJ1276:Cash Machine(多重背包)的更多相关文章

  1. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

  2. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  3. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  6. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. PKU--1267 Cash Machine(多重背包)

    题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...

  9. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

随机推荐

  1. Webdings 图形字体

    如果想在网页上插入图形,最简单的方式就是使用图形字体.Webdings 是一种微软开发的图形字体,在IE浏览器上可以使用它. 什么是Webdings Webdings 是一个TrueType的ding ...

  2. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  3. 配置Docker中国区官方镜像http://get.daocloud.io/ 很好的一个源http://get.daocloud.io/#install-docker

    https://www.daocloud.io/mirror#accelerator-doc 配置Docker中国区官方镜像http://get.daocloud.io/ 很好的一个源http://g ...

  4. 使用JSP实现用户登录

    本文讲述使用JSP实现用户登录,包括用户登录.注册和退出功能等. 1.系统用例图 2.页面流程图 3.数据库设计 本例使用oracle数据库 创建用户表 包括id,username,password和 ...

  5. Matlab之显示输出

    0.recommand: fprintf fprintf('%d\n', i); 1.disp disp(['answer = '  num2str(5)]); 2.sprintf sprintf(' ...

  6. What can be use as an encoder

    原于2018年5月在实验室组会上做的分享,今天分享给大家,希望对大家的科研有所帮助.

  7. 【Raspberry Pi】定时运行python程序读温湿度传感器数据&发邮件

    1.定时执行脚本 http://tech.it168.com/a2011/0707/1214/000001214830_all.shtml /sbin/service crond start //启动 ...

  8. 最基础的PHP分类查询程序

    最初级的PHP分类查询程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  9. Duilib教程-自动布局3-分隔条

    先看一个常用的图,如下: 左边是导航栏,右边是信息区. 中间可以自由拉伸. XML如下: <?xml version="1.0" encoding="utf-8&q ...

  10. CentOS 安装 dotnetcore

    参考官方教程:https://www.microsoft.com/net/core#linuxcentos 安装.NET CORE SDK sudo yum install libunwind lib ...