Monkey and Banana(LIS最长上升子序列)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/B

题意:

       有n种砖,每种砖有无数个,猴子要通过堆砖来吃到香蕉,下面的砖的长宽分别大于它上面的砖的长宽,问最多能有多高。

       案例:

       input

       1
       10 20 30 
       2 
       6 8 10 
       5 5 5 
       7 
       1 1 1 
       2 2 2 
       3 3 3 
       4 4 4 
       5 5 5 
       6 6 6 
       7 7 7 
       5 
       31 41 59 
       26 53 58 
       97 93 23 
       84 62 64 
       33 83 27 
       0 

       output

       Case 1: maximum height = 40
       Case 2: maximum height = 21 
       Case 3: maximum height = 28 
       Case 4: maximum height = 342
思路分析:
        每种砖的高度有三种可能,每次的长都大于宽,然后按从大到小的顺序排列,但要记住还有相等的情况,这个时候就要按宽来排。
        找出每块砖处于最上面时会有多高。最后找出最大高度。
源代码如下:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 using namespace std;
 5 struct node{
 6     int l,w,h;
 7 }s[100];
 8 bool cmp(const node a,const node b)
 9 {
10     return a.l>b.l;
11     if(a.l==b.l) return a.w>b.w;
12 }
13 int main()
14 {
15     int n,t=0,m,maxn,a,b,c,dp[100],i,j;
16     while(scanf("%d",&n)&&n)
17     {
18         t++;
19         m=0;
20         maxn=0;
21         for(i=0;i<n;i++)
22         {
23             scanf("%d%d%d",&a,&b,&c);
24             s[m].l=max(a,b);s[m].w=min(a,b);s[m].h=c;m++;             //每种砖的三种可能性
25             s[m].l=max(a,c);s[m].w=min(a,c);s[m].h=b;m++;
26             s[m].l=max(c,b);s[m].w=min(c,b);s[m].h=a;m++;
27         }
28         sort(s,s+m,cmp);
29         for(i=0;i<m;i++)
30         {
31             dp[i]=s[i].h;
32             for(j=0;j<i;j++)
33                 if((s[j].l>s[i].l&&s[j].w>s[i].w)&&dp[j]+s[i].h>dp[i])       
34                     dp[i]=dp[j]+s[i].h;                  //每种砖放在最顶上的最大高度
35                 if(dp[i]>maxn) maxn=dp[i];
36         }
37         printf("Case %d: maximum height = %d
",t,maxn);
38     }
39     return 0;
40 }
 
原文地址:https://www.cnblogs.com/q-c-y/p/4731134.html