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] 组件Tree系列 —— 作为PopUpButton的弹出菜单
mxml: <?xml version="1.0" encoding="utf-8"?> <!--功能描述:Tree作为PopUpButton ...
- CentOS 7 Apache服务的安装与配置(转)
https://blog.51cto.com/13525470/2070375 一.Apache简介 Apache 是一个知名的开源Web服务器.早期的Apache服务器由Apache Group来维 ...
- [Virtualization] VMware虚拟机三种网络模式详解(转)
原文:http://www.linuxidc.com/Linux/2016-09/135521.htm
- clang命令理解程序
Xcode 创建一个mac OS command Line Tool程序 步骤打开终端 cd + 工程路径(绝对路径)(注:拖拽main.m文件到终端) input —preprocessor— ...
- vue二级路由跳转后外部引入js失效问题解决方案
vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...
- TX2 i2c-tools使用
安装: apt-get install libi2c-dev i2c-tools 检测i2c总线数目 用i2cdetect检测有几组i2c总线在系统上: i2cdetect -l 可以看到系统中有9组 ...
- dotnet publish
发布Release版本:dotnet publish --configuration Release 发布Debug版本:dotnet publish --configuration Debug
- LOJ3069. 「2019 集训队互测 Day 1」整点计数(min_25筛)
题目链接 https://loj.ac/problem/3069 题解 复数真神奇. 一句话题意:令 \(f(x)\) 表示以原点 \((0, 0)\) 为圆心,半径为 \(x\) 的圆上的整点数量, ...
- 问题 I: 闪闪发光
[提交] [状态] [命题人:外部导入] 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计——成为偶像, 只要她 ...
- python怎么解决用matplotlib画图时无法显示中文的问题或者出现方框的问题
在中文前面加上u 加上u以后如果还不可以显示中文显示了方框 就直接加上 from pylab import mpl mpl.rcParams['font.sans-serif']=['SimHei']