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 ...
随机推荐
- PhpStorm比较高级的一些设置
开始使用phpstorm工具,总体感觉还是不错的.有点不舒服的就是他占用内存比较高,反应速度有时会卡,这跟他的功能强大有关系.有些功能对于 我来说是不怎么需要的,比如自动保存功能,会频繁的保存一些文件 ...
- Aspose.Total 最全的一套office文档管理方案
百度百科:http://baike.baidu.com/link?url=ymHDlHGW2-6qomYsFneByjeb3OF4JJsHQkyhHFdFlPjcaYR-MT7xFBGttnzdI_W ...
- django上课笔记4-复习数据库操作-复习模板-Seccion-详细cookie和session的区别
一.复习数据库操作 字段类型 字符串 EmailField(CharField): IPAddressField(Field) URLField(CharField) SlugField(CharFi ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门
默认的身份认证好授权系统 UserManager用来操作用户的类, Singi用来身份认证的 添加AccountController 先声明SignInManager和UserManager这两个服务 ...
- Swift里计数相关的小细节
Swift里对于字符串这些引入了index型,相对其他语言而言字符操作更安全了,但是问题就是一不注意搞错范围就会有各种离奇的bug. 在讲主题前,先说个小细节. Swift里非常严密的定义了一大堆字符 ...
- JAVA多线程(三) 线程池和锁的深度化
github演示代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-servic ...
- scrapy 连接错误
twisted.python.failure.failure twisted.internet.error.connectionlost: connection to the other side w ...
- iOS RSA (Objc)
/* RSA.h @author: ideawu @link: https://github.com/ideawu/Objective-C-RSA */ #import <Foundation/ ...
- RobotFramework自动化测试框架(3)- RobotFramework扩展测试库、资源文件、变量文件
扩展测试库 扩展测试库可使用python或java语言编写.后直接导入需要使用的测试用例文件即可. 具体的实现和操作,后续补充.请参考官网. 资源文件 在资源文件中定义用户关键字,它提供了共享机制,即 ...
- April Fools Contest 2017 B
Description Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? Input The i ...