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对应着 ...
随机推荐
- jmeter服务器监控插件下载配置
jmeter想要监控服务器CPU.内存.磁盘.网络等相关资源需要借助plugins插件 下载jmeter性能监控插件 1.下载jmeterPlugins-Standard和jmeterPlugins- ...
- Cookie、session和localStorage的区别
一.Cookie.session和localStorage的区别 cookie的内容主要包括:名字.值.过期时间.路径和域.路径与域一起构成cookie的作用范围.若不设置时间,则表示这个cookie ...
- Kotlin Eclipse 环境搭建
Kotlin是JetBrains开发的基于JVM的语言.JetBrains是一家捷克的软件开发公司,该公司位于捷克的布拉格,研发了IntelliJ IDEA这款相对于Eclipse有较大改善的大名鼎鼎 ...
- LCS and LIS
LCS #include<bits/stdc++.h> using namespace std; typedef long long ll; int n,m; char s[1005],t ...
- OpenERP how to set the tree view limit
return { 'name':u'库存报表', 'view_type':'form', 'view_mode':'tree,form', 'res_model':'rainsoft.account. ...
- lrzsz
新搞的云服务器用SecureCRT不支持上传和下载,没有找到rz命令.记录一下如何安装rz/sz命令的方法. 一.工具说明 在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz ...
- java反射机制_读取properties
代码: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 反射特点: ...
- 让字体在div容器中垂直居中
1. 只显示一行数据的话: 给容器设置height和line-height,并使两个值相等,再加上over-flow:hidden .test{ height:40px; line-height:40 ...
- C#中的方法(函数),委托和事件
1. 先有方法,然后再有委托,最后再有事件 2. 思考能不能将 A方法 作为参数,传递给 B方法呢? eg: 在传统的编程方式中,A方法 调用 B方法的方式如下: //洗菜 public void W ...
- Oracle 12c JDBC 连接
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...