Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。
 
研究人员有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 
 
经典的动态规划题,这个其实可以看作LIS问题来看待,其实这道题虽然说有无限个砖块,可是他每种砖块转换成6种状态之后,每种状态只可能出现一次,因为还有长和宽的限制,因此可以把所有的砖块做成一个排列。还可以由LIS问题得到启发,得到这个问题的最优子结构,用h[i]表示以第i个砖块作为最上面一个砖块可以得到的最大距离,为了保证其最优性,状态转移方程为h[i]=max{h[j]+第i个砖块的高度,j<i},这个方程是因为事先经过排序,使得面积从大到小排,因此序号大于i的砖块不可能放在砖块i的下面,然后枚举n个砖块放在最上面,其中所能得到的最高高度就是答案
 

#include"iostream"
#include"algorithm"
#include"cstring"
#include"cstdio"
#define inf -1e9
using namespace std;
int n,ans,f,ff; struct node{
int x,y,z;
void getdata(int a,int b,int c)
{
x=a;y=b;z=c;
}
bool operator < (const node &a1) const
{
if(x!=a1.x) return x>a1.x;
return y>a1.y;
}
}a[]; int dp[]; void Init()
{
f=;int aa,bb,cc;
a[].x=a[].y=;
for(int i=;i<n;i++)
{
cin>>aa>>bb>>cc;
a[f++].getdata(aa,bb,cc);
a[f++].getdata(aa,cc,bb);
a[f++].getdata(cc,aa,bb);
a[f++].getdata(cc,bb,aa);
a[f++].getdata(bb,aa,cc);
a[f++].getdata(bb,cc,aa);
}
sort(a,a+f);
} bool isok(node a1,node a2)
{
if(a2.x>a1.x&&a2.y>a1.y) return true;
return false;
} void Work()
{
int MAX=inf;
memset(dp,,sizeof(int)*f);
for(int i=;i<f;i++)
{
for(int j=;j<=i;j++)
if(isok(a[i],a[j])&&dp[j]+a[i].z>dp[i])
dp[i]=dp[j]+a[i].z;
MAX=max(dp[i],MAX);
}
ans=MAX;
} void Print()
{
cout<<"Case "<<ff++<<": maximum height = "<<ans<<endl;
} int main()
{
ff=;
while(cin>>n&&n)
{
Init();
Work();
Print();
}
return ;
}

O(O_O)O


 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=+;
struct node
{
int l,w,h;
void init(int a,int b,int c)
{
l=a;
w=b;
h=c;
}
}f[maxn];
int dp[maxn],e,ca=; bool cmp(node x1,node x2)
{
return x1.l*x1.w>x2.l*x2.w;
} void Work()
{
sort(f,f+e,cmp);
int maxx=-1e9;
for(int k=;k<e;k++)
{
memset(dp,,sizeof(dp));
dp[k]=f[k].h;
for(int i=;i<e;i++)
{
if(i==k) continue;
for(int j=;j<i;j++)
{
if(f[j].l>f[i].l&&f[j].w>f[i].w) dp[i]=max(dp[j]+f[i].h,dp[i]);
}
maxx=max(dp[i],maxx);
}
}
cout<<"Case "<<ca++<<": maximum height = "<<maxx<<endl;
} int main()
{
int n;
while(cin>>n&&n)
{
e=;
int a,b,c;
for(int i=;i<=n;i++)
{
cin>>a>>b>>c;
f[e++].init(a,b,c);
f[e++].init(a,c,b);
f[e++].init(b,a,c);
f[e++].init(b,c,a);
f[e++].init(c,a,b);
f[e++].init(c,b,a);
}
Work();
}
return ;
}


集训第五周 动态规划 B题LIS的更多相关文章

  1. 集训第五周动态规划 E题 LIS

    Description The world financial crisis is quite a subject. Some people are more relaxed while others ...

  2. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  3. 集训第五周动态规划 C题 编辑距离

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  4. 集训第五周动态规划 I题 记忆化搜索

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  5. 集训第五周动态规划 H题 回文串统计

    Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...

  6. 集训第五周动态规划 G题 回文串

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  7. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  8. 集训第五周 动态规划 K题 背包

    K - 背包 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. 集训第五周动态规划 J题 括号匹配

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

随机推荐

  1. 2018SCin tsyzDay1 模拟赛-模拟

    预计得分:70+0+0+100+100+100+100=470 实际得分:70+0+0+30+100+0+40=240 第一天就被模拟虐爆qwq T1 https://www.luogu.org/pr ...

  2. 《windows核心编程系列》三谈谈内核对象及句柄的本质

    内核对象 本章讨论的是相对抽象的概念,不涉及任何具体的内核对象的细节而是讨论所有内核对象的共有特性. 首先让我们来了解一下什么是内核对象.内核对象通过API来创建,每个内核对象是一个数据结构,它对应一 ...

  3. Hdu 5416 CRB and Tree (bfs)

    题目链接: Hdu 5416 CRB and Tree 题目描述: 给一棵树有n个节点,树上的每条边都有一个权值.f(u,v)代表从u到v路径上所有边权的异或值,问满足f(u,v)==m的(u, v) ...

  4. 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...

  5. 字符串处理 BestCoder Round #43 1001 pog loves szh I

    题目传送门 /* 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 */ #include <cstdio> #incl ...

  6. SpringCloud开发学习总结(四)—— 客户端负载均衡Ribbon

    通过上一章<SpringCloud开发学习总结(三)—— 服务治理Eureka>,我们已经搭建起微服务架构中的核心组件——服务注册中心(包括单点模式和高可用模式).同时还注册了一个服务,命 ...

  7. sed练习简记

    1. 使用多命令选项-e sed -e 'command1' -e 'command2' -e 'command3' 在/etc/passwd文件中搜索root.nobody或mail [root@s ...

  8. 里氏替换原则中is和as分别的作用

    is 是用于检查对象是否指定类型兼容 if(empls[i] is SE){ ((SE)empls).SayHi(); } as 不用强转可以直接转换 if(empls[i] is SE){ SE s ...

  9. 聊聊mq的使用场景

    mq的作用 通过异步方式对系统解耦 增加系统的并发处理能力 通过异步方式对系统解耦 以用户注册为例,一般情况下: 分下一下,上面过程存在的一些问题: 注册过程会调用4个服务(注册服务.邮件服务.短信服 ...

  10. java 线程池第一篇 之 ThreadPoolExcutor

    一:什么是线程池? java 线程池是将大量的线程集中管理的类,包括对线程的创建,资源的管理,线程生命周期的管理.当系统中存在大量的异步任务的时候就考虑使用java线程池管理所有的线程.减少系统资源的 ...