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
题目大意:给你几种盒子要求上面的长宽一定比下面的小求一个最大的高度
思路:看完题后感觉这题跟贪心专题的题真的很像,条件变成了长宽了。。但是写起来发现这个也不太一样
做这个题有几个地方是关键,一盒子可以转,一转就是一个新的盒子
第二就是dp了需要记住每种做高的盒子所能到达 的高度然后取最高输出
代码:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct box
{
int x,y,z;
} ibox[91];
bool cmp( box a, box b ){
if( a.x > b.x ) return true;
else if( a.x == b.x && a.y > b.y ) return true;
return false;
}
int main()
{
int n;
int sum=1;
while (cin>>n&&n!=0)
{
int temp=0;
int x,y,z;
for(int j=0;j<n;j++)
{
cin>>x>>y>>z;
ibox[temp].x=max(x,y);
ibox[temp].y=min(x,y);
ibox[temp].z=z;
ibox[temp+2].x=max(y,z);
ibox[temp+2].y=min(y,z);
ibox[temp+2].z=x;
ibox[temp+1].x=max(x,z);
ibox[temp+1].y=min(x,z);
ibox[temp+1].z=y;
temp+=3;
}
sort(ibox,ibox+3*n,cmp);
int dp[100];
memset( dp, 0, sizeof( dp ));
int ans = 0;
for( int i = 0; i < 3 * n; i++ ){
for( int j = 0; j < i; j++ )
if( ibox[i].x <ibox[j].x && ibox[i].y < ibox[j].y ){
dp[i] = max( dp[i], dp[j] );
}
dp[i] += ibox[i].z;
ans = max( ans, dp[i] );
}
cout << "Case " << sum++ << ": maximum height = " << ans << endl;
}
return 0;
}