简单DP
f【i】【j】【k】 表示前i本书放在书架上后第一层厚度j第二层厚度k 时三层书高度和最小的值
f【i】【j】【k】=min(f[i-1][j][k],f[i-1][j-t[i]][k],f[i-1][j][k-t[i]]) 注意如果这本书是每一层第一本需要把该书高度加上(dp之前要将书高度从大到小排序否则该递推不成立)
The Bookcase
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 1888 | Accepted: 560 |
Description
No wonder the old bookcase caved under the massive piles of books Tom had stacked
on it. He had better build a new one, this time large enough to hold all of his books. Tomfinds it practical to have the books close at hand when he works at his desk. Therefore, he is imagining a compact solution with the bookcase standing on the back of the desk. Obviously, this would put some restrictions on the size of the bookcase, it should preferably be as small as possible. In addition, Tom would like the bookcase to have exactly three shelves for aesthetical reasons.
Wondering how small his bookcase could be, he models the problem as follows. He measures the height hi and thickness ti of each book i and he seeks a partition of the books in three non-empty sets S1, S2, S3 such that is minimized, i.e. the area of the bookcase as seen when standing in front of it (the depth needed is obviously the largest width of all his books, regardless of the partition). Note that this formula does not give the exact area of the bookcase, since the actual shelves cause a small additional height, and the sides cause a small additional width. For simplicity, we will ignore this small discrepancy.
Thinking a moment on the problem, Tom realizes he will need a computer program to do the job.
on it. He had better build a new one, this time large enough to hold all of his books. Tomfinds it practical to have the books close at hand when he works at his desk. Therefore, he is imagining a compact solution with the bookcase standing on the back of the desk. Obviously, this would put some restrictions on the size of the bookcase, it should preferably be as small as possible. In addition, Tom would like the bookcase to have exactly three shelves for aesthetical reasons.
Wondering how small his bookcase could be, he models the problem as follows. He measures the height hi and thickness ti of each book i and he seeks a partition of the books in three non-empty sets S1, S2, S3 such that is minimized, i.e. the area of the bookcase as seen when standing in front of it (the depth needed is obviously the largest width of all his books, regardless of the partition). Note that this formula does not give the exact area of the bookcase, since the actual shelves cause a small additional height, and the sides cause a small additional width. For simplicity, we will ignore this small discrepancy.
Thinking a moment on the problem, Tom realizes he will need a computer program to do the job.
Input
The input begins with a positive number on a line of its own telling the number of test cases (at most 20). For each test case there is one line containing a single positive integer N, 3 ≤ N ≤ 70 giving the number of books. Then N lines follow each containing two positive integers hi, ti, satisfying 150 ≤ hi ≤ 300 and 5 ≤ ti ≤ 30, the height and thickness of book i respectively, in millimeters.
Output
For each test case, output one line containing the minimum area (height times width) of a three-shelf bookcase capable of holding all the books, expressed in square millimeters.
Sample Input
2 4 220 29 195 20 200 9 180 30 6 256 20 255 30 254 15 253 20 252 15 251 9
Sample Output
18000 29796
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 100
int dp[2][30*70+10][30*70+10];
int n;
struct book
{
int t,h;
}a[MAXN];
bool cmp(book a,book b)
{
return a.h>b.h;
}
int main()
{
int cs;
cin>>cs;
while(cs--)
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i].h>>a[i].t;
sort(a,a+n,cmp);
int cur=0;
int sumt=0,sumh=0;
memset(dp,-1,sizeof(dp));
dp[cur][0][0]=0;
for(int i=0;i<n;i++)
{
sumt+=a[i].t;
for(int j=sumt;j>=0;j--)
{
for(int k=sumt-j;k>=0;k--)
{
if(j+k==sumt-a[i].t && dp[cur][j][k]!=-1)
dp[cur^1][j][k]=dp[cur][j][k]+a[i].h;
else if(dp[cur][j][k]!=-1)
dp[cur^1][j][k]=dp[cur][j][k];
else
dp[cur^1][j][k]=-1;
if(j>=a[i].t && dp[cur][j-a[i].t][k]!=-1)
{
if(dp[cur^1][j][k]==-1)
dp[cur^1][j][k]=INF;
if(j==a[i].t)
dp[cur^1][j][k]=min(dp[cur^1][j][k],dp[cur][j-a[i].t][k]+a[i].h);
else
dp[cur^1][j][k]=min(dp[cur^1][j][k],dp[cur][j-a[i].t][k]);
}
if(k>=a[i].t && dp[cur][j][k-a[i].t]!=-1)
{
if(dp[cur^1][j][k]==-1)
dp[cur^1][j][k]=INF;
if(k==a[i].t)
dp[cur^1][j][k]=min(dp[cur^1][j][k],dp[cur][j][k-a[i].t]+a[i].h);
else
dp[cur^1][j][k]=min(dp[cur^1][j][k],dp[cur][j][k-a[i].t]);
}
}
}
cur^=1;
}
int ans=0x3f3f3f3f;
for(int i=sumt;i>0;i--)
for(int j=sumt-i;j>0;j--)
{
if(dp[cur][i][j]==-1) continue;
int k=sumt-j-i;
if(k==0) continue;
k=max(i,max(j,k));
ans=min(ans,dp[cur][i][j]*k);
}
cout<<ans<<endl;
}
return 0;
}