uvalive 6122 最长不降子序列

因为数据量非常小(n<=10),所以可以用dfs枚举每个长方体的状态,然后求LIS。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 
 7 const int N = 10;
 8 int dp[N];
 9 int n, ans;
10 
11 struct T
12 {
13     int a, b, c;
14 } t[N];
15 
16 struct X
17 {
18     int a, b;
19 } x[N], tmp[N];
20 
21 bool cmp( X p, X q )
22 {
23     if ( p.a != q.a ) return p.a < q.a;
24     return p.b < q.b;
25 }
26 
27 void dfs( int cur )
28 {
29     if ( cur == n )
30     {
31         for ( int i = 0; i < n; i++ )
32         {
33             tmp[i] = x[i];
34             if ( tmp[i].a > tmp[i].b )
35             {
36                 swap( tmp[i].a, tmp[i].b );
37             }
38         }
39         sort( tmp, tmp + n, cmp );
40         for ( int i = 0; i < n; i++ )
41         {
42             dp[i] = 1;
43             for ( int j = 0; j < i; j++ )
44             {
45                 if ( tmp[j].b <= tmp[i].b )
46                 {
47                     dp[i] = max( dp[i], dp[j] + 1 );
48                 }
49             }
50             ans = max( ans, dp[i] );
51         }
52         return ;
53     }
54     x[cur].a = t[cur].a;
55     x[cur].b = t[cur].b;
56     dfs( cur + 1 );
57     x[cur].a = t[cur].a;
58     x[cur].b = t[cur].c;
59     dfs( cur + 1 );
60     x[cur].a = t[cur].b;
61     x[cur].b = t[cur].c;
62     dfs( cur + 1 );
63 }
64 
65 int main ()
66 {
67     int _case = 1;
68     while ( scanf("%d", &n), n )
69     {
70         for ( int i = 0; i < n; i++ )
71         {
72             scanf("%d%d%d", &t[i].a, &t[i].b, &t[i].c);
73         }
74         ans = -999;
75         dfs(0);
76         printf("Case %d: %d\n", _case++, ans);
77     }
78     return 0;
79 }

 

转载于:https://www.cnblogs.com/huoxiayu/p/4732934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值