HDU 2546(01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546
http://blog.csdn.net/xujinsmile/article/details/7969412
首先拿出5元买最贵的东西,那接下来就是背包容量m-5,物品数量n-1 的01背包问题了。
做背包问题一定要明确边界条件再入手,不然很费时间
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std; #define MEM(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define debug printf("!\n")
#define INF 8000
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ep 1e-6 int dp[INF]; int ci[INF];//容量
int wi[INF];//价值
int n,V,i,j,v,t,sum; void zeroOnePack(int cost,int weight)
{
for(v = V-;v>=cost;v--)
{
dp[v] =MAX(dp[v],dp[v-cost]+weight);
}
} void completePack(int cost,int weight)
{
for(v = cost;v<=V;v++)
{
dp[v] =MAX(dp[v],dp[v-cost]+weight);
pf("tt%d %d %d\n",i,v,dp[v]);
} } int main()
{ int t;
while(sf("%d",&n) && n)
{ MEM(dp,);
MEM(ci,);
MEM(wi,); for(i = ;i<=n;i++)
{
sf("%d",&wi[i]);
} sort(wi+,wi+n+); sf("%d",&V); int m = wi[n]; if(V<)
{
pf("%d\n",V);
continue;
}
else
{
n--;
for(i = ;i<=n;i++)
{
zeroOnePack(wi[i],wi[i]);
} pf("%d\n",V-m-dp[V-]); }
}
return ;
}
HDU 2546(01背包)的更多相关文章
- HDU 2546 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2546 经典的01背包 预留5元买最贵的,剩余的就是01背包. #include<stdio.h> # ...
- HDU 2546(01背包)
饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- hdu 2546 0-1背包
#include<stdio.h> #include<string.h> #define N 1100 int dp[N],a[N]; int main() { int n,m ...
- hdu 1203 01背包 I need a offer
hdu 1203 01背包 I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...
- hdu 1864 01背包 最大报销额
http://acm.hdu.edu.cn/showproblem.php?pid=1864 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- hdu 2955 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...
- [HDOJ1171]Big Event in HDU(01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1171 许多有价值的物品,有重复.问如何将他们分成两堆,使两堆价值之差最小. 对价值求和,转换成01背包 ...
- HDU 2639 01背包(分解)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...
- HDU 1171 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=1171 基础的01背包,求出总值sum,背包体积即为sum/2 #include<stdio.h> # ...
- hdoj1171 Big Event in HDU(01背包 || 多重背包)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意 老师有一个属性:价值(value).在学院里的老师共有n种价值,每一种价值value对应着 ...
随机推荐
- [flex] as3.0 实现基于air的简单浏览器
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx= ...
- leetcode-840-Magic Squares In Grid
题目描述: A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each r ...
- Keil软件编程问题归总
Q:KEIL.uVision.RealView.MDK.KEIL C51之间的关系纠葛(比较区别)? A: KEIL是公司的名称,有时候也指KEIL公司的所有软件开发工具,目前2005年Keil由AR ...
- C#-WebForm JS定时器
JS定时器: 1.window.setTimeout(function(){},3000) 延迟3秒执行 2.window.setInterval(function(){},3000) 也叫重复器,每 ...
- 技巧方法 - CentOS6将Python2.6.6升级到Python2.7.6
1.首先使用“python -V”命令查看python版本,我们测试主机显示的是2.6.6版,于是下面就着手将python2.6.6升级到Python2.7.6.python -V #查看python ...
- Jupyter Notebook & Spyder 常用快捷键
Jupyter Notebook: 1. "shift + enter" notebook单元格内执行代码,并且自动跳至下一个的单元格 2. "tab" 自动补 ...
- Normalize.css – HTML5-ready 的css重置样式集
Normalize.css 是一个可定制的 css文件,使浏览器呈现的所有元素,更一致和符合现代标准.它正是针对只需要统一的元素样式.该项目依赖于研究浏览器默认元素风格之间的差异,精确定位需要重置的样 ...
- guava学习:guava集合类型-table
最近学习了下guava的使用,这里简单记录下一些常用并且使用的工具类把. 看到table的使用时候真的是眼前一亮,之前的代码中写过很多的Map<String,Map<String,Stri ...
- JavaScript 缓动效果
Math.easeout = function (A, B, rate, callback) { if (A == B || typeof A != 'number') { return; } B = ...
- JavaScript设计模式-8.链式调用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...