[DP] UVA437

来源:互联网 发布:拖动拼图验证码js插件 编辑:程序博客网 时间:2024/05/16 11:40

找到各个积木摞起来的最高高度

上面的积木长和宽严格小于下面的

同一个积木可以重复使用


好像一个积木用三次最多,网上也写的是三个放入vec,但没想为什么是那三个就六个都放进去了

依旧是代码一模一样系列

第一次用vector......用来存结构体很方便的样子

DP....感觉要总结一波,要不每次都没思路


先记一下LIS,每次都忘记hhh

int Longest_Increasing ( int num[], int n ) {        int lis[ n ], i, j;        for ( i = 0; i < n; i++ ) {                lis[ i ] = 1;                for ( j = 0; j < i; j++ )                        if ( num[ i ] > num[ j ] && lis[ j ] + 1 > lis[ i ] )                                lis[ i ] = lis[ j ] + 1;        }        int maxn = 0;        for ( i = 0; i < n; i++ )                if ( maxn < lis[ i ] )                        maxn = lis[ i ];        return maxn;}


UVA 437

#include <algorithm>#include <cstdio>#include <iostream>#include <vector>using namespace std;struct Node {        int x, y, z; //长,宽,高        Node ( int a, int b, int c )            : x ( a )            , y ( b )            , z ( c ) {}        bool operator< ( const Node &n ) {                return ( x < n.x && y < n.y ) || ( x < n.y && y < n.x ); // vec[ j ] < vec [ i ]        }};vector<Node> vec;int DP[ 300 ];bool cmp ( Node &a, Node &b ) { return ( a.x * a.y < b.x * b.y ); } //将所有节点从小到大排序int LIS ( int n ) {        int mx = 0; //储存最大高度        for ( int i = 1; i <= 6 * n; i++ ) {                DP[ i ] = vec[ i ].z; //初始化高度                for ( int j = 1; j <= i; j++ ) {                        if ( vec[ j ] < vec[ i ] )                                DP[ i ] =                                    max ( DP[ i ], DP[ j ] + vec[ i ].z ); // LIS方法找到最大高度                        if ( DP[ i ] > mx )                                mx = DP[ i ];                }        }        return mx;}int main () {        int cnt = 1;        int n;        while ( cin >> n && n != 0 ) {                vec.clear (); //*清空*                for ( int i = 1; i <= n; i++ ) {                        int a, b, c;                        scanf ( "%d%d%d", &a, &b, &c );                        vec.push_back ( Node ( a, b, c ) ); //六种情况放入vector                        vec.push_back ( Node ( a, c, b ) );                        vec.push_back ( Node ( b, a, c ) );                        vec.push_back ( Node ( b, c, a ) );                        vec.push_back ( Node ( c, a, b ) );                        vec.push_back ( Node ( c, b, a ) );                }                sort ( vec.begin (), vec.end (), cmp );                printf ( "Case %d: maximum height = %d\n", cnt, LIS ( n ) );                cnt++;        }}