"Summer Vacation" [in-depth dynamic planning]uva 10618 the Bookcase

Source: Internet
Author: User

UVa 12099 the Bookcase

Topic:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067

Ideas:

Assign n books to three layers, making the bookshelf w*h the smallest

Prioritize books in advance, as the first book (the highest book) will be taken into account regardless of whether it is placed on that level, so it is stipulated that it should be placed on the first layer, and the second layer is higher than the third.

Because of the relationship from the big to the small sort, as long as jk==0 so newly added book I is the height of the layer, otherwise the height is unchanged.

set D[I][J][K] to consider the second layer of I book width is j the third layer width is k when the minimum height of two or three layers and.

State transition equation: (---update)

D[I][J][K]->D[I+1][J][K]

D[i][j][k]+f (j,book[i].h)->d[i+1][j+book[i].w][k]

D[i][j][k]+f (K,BOOK[I].H->D[I+1][J][K+BOOK[I].W]

Define F (b): return B esle return 0 when a==0; On the first layer, the height is book[0].h width of pre_w[n]-ww2-ww3;

Optimization:

    1. Time: Considering the first book, J+k does not exceed the sum of the width of the previous I book, reducing the enumeration range of JK.
    2. Time: The book says: If ww2>ww1+30 so can put the 2nd floor of a book on the 1th floor is not worse, so only need to calculate ww2<=ww1+30 and ww3<=ww2+30 situation. At this time there are j<=1065 k<=720.
    3. Space: Scrolling array, the details are not good to judge so keep two lines at the same time, manipulate rows and update rows.

Code:

1#include <iostream>2#include <algorithm>3 #definefor (A,B,C) for (int a= (b); a<= (c); a++)4 using namespacestd;5 6 Const intMAXN = -+5;7 Const intMAXW = -;8 Const intINF =1<< -;9 Ten structnode{ One     intw,h; A     BOOL operator< (Constnode& RHS)Const {  -       returnH>rhs.h | | (H==rhs.h && w>RHS.W); -     } the }BOOK[MAXN]; -  - intd[2][maxn*maxw][maxn*MAXW]; - intPRE_W[MAXN]; +  - intN; +  AInlineintFintJinth) { at     returnj==0? H:0;  - } -InlinevoidUpdateint& X,intv) { -     if(x<0|| V&LT;X) x=v; - }  -  in intMain () { -Ios::sync_with_stdio (false);  to     +    intT Cin>>T; -     while(t--) { theCin>>N; *for (I,0, N-1) cin>>book[i].h>>BOOK[I].W; $Sort (book,book+N); Panax Notoginsengpre_w[0]=0; -for (I,1, N) pre_w[i]=pre_w[i-1]+book[i-1].W; the           +d[0][0][0]=0; A          intt=0; thefor (I,0, N-1) { +For (J,0, pre_w[i+1]) -For (K,0, pre_w[i+1]-J) d[t^1][j][k]=-1;  $                $For (J,0, Pre_w[i]) -For (K,0, pre_w[i]-j)if(d[t][j][k]>=0){  -Update (d[t^1][j][k],d[t][j][k]); theUpdate (d[t^1][j+book[i].w][k],d[t][j][k]+f (j,book[i].h)); -Update (d[t^1][j][k+book[i].w],d[t][j][k]+f (k,book[i].h));Wuyi               } thet^=1;  -      } Wu             -               About              intans=INF; $For (J,1, Pre_w[n]) -For (K,1, pre_w[n]-j)if(d[t][j][k]>=0){ -                   intW=max (Max (j,k), pre_w[n]-j-k); -             inth=d[t][j][k]+book[0].h;  AAns=min (ans,w*h); +               } thecout<<ans<<"\ n"; -    } $    return 0; the}

"Summer Vacation" [in-depth dynamic planning]uva 10618 the Bookcase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.