_xiaobai_

导航

zoj1093 Monkey and Banana(DP)

/*
 经典模型:最大不下降子序列,将每个箱子旋转三次即可,用贪心可证,长边对齐原则
*/

View Code
 1 #include <iostream>
2 #include <cstdlib>
3
4 using namespace std;
5
6 int P[ 6 ][ 3 ] = {
7 0,1,2,0,2,1,
8 1,0,2,1,2,0,
9 2,0,1,2,1,0};
10
11 struct node{
12 int D[ 3 ];
13 }N[ 31 ],S[ 185 ];
14
15 int DP[ 185 ];
16
17 int cmp( const void* a, const void* b )
18 {
19 node *p = (node *)a;
20 node *q = (node *)b;
21 if ( p->D[ 0 ] == q->D[ 0 ] )
22 return q->D[ 1 ] - p->D[ 1 ];
23 return q->D[ 0 ] - p->D[ 0 ];
24 }
25
26 int main()
27 {
28 int n,t = 1;
29 while ( cin >> n && n ) {
30 for ( int i = 0 ; i < n ; ++ i )
31 for ( int j = 0 ; j < 3 ; ++ j )
32 cin >> N[ i ].D[ j ];
33 int count = 0;
34 for ( int i = 0 ; i < n ; ++ i )
35 for ( int j = 0 ; j < 6 ; ++ j ) {
36 S[ count ].D[ 0 ] = N[ i ].D[ P[ j ][ 0 ] ];
37 S[ count ].D[ 1 ] = N[ i ].D[ P[ j ][ 1 ] ];
38 S[ count ].D[ 2 ] = N[ i ].D[ P[ j ][ 2 ] ];
39 ++ count;
40 }
41
42 qsort( S, count, sizeof( node ), cmp );
43 /* 貌似不能用单调队列了,╮(╯▽╰)╭,长度不是 1 */
44 for ( int i = 0 ; i < count ; ++ i ) {
45 DP[ i ] = S[ i ].D[ 2 ];
46 for ( int j = 0 ; j < i ; ++ j )
47 if ( DP[ j ] + S[ i ].D[ 2 ] > DP[ i ]
48 && S[ i ].D[ 1 ] < S[ j ].D[ 1 ]
49 && S[ i ].D[ 0 ] < S[ j ].D[ 0 ] )
50 DP[ i ] = DP[ j ] + S[ i ].D[ 2 ];
51 }
52 int max = 0;
53 for ( int i = 0 ; i < count ; ++ i )
54 if ( DP[ i ] > max )
55 max = DP[ i ];
56 cout << "Case " << t ++ << ": maximum height = " << max << endl;
57 }
58 return 0;
59 }

posted on 2011-08-18 02:11  _xiaobai_  阅读(190)  评论(0编辑  收藏  举报