The Bookcase dp(好题!!)

这道题的疑点终于想明白了。

这道题题意就是有三层书架,每一层都至少放一本书,问三层书架的  总长*max(宽) 最小值是多少。

感觉还是很难想的,但是仔细一想,第一层,我在放完第二层,第三层的时候,默认剩下的书就全都放在了第一层。

首先按照高度由高到低进行排序,这样的目的就是保证了我放进去了这本书,所拥有的高度,剩下的书全部都能放进来,这样直接放就好了,他的高度我就不用再考虑够不够了。有了这种思想,我们就枚举,第二层第三层拥有的宽度,用dp[i][j]表示,第二层宽度为i,第三层宽度为j时,所拥有的最小高度。

这道题代码实现的时候可以考虑类似于背包的方法,还有一点很巧妙,就是当i==0或者j==0的时候,这就说明第二层或者第三层的宽度到0了,这样子不满足题意,我就需要把枚举到的这本书 放在第二层或第三层最开始的位置,所以我的高度要加上这本书的高度。而宽度不是0的时候,总书架的高度是不变的。

The Bookcase
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 1979   Accepted: 604

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.

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<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[2200][2200];
struct node
{
    int h,w;
}book[100];
bool cmp(node a,node b)
{
    return a.h>b.h;
}
int main()
{
    int t,n;
    int sum;
    scanf("%d",&t);
    while(t--){
            sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&book[i].h,&book[i].w);
            sum+=book[i].w;
        }
        sort(book+1,book+1+n,cmp);
        memset(dp,INF,sizeof(dp));
        dp[0][0]=book[1].h;
        int rong=book[1].w;
        int H;
        for(int i=2;i<=n;i++)
        {
            for(int j=rong;j>=0;j--){
                for(int k=rong;k>=0;k--){
                    if(dp[j][k]==INF)
                        continue;
                    if(j==0)
                        H=book[i].h;
                    else
                        H=0;
                    dp[j+book[i].w][k]=min(dp[j+book[i].w][k],dp[j][k]+H);
                    if(k==0)
                        H=book[i].h;
                    else
                        H=0;
                    dp[j][k+book[i].w]=min(dp[j][k+book[i].w],dp[j][k]+H);
                }
            }
            rong=rong+book[i].w;
        }
        int ans=INF;
        for(int j=1;j<=sum;j++){
            for(int k=1;k<=sum;k++)
            {
                if(dp[j][k]==INF||(sum-j-k)<=0)
                    continue;
                ans=min(ans,dp[j][k]*max(max(j,sum-j-k),k));
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/80715521