uva12099 The Bookcase
这道题超经典。dp和优化都值得看一看。
因为i+1只和i有关,用滚动数组节省空间
暑假第一次做感觉很困难,现在看就清晰了很多
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ;
const int maxw = ;
const int INF = ; struct Book {
int h, w;
bool operator < (const Book& rhs) const {
return h > rhs.h || (h == rhs.h && w > rhs.w);
}
} books[maxn];
// dp[i][j][k] is the minimal total heights of level 2 and 3 when we used i books, level 2 and 3's total widths are j and k, int dp[][maxn*maxw][maxn*maxw];
int sumw[maxn]; // sum[i] is the sum of widths of first i books. sum[0] = 0. // increased height if you place a book with height h to a level with width w
// if w == 0, that means the level if empty, so height is increased by h
// otherwise, the height is unchanged because we're adding books in decreasing order of height
inline int f(int w, int h) {
return w == ? h : ;
} inline void update(int& newd, int d) { //更新最低高度
if(newd < || d < newd) newd = d;
} int main () {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%d%d", &books[i].h, &books[i].w);
sort(books, books+n); sumw[] = ;
for(int i = ; i <= n; i++)
sumw[i] = sumw[i-] + books[i-].w; dp[][][] = ;
int t = ; //滚动数组,t表示当前状态
for(int i = ; i < n; i++) {
// Don't use memset. It's too slow
for(int j = ; j <= sumw[i+]; j++)
for(int k = ; k <= sumw[i+]-j; k++) dp[t^][j][k] = -;//初始化下一个状态 for(int j = ; j <= sumw[i]; j++)
for(int k = ; k <= sumw[i]-j; k++)
if(dp[t][j][k] >= ) {
update(dp[t^][j][k], dp[t][j][k]); // level 1
update(dp[t^][j+books[i].w][k], dp[t][j][k] + f(j,books[i].h)); // level 2
update(dp[t^][j][k+books[i].w], dp[t][j][k] + f(k,books[i].h)); // level 3
}
t ^= ;
} int ans = INF;
for(int j = ; j <= sumw[n]; j++) // each level has at least one book
for(int k = ; k <= sumw[n]-j; k++) if(dp[t][j][k] >= ) {
int w = max(max(j, k), sumw[n]-j-k);
int h = books[].h + dp[t][j][k];
ans = min(ans, w * h);
}
printf("%d\n", ans);
}
return ;
}
uva12099 The Bookcase的更多相关文章
- CodeForces #368 div2 D Persistent Bookcase DFS
题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- Persistent Bookcase
Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- 新概念英语(1-37)Making a bookcase
What is Susan's favourite color ? A:You're working hard, Georage. What are you doing? B:I am making ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力
D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...
- codeforces 707D:Persistent Bookcase
Description Recently in school Alina has learned what are the persistent data structures: they are d ...
- codeforces 707D D. Persistent Bookcase(dfs)
题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...
随机推荐
- JAVA GUI THREAD---***
针对用户界面的多线程 GUI下面的多线程方式 1.与GUI类分离方式 分离方式,在创建线程类实例时需要代入GUI句柄,通过GUI句柄操作GUI,也就是说线程类和GUI类都要有对方的实例,以便相互操作. ...
- Struts2 文件上传 之 文件类型 allowedTypes
转自:https://www.cnblogs.com/zxwBj/p/8546889.html '.a' : 'application/octet-stream', '.ai' : ...
- Linux中的gdb调试方法总结
- Cardboard profile的修改
Cardboard盒子中,手机屏幕大小.镜片离屏幕距离,屏幕分辨率等都会影响配戴者的眼中成像,通过对生成图像的变形可以部份解决这一问题,cardboard sdk中提供了cardboardprofil ...
- c语言函数参考
...
- 20170407-ms
invoke v调用 dismiss v解雇 exclusive adj. 专用的; 高级的; 排外的; 单独的; n. 独家新闻; 专有物; 独家经营的产品(或项目.设计等); 排外者; ex ...
- Codeforces Round #421 (Div. 2)B. Mister B and Angle in Polygon(模拟+精度控制)
传送门 题意 给出正n多边形和一个数a,寻找与a最接近的角,输出角编号 分析 找出多边形上所有角,一一比对即可 trick 1.判断的时候注意精度,i.e.x-eps>0 2.double与do ...
- hdoj5327【前缀和思想】
题意: 找给定区间的美丽数,美丽数的意思就是这个数每个位上的数都是唯一的. 思路: 前缀和的思想. 感想: 就是你当前位置代表某个特性的前面的所有和(瞎比比的,说了下感觉).前提是你必须找到这样的特性 ...
- Beta版本冲刺第三天!
该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...
- iOS 监测电话呼入
1.首先引入CoreTelephony框架,代码里: @import CoreTelephony; 项目设置里: 2.定义属性,建立强引用: @property (nonatomic, strong) ...