您的位置:首页 > 其它

zoj1093 Monkey and Banana(DP)

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

View Code

#include <iostream>
#include <cstdlib>

using namespace std;

int P[ 6 ][ 3 ] = {
0,1,2,0,2,1,
1,0,2,1,2,0,
2,0,1,2,1,0};

struct node{
int D[ 3 ];
}N[ 31 ],S[ 185 ];

int DP[ 185 ];

int cmp( const void* a, const void* b )
{
node *p = (node *)a;
node *q = (node *)b;
if ( p->D[ 0 ] == q->D[ 0 ] )
return q->D[ 1 ] - p->D[ 1 ];
return q->D[ 0 ] - p->D[ 0 ];
}

int main()
{
int n,t = 1;
while ( cin >> n && n ) {
for ( int i = 0 ; i < n ; ++ i )
for ( int j = 0 ; j < 3 ; ++ j )
cin >> N[ i ].D[ j ];
int count = 0;
for ( int i = 0 ; i < n ; ++ i )
for ( int j = 0 ; j < 6 ; ++ j ) {
S[ count ].D[ 0 ] = N[ i ].D[ P[ j ][ 0 ] ];
S[ count ].D[ 1 ] = N[ i ].D[ P[ j ][ 1 ] ];
S[ count ].D[ 2 ] = N[ i ].D[ P[ j ][ 2 ] ];
++ count;
}

qsort( S, count, sizeof( node ), cmp );
/* 貌似不能用单调队列了,╮(╯▽╰)╭,长度不是 1 */
for ( int i = 0 ; i < count ; ++ i ) {
DP[ i ] = S[ i ].D[ 2 ];
for ( int j = 0 ; j < i ; ++ j )
if ( DP[ j ] + S[ i ].D[ 2 ] > DP[ i ]
&& S[ i ].D[ 1 ] < S[ j ].D[ 1 ]
&& S[ i ].D[ 0 ] < S[ j ].D[ 0 ] )
DP[ i ] = DP[ j ] + S[ i ].D[ 2 ];
}
int max = 0;
for ( int i = 0 ; i < count ; ++ i )
if ( DP[ i ] > max )
max = DP[ i ];
cout << "Case " << t ++ << ": maximum height = " << max << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: