题意:
有很多不同的立方体(长宽高不同),每一种都有无穷多个,将他们一层层叠加起来,要求上门面一块的立方体的底面要小于下面的一块(长、宽都严格小于),问最多能搭建多高。
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
思路一: 动态规划求解
这题类似之前的Tom的烦恼,但是他很灵活,可以随便放置砖头,也就是长宽高可以分别是其中的任意边,所以预处理时候需要
把对应所有情况都加入处理。对其中按照x排序,当相同时候按照y从小到大排序。
状态转移方程为:
code:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int N = 35*3;
int n;
int dp[N];
struct Block{
int x,y,z;
Block(){}
Block(int px,int py, int pz):x(px),y(py),z(pz){}
};
bool cmp(const Block a, const Block b){
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
Block blocks[N];
int main()
{
int x,y,z;
int kcase = 0;
while(scanf("%d",&n) == 1){
if(n == 0)
break;
memset(dp,0,sizeof(dp));
int cnt = 1;
for(int i = 1; i <= n; ++i){
scanf("%d%d%d",&x,&y,&z);
if(x >= y)
blocks[cnt++] = Block(x,y,z);
else
blocks[cnt++] = Block(y,x,z);
if(x >= z)
blocks[cnt++] = Block(x,z,y);
else
blocks[cnt++] = Block(z,x,y);
if(y >= z)
blocks[cnt++] = Block(y,z,x);
else
blocks[cnt++] = Block(z,y,x);
}
sort(blocks+1,blocks+cnt,cmp);
for(int i = 1; i < cnt; ++i){
dp[i] = blocks[i].z;
}
int ans = 0;
for(int i = 1; i < cnt; ++i){
for(int j = 1; j < i; ++j){
if(blocks[j].x < blocks[i].x && blocks[j].y < blocks[i].y){
dp[i] = max(dp[i],dp[j] + blocks[i].z); //这个 位置 是加上 blcoks[i].z 不要写成j的
}
}
ans = max(dp[i],ans);
}
printf("Case %d: maximum height = %d\n",++kcase,ans);
}
return 0;
}
方法二: floyd 最长路问题
预处理同上,只是不需要排序了。
在 满足 的两个方块建立一条边,边长为 两者高度之和。
下面就是 floyd求最长路的过程,实质上floyd也是动态规划。
注意:mp 数组开大点,不然会wa掉。
code:
#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int N = 1000;
const int INF = 0x3f3f3f3f;
int mp[N][N];
struct Block{
int x,y,z;
Block(){}
Block(int px,int py,int pz):x(px),y(py),z(pz){}
};
Block blocks[N];
int main()
{
int n;
int kcase = 0;
int x,y,z;
while(scanf("%d",&n) && n != 0){
int cnt = 0;
memset(mp,-INF,sizeof(mp));
for(int i = 0; i < n; ++i){
scanf("%d%d%d",&x,&y,&z);
blocks[cnt++] = Block(x,y,z);
blocks[cnt++] = Block(y,x,z);
blocks[cnt++] = Block(x,z,y);
blocks[cnt++] = Block(z,x,y);
blocks[cnt++] = Block(z,y,x);
blocks[cnt++] = Block(y,z,x);
}
for(int i = 0; i < cnt; ++i)
mp[i][i] = blocks[i].z;
for(int i = 0; i < cnt; ++i){
for(int j = 0; j < cnt; ++j){
if(blocks[j].x > blocks[i].x && blocks[j].y > blocks[i].y){
mp[j][i] = blocks[i].z + blocks[j].z;
}
}
}
int ans = 0;
for(int k = 0; k < cnt; ++k){
for(int i = 0; i < cnt; ++i){
for(int j = 0; j < cnt; ++j){
if(i != j && mp[i][j] != -INF){
mp[i][j] = max(mp[i][j],mp[i][k] + mp[k][j]-blocks[k].z);//这快因为建图的时候加上过了,需要去掉重复加上的 blocks[k].z
ans = max(ans,mp[i][j]);
}
}
}
}
printf("Case %d: maximum height = %d\n",++kcase,ans);
}
return 0;
}