HDU 1069 Monkey and Banana

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

單調遞增子序列的變形,一種長方體雖說可以有無限個,但它最多有3中擺放方法(我們假設x方向的長度不小於y方向的長度)。

然後對x遞減一級排序,y遞減二級排序,相當於按面積遞減排序。

dp初始化就是對應狀態的長方體的高度

如果第j個長方體的x,y分別(嚴格)大於第i個長方體的x,y    (這裡排序後的j<i)

說明第j個長方體可以放在第i個長方體的下面

更新壘起來的長度

 

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7  8 struct Cube 9 {10     int x, y, z;11 }cube[100];12 13 int dp[100];14 15 bool cmp(Cube a, Cube b)16 {17     if(a.x != b.x)18         return (a.x > b.x);19     return (a.y > b.y);20 }21 22 int main(void)23 {24     #ifdef LOCAL25         freopen("1069in.txt", "r", stdin);26     #endif27 28     int n, kase = 0;29     while(scanf("%d", &n) && n)30     {31         int i, j;32         for(i = 0; i < n; ++i)33         {34             int x, y, z;35             scanf("%d%d%d", &x, &y, &z);36             cube[i*3].z = z;37             cube[i*3].x = max(x, y);38             cube[i*3].y = min(x, y);39 40             cube[i*3 + 1].z = x;41             cube[i*3 + 1].x = max(y, z);42             cube[i*3 + 1].y = min(y, z);43 44             cube[i*3 + 2].z = y;45             cube[i*3 + 2].x = max(x, z);46             cube[i*3 + 2].y = min(x, z);47         }48         sort(cube, cube + n*3, cmp);49 50         for(i = 0; i < n*3; ++i)51             dp[i] = cube[i].z;52         for(i = 1; i < n*3; ++i)53             for(j = 0; j < i; ++j)54             {55 56                 if(cube[j].x > cube[i].x 57                     && cube[j].y > cube[i].y)58                     dp[i] = max(dp[i], dp[j] + cube[i].z);59             }60 61         int ans = cube[0].z;62         for(i = 0; i < n*3; ++i)63             ans = max(ans, dp[i]);64         printf("Case %d: maximum height = %d\n", ++kase, ans);65     }66     return 0;67 }
代碼君

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.