UVa 437 (变形的LIS) The Tower of Babylon

题意:

有n种类型的长方体,每种长方体的个数都有无限个。当一个长方体的长和宽分别严格小于另一个长方体的长和宽的时候,才可以把这个放到第二个上面去。输出这n种长方体能组成的最大长度。

分析:

虽说每种都有无限个,可每种长方体一共的“姿态”最多也只有三种,将它们三个边长分别作为高。然后按照底面排序,就转化为最大上升子列的问题。

代码中采用了“人人为我”的方法。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 using namespace std;
 7
 8 const int maxn = 100;
 9 struct Cube
10 {
11     int x, y, z;
12     bool operator< (const Cube& a) const
13     { return x < a.x || (x == a.x && y < a.y); }
14 }cubes[maxn];
15 int size[3], dp[maxn];
16
17 bool check(int a, int b)
18 {
19     return (cubes[a].x < cubes[b].x && cubes[a].y < cubes[b].y);
20 }
21
22 int main(void)
23 {
24     #ifdef LOCAL
25         freopen("437in.txt", "r", stdin);
26     #endif
27
28     int n, kase = 0;
29     while(scanf("%d",&n) == 1 && n)
30     {
31         for(int i = 0; i < n; ++i)
32         {
33             for(int j = 0; j < 3; ++j)    scanf("%d", &size[j]);
34             sort(size, size + 3);
35             cubes[i*3].x = size[0], cubes[i*3].y = size[1], cubes[i*3].z = size[2];
36             cubes[i*3+1].x = size[0], cubes[i*3+1].y = size[2], cubes[i*3+1].z = size[1];
37             cubes[i*3+2].x = size[1], cubes[i*3+2].y = size[2], cubes[i*3+2].z = size[0];
38         }
39         n *= 3;
40         sort(cubes, cubes + n);
41         memset(dp, 0, sizeof(dp));
42         for(int i = 0; i < n; ++i)    dp[i] = cubes[i].z;
43         for(int i = 1; i < n; ++i)
44             for(int j = 0; j < i; ++j)
45                 if(check(j, i))    dp[i] = max(dp[i], cubes[i].z + dp[j]);
46         int ans = 0;
47         for(int i = 0; i < n; ++i)    ans = max(ans, dp[i]);
48         printf("Case %d: maximum height = %d\n", ++kase, ans);
49     }
50
51     return 0;
52 }

代码君

时间: 2024-12-14 01:56:09

UVa 437 (变形的LIS) The Tower of Babylon的相关文章

UVA 437 十九 The Tower of Babylon

The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 437 Appoint description:  System Crawler  (2015-08-29) Description Perhaps you have heard of the legend of the Tower of Babylon. No

uva 437 The Tower of Babylon(动态规划 嵌套矩形问题最长路)

有思路就去做,要相信自己 多处理更复杂的情况,你就不觉得现在复杂了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; struct soli { ll a,b,c; }s[40]; int n; ll d[40][3]; int vis[40][3]; ll answer[40][3]; ll dp(int s1,int s2

UVA 437 The Tower of Babylon DP

有向图DAG The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So n

UVa 437 The Tower of Babylon(动态规划)

传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n

UVa 437 The Tower of Babylon

Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: The babylonians had n typ

UVa 437 The Tower of Babylon(DP 最长条件子序列)

 题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于另一个时  这个长方体可以放在另一个上面  要求输出这样累积起来的最大高度 因为每个长方体都有3种放法  比较不好控制   可以把一个长宽高分成三个长方体  高度是固定的  这样就比较好控制了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 105 int x[max

uva 动态规划 437 The Tower of Babylon

The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in

UVA - 10534Wavio Sequence(LIS)

题目:UVA - 10534Wavio Sequence(LIS) 题目大意:给出N个数字,找出这样的序列:2 * n + 1个数字组成.前面的n + 1个数字单调递增,后面n + 1单调递减. 解题思路:从前往后找一遍LIS,再从后往前找一遍LIS.最后只要i这个位置的LIS的长度和LDS的长度取最小值.再*2 - 1就是这个波浪数字的长度.注意这里的求LIS要用nlog(n)的算法,而且这里的波浪数字的对称并不是要求i的LIS == LDS,而是只要求LIS和LDS最短的长度就行了,长的那个

UVA10599 - Robots(II)(变形的LIS)

题意:一个机器人在n * m的网格里面捡垃圾,机器人只能向右或向下走,求出能捡到的垃圾数量的最大值,有多少条路径可以达到最大值,以及输出其中一条路径. 思路:按照题意可以看出,因为机器人只能向右和向下走,所以纵坐标就不重要的,而横坐标是递增的.当将所有拥有垃圾的格子经过计算得到它的一维值(唯一的),得到一组的数组.那就可以转化为求最长上升子序列.但这个LIS的条件是mod(m)要大于前一个.计算数量时,当d[i] = d[j] + 1时,就相当于以i为结束时的最长上升子序列比以j结束时的最长上升