您的位置:首页 > 移动开发 > IOS开发

UVa 437 - The Tower of Babylon

2012-09-22 09:43 369 查看
本质上是最长上升序列,d[i]为前i个block所取得的最大高度,转移方程为 d[i] = max(d[i],d[j]+height[i]),( j < i )

reference to http://belbesy.wordpress.com/2011/06/26/uva-437-the-tower-of-babylon-dp-dag/

#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <string.h>
#include <cmath>
#include <queue>

using namespace std;

#define INF 1e9
#define NEGINF -1e9
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a>b?b:a)
const int MAXN = 100;

//typedef __int64 INT;

struct Node {
int x,y,z;

bool operator < (const Node &node) const {
if(x<=node.x && y<=node.y) return true;
else if( x<=node.x) return true;	/*不能完全覆盖则按x排序*/
else return false;
}

bool operator == (const Node &node) const {
if(x==node.x && y==node.y && z==node.z) return true;
else return false;
}
};

int n,m;

Node nodes[MAXN];
int d[MAXN];

int main(){
#ifndef ONLINE_JUDGE
freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
#endif

int tnum[3];
int i,j;
int cas=1;
while(scanf("%d",&n) && n ) {
m = 0;
while(n--) {		/*只需存入3组*/
scanf("%d %d %d",&tnum[0],&tnum[1],&tnum[2]);
sort(tnum,tnum+3);
nodes[m].x = tnum[0]; nodes[m].y = tnum[1]; nodes[m].z = tnum[2];
m++;
nodes[m].x = tnum[0]; nodes[m].y = tnum[2]; nodes[m].z = tnum[1];
m++;
nodes[m].x = tnum[1]; nodes[m].y = tnum[2]; nodes[m].z = tnum[0];
m++;
}
sort(nodes,nodes+m);
m = unique(nodes,nodes+m) - nodes;	/*去重*/
memset(d,0,sizeof(d));
int ans = 0;
for( i = 0; i < m; i++ ) {
d[i] = nodes[i].z;
for(j = 0; j < i; j++ )
if(nodes[j].x < nodes[i].x && nodes[j].y < nodes[i].y) {
d[i] = MAX(d[i],d[j]+nodes[i].z);
}
if(d[i] > ans)
ans = d[i];
}

printf("Case %d: maximum height = %d\n",cas++,ans);
}

return 0;
}

Input
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
20
10 19 18
18 19 22
23 33 34
19 21 22
32 32 31
10 90 10
10 80 10
22 22 29
29 28 27
26 25 24
19 80  1
22 21 31
29 28 55
58 42 39
48 78 32
2   2 90
3  99 33
54 44 44
57 13 33
10 29 80
5
1  1  1
1  2  1
1  3  1
1  4  1
1  5  1
5
2  3 100
3  4 200
4  6  50
6  8 100
5  5  75
1
80 90 100
6
15 19 3
44 33 21
88 33 57
31 29 20
99 88 1
52 26 5
2
100 100 100
102  98 100
10
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
8
10 16 1
5  8  2
20 32 2
10 16 2
16 10 2
32 20 2
8  5  2
16 10 1
0

Output:
Case 1: maximum height = 342
Case 2: maximum height = 588
Case 3: maximum height = 5
Case 4: maximum height = 481
Case 5: maximum height = 180
Case 6: maximum height = 273
Case 7: maximum height = 200
Case 8: maximum height = 310
Case 9: maximum height = 50
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息