專題三--1005

題目

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

思路

給出多種木塊,每種木塊有無數多個,求木塊可壘成的最大高度。
每種木塊有三種擺放方式,當一個木塊有兩個邊都大于另一個木塊時,兩木塊相容。
將每個木塊分解為三個木塊之后(三種擺放方式),就形成了一個類似于DAG上的最長路的問題(兩個木塊之間是二元關系且不會形成環)。使用動態規劃來來考慮的話,狀態轉移方程是

dp[i]=max{dp[j]+b[i].height,j為與i塊木塊相容的木塊的序號}

需要注意的幾點是:

  1. dp之前要先對木塊進行排序,確保在進行外層的遍歷時,與i相容的木塊都在i之前,i之后不會出現于i相容的木塊
  2. 注意木塊相容的判斷條件,只要兩條邊都大即可,不必嚴格地寬大于寬,長大于長。

AC代碼

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. using namespace std;
  5. struct block{
  6. int c;
  7. int k;
  8. int g;
  9. };
  10. bool cmp(block a,block b){
  11. if((a.c*a.k)<(b.c*b.k)){
  12. return 1;
  13. }
  14. else return 0;
  15. }
  16. int MAX(int a,int b){
  17. return a>b?a:b;
  18. }
  19. int main(){
  20. int n;
  21. struct block tem;
  22. struct block a[300];
  23. int dp[300];
  24. int flag=0;
  25. //freopen("date.in","r",stdin);
  26. //freopen("date.out","w",stdout);
  27. while(cin>>n&&n!=0){
  28. flag++;
  29. for(int i=1;i<=n;i++){
  30. cin>>tem.c>>tem.k>>tem.g;
  31. a[i*3].c=tem.c;a[i*3].k=tem.k;a[i*3].g=tem.g;
  32. //a[i*6-1].c=tem.c;a[i*6-1].k=tem.g;a[i*6-1].g=tem.k;
  33. a[i*3-1].c=tem.k;a[i*3-1].k=tem.g;a[i*3-1].g=tem.c;
  34. //a[i*6-3].c=tem.k;a[i*6-3].k=tem.c;a[i*6-3].g=tem.g;
  35. a[i*3-2].c=tem.g;a[i*3-2].k=tem.c;a[i*3-2].g=tem.k;
  36. //a[i*6-5].c=tem.g;a[i*6-5].k=tem.k;a[i*6-5].g=tem.c;
  37. }
  38. sort(a+1,a+3*n+1,cmp);
  39. /*for(int i=1;i<=3*n;i++){
  40. cout<<a[i].c<<"\t"<<a[i].k<<"\t"<<a[i].g<<"\t"<<endl;
  41. }*/
  42. /*for(int i=1;i<=n;i++)
  43. dp[i]=a[i].g;*/
  44. for(int i=1;i<=3*n;i++){
  45. dp[i]=a[i].g;
  46. for(int j=i-1;j>=1;j--){
  47. if(a[i].c>a[j].c&&a[i].k>a[j].k||a[i].c>a[j].k&&a[i].k>a[j].c)
  48. dp[i]=MAX(dp[j]+a[i].g,dp[i]);
  49. }
  50. }
  51. sort(dp+1,dp+3*n+1);
  52. cout<<"Case "<<flag<<": maximum height = "<<dp[3*n]<<endl;
  53. }
  54. }


來自為知筆記(Wiz)


轉載于:https://www.cnblogs.com/liuzhanshan/p/5544418.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/287506.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287506.shtml
英文地址,請注明出處:http://en.pswp.cn/news/287506.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

shell中的數字

shell中的數字 author :headsen chen date :2017-10-18 15:01:42 個人原創&#xff0c;轉載請注明作者&#xff0c;出處&#xff0c;否則依法追究法律責任 1,生成隨機數&#xff08;范圍&#xff1a;0-32767&#xff09;&#xff0c;用特殊變量&#xff1a;RANDOM 2&#xff…

serviceloader java_【java編程】ServiceLoader使用看這一篇就夠了

轉載:https://www.jianshu.com/p/7601ba434ff4想必大家多多少少聽過spi&#xff0c;具體的解釋我就不多說了。但是它具體是怎么實現的呢&#xff1f;它的原理是什么呢&#xff1f;下面我就圍繞這兩個問題來解釋&#xff1a;實現: 其實具體的實現類就是java.util.ServiceLoader…

.NET7 Preview4 之OpenAPI swagger改進

在MiniAPI系列中&#xff0c;《.NET6之MiniAPI(十八)&#xff1a;OpenAPI swagger》介紹了swagger在MiniAPI框架中的使用&#xff0c;當時留下很多不足&#xff0c;隨著.NET7 Preview4的推出&#xff0c;這方面得到了很大的改進&#xff0c;我還是使用“十八”這篇文章的案例。…

Swift - 自定義單元格實現微信聊天界面

1&#xff0c;下面是一個放微信聊天界面的消息展示列表&#xff0c;實現的功能有&#xff1a; &#xff08;1&#xff09;消息可以是文本消息也可以是圖片消息&#xff08;2&#xff09;消息背景為氣泡狀圖片&#xff0c;同時消息氣泡可根據內容自適應大小&#xff08;3&#x…

[python opencv 計算機視覺零基礎到實戰] 十三 直方圖顏色提鮮

一、學習目標 了解了均衡化的作用是什么了解灰度、YUV、彩色圖片均衡化的方法是使用什么方法了解了合并通道的方法是什么了解了分離通道的方法是什么 如有錯誤歡迎指出~ 二、了解圖像均衡化 2.1 了解直方圖均衡化 圖像直方圖均衡化主要是對圖像中的少數灰度進行壓縮&#…

java 中字符串比較方法_java中常用的字符串的比較方法(兩種)

比較字符串比較常用的兩個方法是運算符“”和String的equals方法。使用“”比較兩個字符串&#xff0c;是比較兩個對象的的“地址”是否一致&#xff0c;本質就是判斷兩個變量是否指向同一個對象&#xff0c;如果是則返回true&#xff0c;否則返回的是false。而String類的equal…

Android之稍微靠譜點的透明Activity(不獲取觸摸事件)

1 問題 實現透明的Activity(不獲取觸摸事件),就行什么也看不到,打開了透明activity,也不影響其他頁面的滑動和點擊,就行什么事情都沒發生一樣。 2 代碼實現 1)配置樣式 <style name="TestTheme" parent="Theme.AppCompat.Light"><item na…

分布式服務框架dubbo原理解析 轉

alibaba有好幾個分布式框架&#xff0c;主要有&#xff1a;進行遠程調用(類似于RMI的這種遠程調用)的(dubbo、hsf)&#xff0c;jms消息服務(napoli、notify)&#xff0c;KV數據庫(tair)等。這個框架/工具/產品在實現的時候&#xff0c;都考慮到了容災&#xff0c;擴展&#xff…

【傾情奉獻】遙感物候研究:30年長時間序列遙感數據集GIMMS 3g NDVI產品預處理完整步驟

本文為作者碩士學位論文遙感物候研究數據處理過程總結。GIMMS(Global Inventory Modelling and Mapping Studies) 3g NDVI來源于ECOCAST網站(http://ecocast.arc.nasa.gov),是由NOAA衛星搭載的AVHRR傳感器獲取的全球植被數據,空間分辨率為0.0833?,時間分辨率為15?d,一…

過早的給方法中 引用對象 設為 null 可被 GC提前回收嗎?

經常在代碼中看到有人將 null 賦值給引用類型&#xff0c;來達到讓 GC 提前回收的目的&#xff0c;這樣做真的有用嗎&#xff1f;今天我們就來研究一下。為了方便講解&#xff0c;來一段測試代碼&#xff0c;提前將 test1null &#xff0c;然后調用 GC.Collect() 看看是否能提前…

[python opencv 計算機視覺零基礎到實戰] 十五 直方圖反向投影

一、學習目標 了解了直方圖反向投影的一般流程了解2D直方圖的使用 如有錯誤歡迎指出~ 二、了解直方圖反向投影 2.1 了解2D直方圖 需要對直方圖進行反向投影&#xff0c;需要使用2D直方圖。2D直方圖需要使用calcHist方法。calcHist方法在前兩節中已經有了解&#xff0c;現在…

關聯規則java代碼_重量挖掘關聯規則挖掘方法,哪個大神可以將以下偽代碼轉換為Java代碼?...

重量挖掘關聯規則挖掘方法&#xff0c;哪個大神可以將以下偽代碼轉換為Java代碼&#xff1f; 10改進的加權關聯規則算法的基本步驟與Apriori算法相似: 首先找到加權支持度不小于用戶指定的最小加權支持度的所有頻繁項集加權關聯規則&#xff0c;然后使用頻繁項集生成所有滿足最…

Boostrap ZURB Foundation —— Web開發前端框架

webflow&#xff1a;Webflow 允許設計師通過自由的拖拉拽與 CSS 類互動&#xff0c;而定義它們的過程無需寫任何一行代碼。用戶在完成從設計到 CSS 構架之后&#xff0c;甚至可以在線直接將建好的網頁發布&#xff0c;而不需要導出代碼到其他發布工具上。類似的這些 B2D 市場&a…

Git之HEAD和origin

1 問題 我們經常看見git相關操作里面看到HEAD和origin這些專業名稱&#xff0c;它娘的到底什么意思。 2 解釋 1&#xff09;HEAD git 中的分支&#xff0c;本質上僅僅是個指向 commit 對象的可變指針&#xff0c; HEAD 是一個特別指針&#xff0c;它是一個指向你正在工作中的…

如何離線安裝chrome插件

如何離線安裝chrome插件 本文轉自Work Hard Work Smart博客園博客&#xff0c;原文鏈接&#xff1a;http://www.cnblogs.com/linlf03/p/6838852.html&#xff0c;如需轉載請自行聯系原作者

多種語言《九九乘法表》薈萃:C、C++、C#、JavaScript、SQL、VB、VBA、Python

九九乘法表對于我們學習循環結構,尤其是雙重循環特別有幫助,本文演示用C、C++、C#、HTML、SQL、VB、VBA、Python等多種語九九乘法表。 一、C語言 #include<stdio.h> main() {int i,j;for(i=1;i<=9;i++){for(j=1;j<=i;j++){printf("%d*%d=%d\t",j,i,i*j…

Git之刪除遠程分支

1 問題 在工作區間刪除遠程分支 2 刪除命令 git push origin --delete name

iptables (2) 基本配置

iptables 基本命令使用舉例 一、鏈的基本操作 1、清除所有的規則。1&#xff09;清除預設表filter中所有規則鏈中的規則。# iptables -F -F, --flush [chain]Flush the selected chain (all the chains in the table if none is given). This is equivalent to deleting all …

[python opencv 計算機視覺零基礎到實戰] 十六、用opencv畫畫

一、學習目標 了解如何使用line方法了解如何使用rectangle方法了解如何使用ellipse方法 如有錯誤歡迎指出~ 二、了解OpenCV的繪圖方法 2.1 了解直線繪圖方法 我們在前兩節中有了解使用OpenCV中的矩形繪制&#xff0c;接下來我們了解一下更多的圖形繪制方法。我們在OpenCV中…