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 ...
随机推荐
- bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...
- 架构:MVC
ylbtech-架构:MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数 ...
- bzoj3198
容斥原理+哈希表 恰好k个,那么上容斥原理,我们先2^6枚举相同的位置,用哈希表判断有多少个对应位置相同的元素,然后用容斥原理计算,似乎这里的容斥没有那么简单,详见这里 http://www.cnbl ...
- Button Style
Button Style BS_3STATE 与复选框一样本样式按钮可被单击变暗.变暗状态通常用于指示本样式的按键正处于禁用状态. BS_AUTO3STATE 与三状态的复选框一样当用户选中它本按钮样 ...
- emacs在org-mode时输出pdf时,只输出为链接
这个找到问题了,写错格式了,输出pdf中要内嵌图像,格式要写成[[file:/home/xxx/yyy.jpg]],不能写成file:/home/xxx/yyy.jpg或者[[file:/home/x ...
- E20170414-ms
collapse v/n 奔溃,垮台 constraint n 约束,限制 adaptive adj 适应的; 有适应能力的; exhausitive adj. 详尽的; store n 商店,仓 ...
- 洛谷 - P2887 - 防晒霜Sunscreen - 贪心
https://www.luogu.org/problemnew/show/P2887 感觉可以: 把防晒霜拆点限制流量为瓶数,奶牛拆点限制流量为1,当某个防晒霜与奶牛匹配时连一条边,求最大流.但是这 ...
- Django学习:url路由系统
一.MTV模型 1.Django的MTV分别代表: Model(模型):和数据库相关的,负责业务对象与数据库的对象(ORM) Template(模板):放所有的html文件 模板语法:目的是将白变量( ...
- 简述网站、B/S架构与C/S架构
一.什么是网站? 定义:网站是指在因特网上根据一定的规则,使用HTML等工具制作的用于展示特定内容相关网页的集合. 简单地说,网站是一种沟通工具(或者说是一种软件——建设网站也是软件开发的一种),我们 ...
- 限制属性绑定(__slots__)
正常情况下,当定义了一个class并创建实例后,可以给该实例绑定任何属性和方法,这就是动态语言的灵活性 属性和方法是可以直接定义在class中的,但动态绑定允许在程序运行的过程中动态给class加上属 ...