这道题超经典。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的更多相关文章

  1. CodeForces #368 div2 D Persistent Bookcase DFS

    题目链接:D Persistent Bookcase 题意:有一个n*m的书架,开始是空的,现在有k种操作: 1 x y 这个位置如果没书,放书. 2 x y 这个位置如果有书,拿走. 3 x 反转这 ...

  2. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  3. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

  4. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  5. Persistent Bookcase

    Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  6. 新概念英语(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 ...

  7. Codeforces Round #368 (Div. 2) D. Persistent Bookcase 离线 暴力

    D. Persistent Bookcase 题目连接: http://www.codeforces.com/contest/707/problem/D Description Recently in ...

  8. codeforces 707D:Persistent Bookcase

    Description Recently in school Alina has learned what are the persistent data structures: they are d ...

  9. codeforces 707D D. Persistent Bookcase(dfs)

    题目链接: D. Persistent Bookcase time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. 【NOIP2016】 组合数问题

    [题目链接] 点击打开链接 [算法] 杨辉三角 + 二维前缀和 O(1)计算答案 [代码] #include<bits/stdc++.h> using namespace std; #de ...

  2. HNOI2008 越狱 (组合数学)

    传送门 应该是HNOI2008年最简单的一道题了吧……简单的组合数题,不过要换个思路. 我们直接考虑发生越狱的情况似乎有点复杂,那我们换个思路,考虑不发生越狱的情况,也就是两个有相同宗教的人不会坐在一 ...

  3. MongoDB 用户名密码登录

    Mongodb enable authentication MongoDB 默认直接连接,无须身份验证,如果当前机器可以公网访问,且不注意Mongodb 端口(默认 27017)的开放状态,那么Mon ...

  4. python之路,day7-面向对象变成

    本篇内容: 面向对象.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 一.面向对象高级语法部分 静态方法: #@staticmethod只是名义上归类管理,实际上跟类没什么关系 ...

  5. 6-9 Haar+adaboost人脸识别

    我们重点分析了Haar特征的概念以及如何计算Haar特征,并介绍了Haar+Adaboost分类器它们的组合以及Adaboost分类器如何使用和训练.这节课我们将通过代码来实现一下Haar+Adabo ...

  6. codeforces 724C

    在一个nxm的镜面二维空间内,向(1,1)发射一条射线,来回反射,当遇到四个角之一时光线消失. 给K个点,问K个点第一次被射中是什么时候(v = sqrt(2)) 解:注意到只有 2*(n+m)个对角 ...

  7. vue父组件调用子组件方法

    父组件: 代码 <sampleapplylinemodel ref="sampleapplylinemodel" @reLoad="_fetchRecords&qu ...

  8. P1211 街道赛跑

    又是一下午的杠题,累啊~~~ 这道题第一问很简单,只需去掉一个点,判断能不能到达终点就行了: 第二问其实仔细想想也不难,就是判断去掉一个点后是否形成两个图:首先要知道是建立在第一问的基础上的:在加边的 ...

  9. python 列表 元组

    1.列表的基本声明和赋值 #声明一个空列表 变量 = [] 或者 变量 = list() var = [1] var2 = list() # 声明带有数据的列表 var = [1,2,3,'一','二 ...

  10. hdu 3484 Interviewe RMQ+二分

    #include <cstdio> #include <iostream> #include <algorithm> using namespace std; + ...