dp之多重背包poj2392
题意:有k种石头,高为hi,在不超过ai的高度下,这种石头可以放置,有ci种这个石头,求这些石头所能放置的最高高度.........
思路:以往的什么硬币种数,最大硬币数之类的,他们的硬币都已经是排好序了的,总是从小到大,但是这个题目不同,它有着最高高度的限制,那么在思考的时候,要得到最优的,那么首先就是要对ai排序......这是贪心,然后就是多重背包了........
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
int h;
int a;
int c;
}s[500];
int dp[50000],num[41000];
int cmp(const node p,const node q)
{
return p.a<q.a;
}
int main()
{
int n;
while(scanf("%d",&n)>0)
{
int maxx=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&s[i].h,&s[i].a,&s[i].c);
if(maxx<s[i].a)
maxx=s[i].a;
}
sort(s+1,s+1+n,cmp);
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=1;i<=n;i++)
{
memset(num,0,sizeof(num));
for(int j=s[i].h;j<=maxx;j++)
if(dp[j-s[i].h]&&dp[j-s[i].h]+s[i].h>dp[j]&&dp[j-s[i].h]+s[i].h-1<=s[i].a&&num[j-s[i].h]<s[i].c)
{
dp[j]=dp[j-s[i].h]+s[i].h;
//printf("%d %d\n",dp[j],s[i].h);
num[j]=num[j-s[i].h]+1;
//if(dp[j]==37)
//printf("%d\n",num[j]);
}
}
int maxn=0;
for(int i=0;i<=maxx;i++)
if(maxn<dp[i])
maxn=dp[i];
printf("%d\n",maxn-1);
}
return 0;
}
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
int h;
int a;
int c;
}s[500];
int dp[50000],c[5000][2];
int cmp(const node p,const node q)
{
return p.a<q.a;
}
int main()
{
int n;
while(scanf("%d",&n)>0)
{
int cnt=0,maxx=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&s[i].h,&s[i].a,&s[i].c);
if(maxx<s[i].a)
maxx=s[i].a;
}
//printf("%d\n",maxx);
sort(s+1,s+1+n,cmp);
//for(int i=1;i<=n;i++)
//printf("%d %d %d\n",s[i].h,s[i].a,s[i].c);
for(int i=1;i<=n;i++)
{
int k=1;
while(s[i].c-k>0)
{
c[cnt][0]=k*s[i].h;
c[cnt++][1]=s[i].a;
s[i].c-=k;
k*=2;
//if(s[i].h==5)
//printf("%d %d\n",c[cnt-1][0],c[cnt-1][1]);
}
c[cnt][0]=s[i].h*s[i].c;
c[cnt++][1]=s[i].a;
}
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=0;i<cnt;i++)
{
for(int j=maxx;j>=c[i][0];j--)
if(dp[j-c[i][0]]&&dp[j-c[i][0]]+c[i][0]-1<=c[i][1]&&dp[j-c[i][0]]+c[i][0]>dp[j])
{
dp[j]=dp[j-c[i][0]]+c[i][0];
//printf("%d\n",dp[j]);
}
}
int maxn=0;
for(int i=0;i<=maxx;i++)
if(maxn<dp[i])
maxn=dp[i];
printf("%d\n",maxn-1);
}
return 0;
}
dp之多重背包poj2392的更多相关文章
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- dp之多重背包hdu1059
题意:价值为1,2,3,4,5,6. 分别有n[1],n[2],n[3],n[4],n[5],n[6]个.求能否找到满足价值刚好是所有的一半的方案. 思路:简单的多重背包,我建议多重背包都用二进制拆分 ...
- nyoj 546——Divideing Jewels——————【dp、多重背包板子题】
Divideing Jewels 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. ...
- dp之多重背包(二进制优化)
void solve(int v,int w,int c){ int count=0; for(int k=1;k<=c;k<<=1) { val[c ...
- 硬币问题 (dp,多重背包的二分优化)
题目描述 给你n种硬币,知道每种的面值Ai和每种的数量Ci.问能凑出多少种不大于m的面值. 输入 有多组数据,每一组第一行有两个整数 n(1≤n≤100)和m(m≤100000),第二行有2n个整数, ...
- hdu1059&poj1014 Dividing (dp,多重背包的二分优化)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- [DP之多重背包优化方法]
首先我们看一道有趣的题目 然后这道题很快想到是一个多重背包和无限背包混合体 那么我们就以这道题 来讨论一下多重背包的优化 首先我们看看朴素打法 memset(F,,]=; ;i<=N;i++) ...
- poj 1742 Coins(dp之多重背包+多次优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- dp之多重背包poj1276
题意:有现今cash,和n种钱币,每种钱币有ni个,价值为di,求各种钱币组成的不超过cash的最大钱数....... 思路:二进制拆分转化为01背包,或者转化为完全背包都是可以的. 反思:这个题目我 ...
随机推荐
- JavaScript实现瀑布流
前端内容: 使用JavaScript和四个div,将照片放入四个div中 <!DOCTYPE html> <html lang="en"> <head ...
- jQuery中读取json文件
json文件是一种轻量级的数据交互格式.一般在jquery中使用getJSON()方法读取. $.getJSON(url,[data],[callback]) url:加载的页面地址 data: 可选 ...
- SQL PRIMARY KEY,SQL FOREIGN KEY
A primary key is defined as a column or a group of column that their value are always be unique. Nor ...
- [Python爬虫] 之三十一:Selenium +phantomjs 利用 pyquery抓取消费主张信息
一.介绍 本例子用Selenium +phantomjs爬取央视栏目(http://search.cctv.com/search.php?qtext=消费主张&type=video)的信息(标 ...
- [Compose] 17. List comprehensions with Applicative Functors
We annihilate the need for the ol' nested for loop using Applicatives. For example we have this kind ...
- nGrinder工具进行接口性能测试
1.背景 之前在这篇文章中性能测试初探—接口性能测试介绍过nGrinder,本文将介绍在nGrinder脚本中使用资源文件中数据作为接口参数和解析生成的CSV结果,生成TPS标准差,TPS波动率,最小 ...
- 10分钟,利用canvas画一个小的loading界面
首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" styl ...
- iOS 检查app版本更新操作
iOS 检查app版本更新操作 1:获取本地安装的版本信息: [NSString stringWithFormat:@"Version %@ (%@)", [[[NSBundle ...
- com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "ExceptionId"
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Exception ...
- iOS中使用iCloud一些须要注意的地方(Xcode7.2)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;) 在自己的App中怎样使用iCloud有非常多文章能够查阅,这里 ...