Monkey and Banana

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

Description

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
 
 
 
题目大意:给你n种长方体木块,每种无限个,给你长宽高,木块可以翻转,即可以将不同的面作为底面,现在让你将这些木块摞起来,问你最高能摞多高。一个木块可以放在另一个木块上的条件是,上面的木块的长、宽小于下面木块的长、宽。
 
解题思路:开始写得很麻烦,还错了,然后参考了别人的思想,先将底面以长为条件排序,如果长度相同,按照宽从小到大排序。定义dp[i]表示,以i为底座时,这些木块最高可以摞多高。
 
  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<string.h>
  4. using namespace std;
  5. const int maxn = 1000;
  6. struct Cube{
  7. int a,b,h;
  8. }cube[maxn];
  9. int dp[maxn];
  10. bool cmp(Cube a,Cube b){
  11. if(a.a == b.a)
  12. return a.b < b.b;
  13. return a.a < b.a;
  14. }
  15. int main(){
  16. int n,cnt = 0;
  17. while(scanf("%d",&n)!=EOF&&n){
  18. memset(dp,0,sizeof(dp));
  19. for(int i = 1; i <= 3*n; i+=3){
  20. int aa,bb,cc;
  21. scanf("%d%d%d",&aa,&bb,&cc);
  22. for(int j = 0; j < 3; j++){
  23. if(j == 1)
  24. swap(aa,cc);
  25. if(j == 2)
  26. swap(bb,cc);
  27. cube[i+j].a = aa;
  28. cube[i+j].b = bb;
  29. if(cube[i+j].a > cube[i+j].b){
  30. swap(cube[i+j].a,cube[i+j].b);
  31. }
  32. cube[i+j].h = cc;
  33. }
  34. }
  35. sort(cube+1,cube+1+3*n,cmp);
  36. for(int i = 1; i <= 3*n;i ++){
  37. dp[i] = cube[i].h;
  38. }
  39. for(int i = 1; i <= 3*n; i++){
  40. for(int j = i+1; j <= 3*n; j++){
  41. if(cube[j].a > cube[i].a && cube[j].b > cube[i].b){
  42. dp[j] = max(dp[j],dp[i]+cube[j].h);
  43. }
  44. }
  45. }
  46. int ans = 0;
  47. for(int i = 1; i <= 3*n;i++)
  48. ans = max(ans,dp[i]);
  49. printf("Case %d: maximum height = %d\n",++cnt,ans);
  50. }
  51. return 0;
  52. }

  

 

HDU 1069—— Monkey and Banana——————【dp】的更多相关文章

  1. hdu 1069 Monkey and Banana 【动态规划】

    题目 题意:研究人员要测试猴子的IQ,将香蕉挂到一定高度,给猴子一些不同大小的箱子,箱子数量不限,让猩猩通过叠长方体来够到香蕉. 现在给你N种长方体, 要求:位于上面的长方体的长和宽  要小于  下面 ...

  2. HDU 1069 Monkey and Banana 基础DP

    题目链接:Monkey and Banana 大意:给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. ...

  3. HDU 1069 Monkey and Banana(DP——最大递减子序列)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 题意描述: 给n块砖,给出其长,宽和高 问将这n块砖,怎样叠放使得满足以下条件使得 ...

  4. HDU 1069 Monkey and Banana (dp)

    题目链接 Problem Description A group of researchers are designing an experiment to test the IQ of a monk ...

  5. HDU1069 - Monkey and Banana【dp】

    题目大意 给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子) 思路 首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放.. ...

  6. HDU 1069 Monkey and Banana ——(DP)

    简单DP. 题意:给出若干种长方体,如果摆放时一个长方体的长和宽小于另一个的长宽,那么它可以放在另一个的上面,问最高能放多少高度.每种长方体的个数都是无限的. 做法:因为每种个数都是无限,那么每种按照 ...

  7. HDU 1069 Monkey and Banana dp 题解

    HDU 1069 Monkey and Banana 纵有疾风起 题目大意 一堆科学家研究猩猩的智商,给他M种长方体,每种N个.然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉. 现在给你M种 ...

  8. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  9. HDU 1069 Monkey and Banana(转换成LIS,做法很值得学习)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

随机推荐

  1. C# Task的使用

    1.Task的使用 创建一个Task,有三种方式 //第一种 Task t1 = new Task(() => { Console.WriteLine(DateTime.Now.ToString ...

  2. C#多线程编程实战1.2暂停线程(休眠)

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  3. 十三、Node.js-fs模块(上)

    Node.js内置的fs模块就是文件系统模块,负责读写文件以及对文件进行相关操作. 下面直接可参考下面的代码进行fs模块里面基本方法的学习: /** * Created by Administrato ...

  4. javaee--学生成绩录入与显示--Struts2标签的使用

    类Score.java:各课程的成绩及平均成绩 类Student.java:学生姓名.学号及Score类 类ScoreAction.java:将Student类存在一个List对象中, execute ...

  5. centos7 docker 安装 zookeeper 3.4.13 集群

    假设三台主机的ip分别为: 主机一:192.168.0.168 主机二:192.168.0.169 主机三:192.168.0.170 三台主机的安装步骤相似,以主机一为例: 1. 查找zookeep ...

  6. django 部署到Ubuntu-apache2遇到的问题汇总

    1.x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Turns out ...

  7. SDUT OJ 数据结构上机测试1:顺序表的应用

    数据结构上机测试1:顺序表的应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  8. [USACO07DEC]泥水坑Mud Puddles BFS BZOJ 1627

    题目描述 Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the ...

  9. Qt 学习之路 2(64):使用 QJsonDocument 处理 JSON

    Home / Qt 学习之路 2 / Qt 学习之路 2(64):使用 QJsonDocument 处理 JSON Qt 学习之路 2(64):使用 QJsonDocument 处理 JSON  豆子 ...

  10. kotlin spring @value 注解

    spring boot和kotlin里静态类使用@Value注解配置解决方案前言spring boot里默认是不能给静态属性使用@Value赋值的.所以这里使用中间变量过渡绑定. 方案//applic ...