【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase
题目:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067
思路:
将n本书分配到三层,使得形成的书架w*h最小
提前将书籍按照高度排序,因为无论第一本书(最高的书)无论放在那一层都会被考虑到,所以规定将它放在第一层,且第二层比第三层高。
因为从大到小排序的关系,只要jk==0那么新加入的书i就是该层的高度,否则高度不变。
设d[i][j][k]表示考虑过i本书第二层宽度为j第三层宽度为k时二三层的最小的高度和。
状态转移方程:(->表示更新)
d[i][j][k]->d[i+1][j][k]
d[i][j][k]+f(j,book[i].h)->d[i+1][j+book[i].w][k]
d[i][j][k]+f(k,book[i].h->d[i+1][j][k+book[i].w]
定义f(a,b):当a==0时return b esle return 0; 关于第一层,高度为book[0].h宽度为pre_w[n]-ww2-ww3;
优化:
- 时间:考虑到第i本书时, j+k不超过前i本书的宽度之和,减小jk的枚举范围。
- 时间:书上这样说:如果ww2>ww1+30 那么可以把第2层的一本书放到第1层情况不会更差,所以只需要计算ww2<=ww1+30 且ww3<=ww2+30情况。此时有j<=1065 k<=720。
- 空间:滚动数组,细节不好判断因此同时保留两行,操作行与更新行。
代码:
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int maxn = + ;
const int maxw = ;
const int INF = <<; struct Node{
int w,h;
bool operator <(const Node& rhs) const {
return h>rhs.h || (h==rhs.h && w>rhs.w);
}
}book[maxn]; int d[][maxn*maxw][maxn*maxw];
int pre_w[maxn]; int n; inline int f(int j,int h) {
return j==? h:;
}
inline void update(int& x,int v) {
if(x< || v<x) x=v;
} int main() {
ios::sync_with_stdio(false); int T; cin>>T;
while(T--) {
cin>>n;
FOR(i,,n-) cin>>book[i].h>>book[i].w;
sort(book,book+n);
pre_w[]=;
FOR(i,,n) pre_w[i]=pre_w[i-]+book[i-].w; d[][][]=;
int t=;
FOR(i,,n-) {
FOR(j,,pre_w[i+])
FOR(k,,pre_w[i+]-j) d[t^][j][k]=-; FOR(j,,pre_w[i])
FOR(k,,pre_w[i]-j) if(d[t][j][k]>=){
update(d[t^][j][k],d[t][j][k]);
update(d[t^][j+book[i].w][k],d[t][j][k]+f(j,book[i].h));
update(d[t^][j][k+book[i].w],d[t][j][k]+f(k,book[i].h));
}
t^=;
} int ans=INF;
FOR(j,,pre_w[n])
FOR(k,,pre_w[n]-j) if(d[t][j][k]>=){
int w=max(max(j,k),pre_w[n]-j-k);
int h=d[t][j][k]+book[].h;
ans=min(ans,w*h);
}
cout<<ans<<"\n";
}
return ;
}
【暑假】[深入动态规划]UVa 10618 The Bookcase的更多相关文章
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
- 【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall
UVa 10618 Fixing the Great Wall 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=361 ...
- 【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection
UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to lea ...
- UVa 12099 The Bookcase - 动态规划
题目大意 给定一些书,每个书有一个高度和宽度,然后将它们放到一个三层的书架里(要求每一层都不为空).定义书架的大小为每层最大的高度和 乘 每层宽度和的最大值.求最小的书架大小. 显然动态规划(直觉,没 ...
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 12170 Easy Climb
UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路: 引别人一 ...
- 【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
- 【暑假】[深入动态规划]UVa 1412 Fund Management
UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS Memory Limit: Unknown ...
随机推荐
- eclipse/MyEclipse 日期格式、注释日期格式、时区问题
eclipse/MyEclipse 日期格式.注释日期格式.时区问题 在eclipse/MyEclipse中,如果你的注释或是运行System.out.print(new java.util.Date ...
- PAT-乙级-1004. 成绩排名 (20)
1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...
- spring <form:checkboxes> tag and css class
I have issue with: <form:checkboxes path="roles" cssClass="checkbox" items=&q ...
- Visual C++ unicode and utf8 转换
ATL宏: USES_CONVERSION; W2A A2W CString StringUtil::UTF8_to_UNICODE(const char *utf8_string, int leng ...
- PHP set_exception_handler 设置异常处理函数
If you're handling sensitive data and you don't want exceptions logging details such as variable con ...
- linux cd命令不带路径参数
#切换到当前用户的主目录.若为root用户,则切换到/root,若普通用户,则切换到/home/username $ cd
- Eclipse执行Hadoop WordCount
前期工作 我的Eclipse是安装在Windows下的,通过Eclipse执行程序连接Hadoop, 需要让虚拟机的访问地址和本机的访问地址保持在同一域内,虚拟机的地址更改前面的文章介绍过了,如果想改 ...
- 一个简单的Android小实例
原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台 3.安装J ...
- WinForm实现简单的拖拽文件到出题的功能(C#)(3)
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString(); textBox1. ...
- Counting sheep...
Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...