Monkey and Banana
题意: 给定一些长方体,问可以摆出的最大高度,要求下层长宽必须严格大于上层。
分析: 每个长方体都有三种摆放的顺序呢,那么接下来就把所有情况记录下来搭建积木。
代码如下:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cctype>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
struct node{
int a,b,h;
bool operator <(const node s)const{
if(a == s.a)
return b < s.b;
else return a < s.a;
}
}box[100];
int dp[100];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,kase = 1;
while(cin>> n && n){
int s[3],k = 0;
for(int i=0;i<n;i++){
cin>> s[0] >> s[1] >> s[2];
sort(s,s+3);
//转换成三个不同高度的长方体
box[k].a = s[0],box[k].b = s[1],box[k].h = s[2];k++;
box[k].a = s[0],box[k].b = s[2],box[k].h = s[1];k++;
box[k].a = s[1],box[k].b = s[2],box[k].h = s[0];k++;
}
sort(box,box+k);
/*for(int i=0;i<k;i++)
cout<< box[i].a <<box[i].b << box[i].h<<endl;
*/
for(int i=0;i<k;i++)
dp[i] = box[i].h;
for(int i=0;i<k;i++){
for(int j=i+1;j>=0;j--){
if(box[i].a>box[j].a && box[i].b>box[j].b){
if(dp[i]<dp[j] + box[i].h)
dp[i] = dp[j] + box[i].h;
}
}
}
int ans = dp[0];
for(int i=0;i<k;i++){
if(ans<dp[i])
ans = dp[i];
}
printf("Case %d: maximum height = %d\n",kase++,ans);
}
return 0;
}