Monkey and Banana

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.

Input

The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".

Sample 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

Sample Output

Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

代码

//#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<climits>
#include<queue>
#include<set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define swap(x,y) (x^=y,y^=x,x^=y)
const int N=50;
typedef long long ll;
const int INF=0x3f3f3f3f;
using namespace std;
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=-1;
    for(; c>='0'&&c<='9'; c=getchar()) x=(x<<1)+(x<<3)+(c&15);
    x*=f;
}
template<typename T>inline void print(T x)
{
    if(x<0) putchar('-'),x*=-1;
    if(x>=10) print(x/10);
    putchar(x%10+'0');
}
struct node
{
    ll a,b,c;
}num[N*6];
bool cmp(node a,node b)
{
    if(a.a==b.a)
        return a.b<b.b;
    else
    return a.a<b.a;
}
ll dp[N*6];
int main()
{
    int t;
    int cas=0;
    while(scanf("%d",&t))
    {
        if(t==0)
            break;
        cas++;
        memset(dp,0,sizeof(dp));
        int c=0;
        ll q,w,e;
        for(int i=0;i<t;i++)
        {
            read(q);read(w);read(e);
            num[c++]=(node){q,w,e};
            num[c++]=(node){q,e,w};
            num[c++]=(node){w,e,q};
            num[c++]=(node){w,q,e};
            num[c++]=(node){e,q,w};
            num[c++]=(node){e,w,q};
        }
        sort(num,num+c,cmp);
        dp[0]=num[0].c;
        int maxn;
        for(int i=1;i<c;++i)
        {
            maxn=0;
            for(int  j=0;j<i;++j )
            {
                if( num[j].a<num[i].a && num[j].b<num[i].b )
                    maxn=maxn>dp[j]?maxn:dp[j];

            }

            dp[i]=num[i].c+maxn;

        }

        ll ans=-INF;
        for(int i=0;i<c;i++)
            ans=max(ans,dp[i]);
        printf("Case %d: maximum height = %lld
",cas,ans);
    }
    return 0;
}

另一种格式

//#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<climits>
#include<queue>
#include<set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define swap(x,y) (x^=y,y^=x,x^=y)
const int N=50;
typedef long long ll;
const int INF=0x3f3f3f3f;
using namespace std;
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=-1;
    for(; c>='0'&&c<='9'; c=getchar()) x=(x<<1)+(x<<3)+(c&15);
    x*=f;
}
template<typename T>inline void print(T x)
{
    if(x<0) putchar('-'),x*=-1;
    if(x>=10) print(x/10);
    putchar(x%10+'0');
}
struct node
{
    ll a,b,c;
}num[N*6];
bool cmp(node a,node b)
{
    if(a.a==b.a)
        return a.b>b.b;
    return a.a>b.a;
}
ll dp[N*6];
int main()
{
    int t;
    int cas=0;
    while(scanf("%d",&t))
    {
        cas++;
        if(t==0)
            break;
        memset(dp,0,sizeof(dp));
        int c=0;
        ll q,w,e;
        for(int i=0;i<t;i++)
        {
            read(q);read(w);read(e);
            num[c++]=(node){q,w,e};
            num[c++]=(node){q,e,w};
            num[c++]=(node){w,e,q};
            num[c++]=(node){w,q,e};
            num[c++]=(node){e,q,w};
            num[c++]=(node){e,w,q};
        }
        sort(num,num+c,cmp);
        int maxn;
        for(int i=1;i<c;i++)
        {
            ll po=num[i].c;
            dp[i]=po;     //不能少。
            for(int j=i-1;j>=0;j--)
            {
                if(num[j].a>num[i].a&&num[j].b>num[i].b)
                    dp[i]=max(dp[i],po+dp[j]);

            }
        }
        ll ans=-INF;
        for(int i=0;i<c;i++)
            ans=max(ans,dp[i]);
        printf("Case %d: maximum height = %lld
",cas,ans);
    }
    return 0;
}

思路

最长的满足两个条件的绝对递增序列.

ps

wa了4次的地方竟然是在循环里少了dp[i]=po; 注意下dp的初值对应着数组的值。

原文地址:https://www.cnblogs.com/xxffxx/p/11829595.html