【暑假】[深入动态规划]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 ...
随机推荐
- java 取小数点后两位 不四舍五入,怎么做
java 取小数点后两位 不四舍五入,怎么做 正常版: //正常版: import java.text.DecimalFormat; import java.math.RoundingMode; De ...
- BZOJ 4146 [AMPPZ2014] Divisors 解题报告
这个题感觉比较小清新... 我们记录每个数出现的次数 $T_i$. 首先依次枚举每个数字,令 $ans = ans + T_i \times (T_i - 1)$,然后枚举这个数的倍数,令 $ans ...
- uva 701
参考了一下http://hi.baidu.com/renxl51/item/e80b688f9f54aadd5e0ec1de 给一个数字x,求最小的正整数e,使得pow(2,e) == x*pow(1 ...
- 关于Spark中RDD的设计的一些分析
RDD, Resilient Distributed Dataset,弹性分布式数据集, 是Spark的核心概念. 对于RDD的原理性的知识,可以参阅Resilient Distributed Dat ...
- C++: 单例模式和缺陷
C++: 单例模式和缺陷 实现一个单例模式 1 class Singleton { 2 private: 3 Singleton() { cout << " ...
- linux 下安装redis以及php Redis扩展
[php] view plaincopy在CODE上查看代码片派生到我的代码片 linux 下安装redis以及php Redis扩展 环境配置: centos6. nginx/ php/ mysql ...
- Agri Net POJ1258 && Constructing Roads POJ2421
题意,在给出的图中,使用最小花费的边,使这个图仍然连通. #include <cstdio> #include <algorithm> #include <cstring ...
- 218. The Skyline Problem
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
- 208. Implement Trie (Prefix Tree)
题目: Implement a trie with insert, search, and startsWith methods. 链接: http://leetcode.com/problems/i ...
- ZOJ1586——QS Network(最小生成树)
QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...