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对应着 ...
随机推荐
- 《快学Scala》第六章 对象 第七章 包和引入
- 为服务器设置固定IP地址
为服务器设置固定IP地址 1.获取超级管理员权限 命令:$ su - 输入root密码 2.判断哪个网卡有流量,或者确定需要设置哪个网卡的固定ip 命令:# ifconfig PS:可以查询哪些网卡有 ...
- 如何在Cordova Android 7.0.0 以下版本集成最新插件 极光插件为例
前提 Cordova Android 7.0.0开始改变了项目安卓平台的架构.新建一个空项目分别添加Android 6.4.0 和 Android 7.0.0平台: cordova platform ...
- iOS -- UILabel的常见使用
UILabel是iOS开发经常用到的一个控件,主要用于显示文字.下面记录一些常用的UIlabel的使用. 先定义:UILabel *label = [[UILabel alloc]initWithFr ...
- GCD介绍:Dispatch_source
[转自:GCD介绍(三): Dispatch Sources] 何为Dispatch Sources 简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一 ...
- 【Quartz】Spring Boot使用properties文件配置Quartz
(1)在resource目录下新建quartz.properties文件 #============================================================== ...
- 【Quartz】一个小Demo
Quartz是一个优秀的框架,功能强大,用法简单,比如说下面这个例子. (1)引入Jar包 <!-- quartz 模块 --> <dependency> <groupI ...
- jenkins发送测试报告邮件
1.安装插件 Email Extension Plugin 2.设置Extended E-mail Notification a."系统管理"--“系统设置”.配置Extende ...
- SpringBoot进阶用法-随笔
SpringBoot进阶用法 实现setApplicationContext //实现ApplicationContextAware接口,重写setApplicationContext方法 publi ...
- java容器类1:Collection,List,ArrayList,LinkedList深入解读
1. Iterable 与 Iterator Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素. public interface Iterable<T> 修饰符 ...