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 ...
随机推荐
- linux/unix下 pid文件作用浅析
l在linux系统的目录/var/run下面一般我们都会看到很多的*.pid文件.而且往往新安装的程序在运行后也会在/var/run目录下面产生自己的pid文件.那么这些pid文件有什么作用呢?它的内 ...
- qunar面试题及一位大牛的解答
本文摘自:http://www.cnblogs.com/jarson-7426/p/3989208.html 1.写一个函数padstare(string str1,min_lenthg,string ...
- Meta标签中的format-detection属性及含义(转)
一.Meta标签中的format-detection属性及含义 意为:格式检测 或许你会有这样的经历:当你在制作手机端的页面中,点击了没有加任何链接的格式的数字时,这时手机会进行自动拔号提示操作! 禁 ...
- class.forName()和.class有什么区别?
class.forName()会初始化类的成员(静态的),最先加载的是类的静态成员变量,然后是静态代码块. 访问常量并不会导致类的初始化,但是访问静态成员会.
- mysql5.7根据.frm和.ibd文件恢复表结构和数据
一.恢复表结构 1.环境:Windows .mysql5.7:首先创建一个数据库,可以通过navicat来创建: 2.使用当前创建的数据库:use ww; 3.随意创建一张表,但是这张表的名字 ...
- ACM-ICPC 中可能会使用到的库
sort(v.first(),v.end(),cmp())unique(v.first(),v.end(),cmp()) 第三个参数可以传入一个bool型,用来判断是不是相等,返回unique后的超尾 ...
- Codeforces 550B 【暴力】
题意: 有n个数字, 要求在这n个数中选出至少两个数字, 使得它们的和在L,R之间,并且最大的与最小的差值要不小于x 思路: 撒比了啊... 根据状态的话一共也就是2^15-直接暴力,二进制的小魅力还 ...
- NOIp2002神经网络 【拓扑排序】By cellur925
题目传送门 这道题目没有什么难的,是一道拓扑排序+递推的题目.我的思路是开始处理出拓扑序,然后因为数据范围很小怎么搞都可以,就邻接矩阵存图+暴力枚举.结果60分. 后来看题解发现,大家都是边拓扑边进行 ...
- 如何在VS2010的VC++ 基于对话框的MFC程序中添加菜单
方法1:亲测 成功 转载自https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/48338f6b-e5d9-4c0c-8b17-05ca3ef ...
- Educational Codeforces Round 20 B
Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...