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背包,或者转化为完全背包都是可以的. 反思:这个题目我 ...
随机推荐
- T-SQL经典语句(SQL server)
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- WordPress 禁用文章修订和自动保存的方法
以下代码亲测并没有效果,不能禁用自动保存 /* 移除自动保存和修订版本 */ remove_action('pre_post_update', 'wp_save_post_revision' ); ...
- 杭电 HDU 1031 Design T-Shirt
Design T-Shirt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- (剑指Offer)面试题49:把字符串转换为整数
题目: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 思路: 考虑+.-.空格.非数字字符,以及溢出问题 代码: #include <iostream> using n ...
- Oracle spatial 空间修正函数(SDO_UTIL.RECTIFY_GEOMETRY)
Oracle spatial有个空间修正函数SDO_UTIL.RECTIFY_GEOMETRY,它可以修复以下可能:a.重复节点 b.自相交 c.坐标串朝向不正确. 该函数的构造函数格式: SDO_U ...
- 10分钟,利用canvas画一个小的loading界面
首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" styl ...
- 自己定义View步骤
概述 Android已经为我们提供了大量的View供我们使用,可是可能有时候这些组件不能满足我们的需求,这时候就须要自己定义控件了.自己定义控件对于刚開始学习的人总是感觉是一种复杂的技术. 由于 ...
- VS选项中没有C#相关设置选项?
在VS中,行号的设置在工具-选项-文本编辑器,可里面没有VS,我还以为是之前卸载了SP1后造成的. 原来是因为,VS默认环境设置的VB,选项对话框左下角有"显示所有设置"的复选框, ...
- nginx命令:启动,停止及命令参数详解
nginx命令:启动nginx 在Windows上安装好nginx后,我们需要启动nginx服务,启动nginx服务的命令行操作主要有两种方式,即 1 C:/nginx-0.8.53>nginx ...
- TCP/IP编程 - 1) 基础知识
1. What Is a Socket?(什么是套接字) A socket is an abstraction through which an application may send and re ...