POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276
多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了。
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <algorithm>
- #include <math.h>
- using namespace std;
- int V,n,w[],cnt[],dp[];
- void com(int v)
- {
- for(int i=v; i<=V; i++)
- {
- dp[i]=max(dp[i],dp[i-v]+v);
- }
- return ;
- }
- void one(int v)
- {
- for(int i=V;i>=v;i--)
- {
- dp[i]=max(dp[i],dp[i-v]+v);
- }
- return ;
- }
- int main()
- {
- while(scanf("%d",&V)!=EOF)
- {
- scanf("%d",&n);
- for(int i=; i<n; i++)
- {
- scanf("%d%d",&cnt[i],&w[i]);
- }
- memset(dp,,sizeof(dp));
- for(int i=; i<n; i++)
- {
- if(cnt[i]==) continue;
- if(cnt[i]*w[i]>=V)
- {
- com(w[i]);
- }
- else
- {
- int t=;
- while(t<cnt[i])
- {
- one(w[i]*t);
- cnt[i]-=t;
- t<<=;
- }
- one(w[i]*cnt[i]);
- }
- }
- printf("%d\n",dp[V]);
- }
- return ;
- }
POJ1276:Cash Machine(多重背包)的更多相关文章
- POJ-1276 Cash Machine 多重背包 二进制优化
题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- POJ1276:Cash Machine(多重背包)
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ 1276:Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30006 Accepted: 10811 De ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- PKU--1267 Cash Machine(多重背包)
题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
随机推荐
- Webdings 图形字体
如果想在网页上插入图形,最简单的方式就是使用图形字体.Webdings 是一种微软开发的图形字体,在IE浏览器上可以使用它. 什么是Webdings Webdings 是一个TrueType的ding ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- 配置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 ...
- 使用JSP实现用户登录
本文讲述使用JSP实现用户登录,包括用户登录.注册和退出功能等. 1.系统用例图 2.页面流程图 3.数据库设计 本例使用oracle数据库 创建用户表 包括id,username,password和 ...
- Matlab之显示输出
0.recommand: fprintf fprintf('%d\n', i); 1.disp disp(['answer = ' num2str(5)]); 2.sprintf sprintf(' ...
- What can be use as an encoder
原于2018年5月在实验室组会上做的分享,今天分享给大家,希望对大家的科研有所帮助.
- 【Raspberry Pi】定时运行python程序读温湿度传感器数据&发邮件
1.定时执行脚本 http://tech.it168.com/a2011/0707/1214/000001214830_all.shtml /sbin/service crond start //启动 ...
- 最基础的PHP分类查询程序
最初级的PHP分类查询程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- Duilib教程-自动布局3-分隔条
先看一个常用的图,如下: 左边是导航栏,右边是信息区. 中间可以自由拉伸. XML如下: <?xml version="1.0" encoding="utf-8&q ...
- CentOS 安装 dotnetcore
参考官方教程:https://www.microsoft.com/net/core#linuxcentos 安装.NET CORE SDK sudo yum install libunwind lib ...