垒石头(排序+dp)

HDU 1069

一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。
 
研究人员有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。
 
在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。
 
你的任务是编写一个程序,计算猴子们最高可以堆出的砖块们的高度。

Input

输入文件包含多组测试数据。
每个测试用例的第一行包含一个整数n,代表不同种类的砖块数目。n<=30.
接下来n行,每行3个数,分别表示砖块的长宽高。
当n= 0的时候,无需输出任何答案,测试结束。

Output对于每组测试数据,输出最大高度。格式:Case 第几组数据: maximum height = 最大高度Sample Input

1
10 20 30 

6 8 10 
5 5 5 

1 1 1 
2 2 2 
3 3 3 
4 4 4 
5 5 5 
6 6 6 
7 7 7 

31 41 59 
26 53 58 
97 93 23 
84 62 64 
33 83 27 

Sample Output

Case 1: maximum height = 40
Case 2: maximum height = 21 
Case 3: maximum height = 28 
Case 4: maximum height = 342 
题目大意:
题意:给出一些长方体,然后让你把他堆成塔,
            要求下面的塔要严格大于上面的塔(长和宽),
            而且每种长方体是无限的。
这个题有连个限制条件,你要先按照一个拍好序
bool cmp(node a,node b){  
    if (a.x==b.x)  
        return a.y>b.y;  
    return a.x>b.x;  
}  

然后你就枚举一个i,向前遍历符合条件就跟新结果就行

for(int i=1;i<=cnt;i++){
        dp[i]=a[i].h;
       for(int j=i-1;j>=1;j--){  
            if(a[i].x<a[j].x&&a[i].y<a[j].y){
                dp[i]=max(dp[i],dp[j] + a[i].h);
            }
        }  
        ans=max(ans,dp[i]);
    }

 还有一点,就是这个长方形的长宽高可以换的

void PushUp(int t1,int t2,int t3)  
{  
    a[ant].h = t3;  
    a[ant].x = t1;  
    a[ant].y = t2;  
    ant++;  
    a[ant].h = t3;  
    a[ant].x = t2;  
    a[ant].y = t1;  
    ant++;  
    a[ant].h = t2;  
    a[ant].x = t1;  
    a[ant].y = t3;  
    ant++;  
    a[ant].h = t2;  
    a[ant].x = t3;  
    a[ant].y = t1;  
    ant++;  
    a[ant].h = t1;  
    a[ant].x = t2;  
    a[ant].y = t3;  
    ant++;  
    a[ant].h = t1;  
    a[ant].x = t3;  
    a[ant].y = t2;  
    ant++;  
}  

 


#include <stdio.h>  
#include <cstring>  
#include <algorithm>  
/*
Monkey and Banana
*/
using namespace std;  
#define CLR(a,b) memset(a,b,sizeof(a))  
#define INF 0x3f3f3f3f  
#define LL long long  
struct node  
{  
    int x,y,h;      //让x >= y   
}a[200];  
int ant;  
bool cmp(node a,node b)  
{  
    if (a.x == b.x)  
        return a.y > b.y;  
    return a.x > b.x;  
}  
void PushUp(int t1,int t2,int t3)  
{  
    a[ant].h = t3;  
    a[ant].x = t1;  
    a[ant].y = t2;  
    ant++;  
    a[ant].h = t3;  
    a[ant].x = t2;  
    a[ant].y = t1;  
    ant++;  
    a[ant].h = t2;  
    a[ant].x = t1;  
    a[ant].y = t3;  
    ant++;  
    a[ant].h = t2;  
    a[ant].x = t3;  
    a[ant].y = t1;  
    ant++;  
    a[ant].h = t1;  
    a[ant].x = t2;  
    a[ant].y = t3;  
    ant++;  
    a[ant].h = t1;  
    a[ant].x = t3;  
    a[ant].y = t2;  
    ant++;  
}  
int main()  
{  
    int n;  
    int ans;  
    int dp[200];  
    int Case = 1;  
    while (~scanf ("%d",&n) && n)  
    {  
        ant = 1;  
        for (int i = 1 ; i <= n ; i++)  
        {  
            int t1,t2,t3;  
            scanf ("%d %d %d",&t1,&t2,&t3);  
            PushUp(t1,t2,t3);  
        }  
        ant--;  
        printf ("Case %d: maximum height = ",Case++);  
        sort (a+1 , a+1+ant , cmp);  
        ans = 0;  
        for (int i = 1 ; i <= ant ; i++)  
        {  
            dp[i] = a[i].h;  
            for (int j = i - 1 ; j >= 1 ; j--)  
            {  
                if (a[i].x < a[j].x && a[i].y < a[j].y)  
                    dp[i] = max (dp[i] , dp[j] + a[i].h);  
            }  
            ans = max (ans , dp[i]);  
        }  
        printf ("%d\n",ans);  
    }  
    return 0;  
}  

LDU中的题:

 Problem 4590 


#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
struct node{
    int x,y,h;
}a[maxn];
int dp[maxn];
int cnt=0;
bool cmp(node a,node b){  
    if (a.x==b.x)  
        return a.y>b.y;  
    return a.x>b.x;  
}  
void push(int x1,int y1,int h1){
    a[++cnt].h=x1;
    a[cnt].x=y1;
    a[cnt].y=h1;
    
    a[++cnt].h=x1;
    a[cnt].x=h1;
    a[cnt].y=y1;
    
    a[++cnt].h=y1;
    a[cnt].x=x1;
    a[cnt].y=h1;
    
    a[++cnt].h=y1;
    a[cnt].x=h1;
    a[cnt].y=x1;
    
    a[++cnt].h=h1;
    a[cnt].x=x1;
    a[cnt].y=y1;
    
    a[++cnt].h=h1;
    a[cnt].x=y1;
    a[cnt].y=x1;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int x1,y1,h1;
        cin>>x1>>y1>>h1;
        push(x1,y1,h1);
    }
    sort(a+1,a+cnt+1,cmp); 
    int ans=0;
    for(int i=1;i<=cnt;i++){
        dp[i]=a[i].h;
        for(int j=i-1;j>=1;j--){  
            if(a[i].x<a[j].x&&a[i].y<a[j].y){
                dp[i]=max(dp[i],dp[j] + a[i].h);
            }
        }  
        ans=max(ans,dp[i]);
    }
    cout<<ans<<endl;
}

 


posted @ 2021-01-28 11:10  哎呦哎(iui)  阅读(148)  评论(0编辑  收藏  举报