Uva--437the Tower of BABYLON+DP

Source: Internet
Author: User

Test Instructions:

Given some cubes, for two cubes, only one of the two sides of the bottom is strictly less than the other one can be placed above it. The maximum height that can be obtained.

Ideas:

A cube can be turned into 6 different rectangles with different weights (heights) on the bottom, and then the nesting of these rectangles. It used to turn the problem into the longest way on a DAG map, and this time the rectangles are sorted by the bottom area from small to large, and then the problem becomes the longest ascending subsequence.


The code is as follows:


#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace Std;int k;typedef struct{int x, y, Z;} S S St[200];int CMP (s s1,s s2) {return s1.x*s1.y>s2.x*s2.y;}    int main () {int n,case=0;        while (scanf ("%d", &n) &&n) {k=1;            for (int i=0;i<n;i++) {int x, y, Z;            scanf ("%d%d%d", &x,&y,&z); St[k].x=x; St[k].y=y;            St[k].z=z;            k++; St[k].x=y; St[k].y=x;            St[k].z=z;            k++; St[k].x=z; St[k].y=x;            St[k].z=y;            k++; St[k].x=x; St[k].y=z;            St[k].z=y;            k++; St[k].x=z; St[k].y=y;            St[k].z=x;            k++; St[k].x=y; St[k].y=z;            St[k].z=x;        k++;        } sort (st+1,st+k,cmp);        int dp[200],ans=0;        for (int i=1;i<k;i++) dp[i]=st[i].z; for (int i=1;i<k;i++) for (int j=1;j<i;j++) {if (st[i].x< st[j].x&&st[i].y<st[j].y) {Dp[i]=max (dp[i],dp[j]+st[i].z);                Ans=max (Ans,dp[i]);    }} printf ("Case%d:maximum height =%d\n", ++case,ans); } return 0;}




Uva--437the Tower of BABYLON+DP

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.