poj 3628 Bookshelf 2 基本01背包
题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少。
题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp[]即所求答案。
#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 2000005 using namespace std; int dp[MAX],a[MAX],sum,ans; void Init()
{
sum=;
ans=INF;
memset(dp,,sizeof(dp));
}
int main()
{
int i,j,maxn,n,h; while(scanf("%d%d",&n,&h)!=EOF)
{
Init(); for(i=;i<=n;i++)
{scanf("%d",&a[i]);sum+=a[i];} for(i=;i<=n;i++)
{
for(j=sum;j>=a[i];j--)
{
dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
}
} for(i=;i<=sum;i++)
{
if(dp[i] >= h)//如果满足条件,则继续进行比较,保留较小的值
{
ans=min(ans,dp[i]-h);
}
} printf("%d\n",ans);
}
return ;
}
poj 3628 Bookshelf 2 基本01背包的更多相关文章
- POJ 3628 Bookshelf 2【01背包】
题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== #include<iostream& ...
- POJ 3628 Bookshelf 2(01背包)
Bookshelf 2 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9488 Accepted: 4311 Descr ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- POJ 3628 Bookshelf 2 0-1背包
传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...
- poj 3628 Bookshelf 2
http://poj.org/problem?id=3628 01背包 #include <cstdio> #include <iostream> #include <c ...
- PKU--3628 Bookshelf 2(01背包)
题目http://poj.org/problem?id=3628 分析:给定一堆牛的高度,把牛叠加起来的高度超过牛棚的高度. 且是牛叠加的高度与牛棚高度之差最小. 把牛叠加的高度看作是背包的容量,利用 ...
- POJ3628:Bookshelf 2【01背包】
Description Farmer John recently bought another bookshelf for the cow library, but the shelf is gett ...
- POJ 3624 Charm Bracelet (01背包)
题目链接:http://poj.org/problem?id=3624 Bessie has gone to the mall's jewelry store and spies a charm br ...
随机推荐
- 第九节,基本条件语句if
条件语句 如果我们希望有效的响应用户的输入,代码就需要具有判断能力.能够让程序进行判断的结构成为条件,条件判断语句返回的是布尔值真或假,真就执行一条线路,假就执行另外一条线路 注意if判断如果怎样,否 ...
- mysql 数据表
CREATE DATABASE IF NOT EXISTS `shop`;USE `shop`; drop table if exists lidepeng; create table lidepe ...
- 缓存1 静态缓存-->读库保存成php文件 mkdir-->file_put_contents-->var_export -->include
@mkdir()-->file_put_contents-->$data = "<?php\nreturn ".var_export($setting, tru ...
- openwrt下加载snmp模块
加snmp模块到openwrt中去 1.下载snmp的解压包文件 net-snmp-5.4.2.1.tar.gz 下载地址为:http://www.net-snmp.org/download.html ...
- HDU1073:Online Judge
Problem Description Ignatius is building an Online Judge, now he has worked out all the problems exc ...
- Self Numbers
Self Numbers Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- NoSQL数据库种类
NoSQL数据库的四大分类 键值(Key-Value)存储数据库 这一类数据库主要会使用到一个哈希表,这个表中有一个特定的键和一个指针指向特定的数据.Key/value模型对于IT系统来说的优 ...
- 超赞!聊聊WEB APP、HYBRID APP与NATIVE APP的设计差异
编者按:这3类主流应用你都了解吗?设计师除了要有视觉功夫,对不同形式的APP也应当了然于胸,今天百度的同学写了一篇非常全面的总结,帮你迅速搞定3类主流APP的设计方法,附带一大波避雷针,带你巧妙跳过A ...
- ios 做的一个三个数求平均数 最大数 最小数
#import "ViewController.h" @interface ViewController ()@property (weak, nonatomic) IBOutle ...
- viewpager处理(一):让viewpager不能滑动
1.实现原理: 自定义viewpager,重写onTouchEvent方法,什么触摸事件都不响应即可让viewpager不能滑动. 2.代码如下 public class NoScrollViewPa ...