您的位置:首页 > 其它

uvalive 6122 最长不降子序列

2015-08-15 18:55 375 查看
因为数据量非常小(n<=10),所以可以用dfs枚举每个长方体的状态,然后求LIS。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

const int N = 10;
int dp
;
int n, ans;

struct T
{
int a, b, c;
} t
;

struct X
{
int a, b;
} x
, tmp
;

bool cmp( X p, X q )
{
if ( p.a != q.a ) return p.a < q.a;
return p.b < q.b;
}

void dfs( int cur )
{
if ( cur == n )
{
for ( int i = 0; i < n; i++ )
{
tmp[i] = x[i];
if ( tmp[i].a > tmp[i].b )
{
swap( tmp[i].a, tmp[i].b );
}
}
sort( tmp, tmp + n, cmp );
for ( int i = 0; i < n; i++ )
{
dp[i] = 1;
for ( int j = 0; j < i; j++ )
{
if ( tmp[j].b <= tmp[i].b )
{
dp[i] = max( dp[i], dp[j] + 1 );
}
}
ans = max( ans, dp[i] );
}
return ;
}
x[cur].a = t[cur].a;
x[cur].b = t[cur].b;
dfs( cur + 1 );
x[cur].a = t[cur].a;
x[cur].b = t[cur].c;
dfs( cur + 1 );
x[cur].a = t[cur].b;
x[cur].b = t[cur].c;
dfs( cur + 1 );
}

int main ()
{
int _case = 1;
while ( scanf("%d", &n), n )
{
for ( int i = 0; i < n; i++ )
{
scanf("%d%d%d", &t[i].a, &t[i].b, &t[i].c);
}
ans = -999;
dfs(0);
printf("Case %d: %d\n", _case++, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: