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 ...
随机推荐
- uGUI Anchor
Anchor定位:inspector面板的Rect Transform组件中PosX左边的方框图标就是设置锚点的,做界面自适应时可定位控件在视图中的位置,与NGUI类似.Anchor+Canvas的C ...
- easyreport 安装手记
jdk tomcat eclipse maven 按下不表,网上多了去,将遇到的几个坑回顾下: 1.要安装lombok.jar 详见 https://www.cnblogs.com/lrzy/p/10 ...
- CentOS7.1 安装Liberty之环境准备(1)
一.基础平台 1.一台装有VMware的windows系统(可联网) 2.CentOS 7.1 64bit镜像 二.最小化安装两台CentOS 7.1 的虚拟机controller.compute1, ...
- QT国际化,中英文等多语言界面显示的方法
在网上学习了一下QT的国际化使用方法,最后将自己试成功的方法总结例如以下: 当中遇到的问题有:生成的ts文件里 代码中的中文 有的不显示,有的显示乱码. 步骤1: 生成.ts文件,在pro项目文件 ...
- cmake实战第二篇:让我们的代码更像个工程
为工程添加以下文件夹: bin 用来放编译好的可执行二进制文件. src 用来放源代码. lib 用来放编译好的库文件. include 用来放头文件. sudo mkdir -p /code_ ...
- server r2 系统更新文件清理
https://support.microsoft.com/zh-cn/kb/2852386
- TaoKeeper
基于zookeeper的监控管理工具taokeeper,由淘宝团队开源的zk管理中间件: 按照taokeeper官方说明 http://jm-blog.aliapp.com/?p=1450 下载tao ...
- HibernateSessionFactory演示样例
package common; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hi ...
- select循环读取数据
<select id="srType" name="srType" value="test"> <c:forEach va ...
- Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
EF6进行Insert操作的时候提示错误 Store update, insert, or delete statement affected an unexpected number of rows ...