NYOJ-232 How to eat more Banana

How to eat more Banana

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
 
描述
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.
 
输入
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.
输出
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".
样例输入
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
样例输出
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
来源
Trinity
 
题意:
  给定n种三个边长不完全相同的长方体,每种长方体的数量无限多,长方体的每个面都可以做底,现要求将这些长方体一个一个摞起来,一个长方体可以摞在另一个长方体上边,当且仅当上边长方体的长和宽必须严格大于下边长方体的长和宽,问在满足要求的情况下最多可以摞起来的高度是多少?
题解:
  将给定的一个长宽高的长方体,将每种可能的方法分别划分为不同的长方体(即按照规定的长宽高放),然后按照长宽从大到小排序,则最后的结果就是LIS的算法了。
 
 
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 struct node 
 9 {
10     int l;
11     int w;
12     int h;
13 }a[100];
14 int dp[100];
15 
16 int max(int m, int n)
17 {
18     return m > n ? m : n;
19 }
20 
21 int min(int m, int n)
22 {
23     return m < n ? m : n;
24 }
25 
26 bool cmp(node p, node q)
27 {
28     return (p.l > q.l || p.l == q.l && p.w > q.w);
29 }
30 
31 int main()
32 {
33     int n, cnt, x, y, z, max_h;
34     int T = 1;
35     while(scanf("%d", &n), n)
36     {
37         cnt = 0;
38         memset(dp, 0, sizeof(dp));
39         while(n--)
40         {
41             scanf("%d%d%d", &x, &y, &z);
42             
43             a[cnt].l = max(x, y);
44             a[cnt].w = min(x, y);
45             a[cnt++].h = z;
46            
47             a[cnt].l = max(x, z);
48             a[cnt].w = min(x, z);
49             a[cnt++].h = y;
50             
51             a[cnt].l = max(z, y);
52             a[cnt].w = min(z, y);
53             a[cnt++].h = x;
54         }
55         
56         sort(a, a+cnt, cmp);
57 
58         for(int k = 0; k < cnt; ++k)  // 注意别忘了这里 dp 的初始化,让我调试了半天
59             dp[k] = a[k].h;
60 
61         max_h = dp[0];
62         for(int i = 1; i < cnt; ++i)
63         {
64             for(int j = 0; j < i; ++j)
65             {
66                 if(a[j].l > a[i].l && a[j].w > a[i].w && dp[j]+a[i].h>dp[i])
67                     dp[i] = dp[j]+a[i].h;
68             }
69             if(max_h < dp[i])
70                 max_h = dp[i];
71         }
72         printf("Case %d: maximum height = %d\n", T++, max_h);
73     }
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/dongsheng/archive/2013/05/25/3098988.html

孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值