题意:

有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的)

分析:

不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度

dp[i][j]=min(dp[i-w[i]][j],dp[i][j-w[i]])放在第一、二层取最小,当i,j放的是第一本书的时候要加上相应的高度,先按高度升序排列保证正确性。

最后遍历求最小面积

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct book{
int h,w;
}b[];
int dp[][],sum,n;
bool cmp(book x,book y){
if(x.h==y.h)return x.w<y.w;
return x.h>y.h;
}
void solve(){
for(int i=;i<=sum;++i)
for(int j=;j<=sum;++j)
dp[i][j]=INF;
dp[][]=;
int tw=;
for(int i=;i<n;++i){
for(int j=tw;j>=;--j)
for(int k=tw;k>=;--k){
if(dp[j][k]==INF||j+k>tw)continue;
int tmp=;
if(j==)
tmp=b[i].h;
dp[j+b[i].w][k]=min(dp[j+b[i].w][k],dp[j][k]+tmp);
tmp=;
if(k==)
tmp=b[i].h;
dp[j][k+b[i].w]=min(dp[j][k+b[i].w],dp[j][k]+tmp);
}
tw+=b[i].w;
}
int mina=INF;
for(int i=;i<=sum;++i)
for(int j=;j<=sum;++j){
if(dp[i][j]==INF||i+j>=sum)continue;
int k=sum-i-j;
int mw=max(max(i,j),k);
mina=min(mina,(dp[i][j]+b[].h)*mw);
}
printf("%d\n",mina);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
sum=;
for(int i=;i<n;++i){
scanf("%d%d",&b[i].h,&b[i].w);
sum+=b[i].w;
}
sort(b,b+n,cmp);
solve();
}
return ;
}

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 ...

  10. Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)

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

随机推荐

  1. http://www.aboutyun.com/thread-8792-1-1.html

    http://www.aboutyun.com/thread-8792-1-1.html

  2. nohup 程序名 & (使程序推到后台运行,即使终端关闭,该程序依然运行)

    IshallbeThatIshallbe:~ iamthat$ ps -ef |grep ping 502 450 1 0 9:30PM ?? 0:00.05 ping www.baidu.com 5 ...

  3. hdu 3863 No Gambling

    #include<stdio.h> int main() { int n; ) { printf("I bet on Oregon Maple~\n"); } ; } ...

  4. LR_问题_如何将场景中的用户设置为百分比形式

    一个场景运行多个脚本时,如何按照百分比模式运行

  5. esriControlsMousePointer 控制鼠标指针

    axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass; 控制鼠标指针选项. 不变 值 描述 esriPo ...

  6. 含有特殊字符的JSON串解析方法

    工具方法:public static <T> T parseJsonString(String json,Class<T> classType){ ObjectMapper m ...

  7. C#Winfrom中,窗体加载时会自动执行一次控件的textchange事件,怎么让它不执行?

    http://zhidao.baidu.com/link?url=iTSyfa5_RJBSb37S8efdWoL5eDMrnxeAm-prhGSNBXqdP9r7PzNDQTc7gVzJgCNdzli ...

  8. NFC(3)Android上的NFC,开启NFC,3种NDEF数据

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...

  9. windows 下 apache设置

    apache,apache配置,配置端口 mac下apache配置 添加虚拟主机: Alias /selftest/ "D:/self-test/" <Directory & ...

  10. windows线程同步

    一.前言 之前在项目中,由于需要使用到多线程,多线程能够提高执行的效率,同时也带来线程同步的问题,故特此总结如下. 二.windows线程同步机制 windows线程同步机制常用的有几种:Event. ...