Main idea of the title:
There are cuboids in n, each of which has infinite numbers. Some cubes are required to be constructed as high as possible so that the length and width of the bottom of each cube are strictly smaller than that of the cube below it (the length is similar to that of the pyramid). Note that not small equals!! And a cube has three parameters, each of which can be regarded as high.
Topic Analysis:
This is the problem of optimal substructure. From the beginning, we should consider how to maximize the tower. At any time, only the size of the current top surface will affect subsequent decisions. Therefore, a two-dimensional array d(i,j) can be used to represent the length and width of the current top surface. But because the length and width may be very large, leading to the array can not open, so we have to indirectly express this state.
From the analysis of the problem, we find that there are three possibilities for the height of a cube. That is to say, a cube can be divided into three forms. If we use a two-dimensional array block[i][k] to access the three parameters of the first cube from the beginning, we can use d(idx,j) to represent the jth form of the idx cube, and the length and width correspond to the other two elements in r[i], except the jth element. This greatly reduces the size of the array.
The total number of States is O(n), the decision-making is O(n), and the time complexity is O(n^2).
State transition equation:
d(i)(j) = max(d(i)(j) , block[i][j] + dp(x,y)).
Because recursion is too complex, memory search is used.
Code:
#include <set>
#include <numeric>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
#include <sstream>
#include <map>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define REP(idx1,num1) for(int idx1=0;idx1<(num1);idx1++)
#define pb push_back
#define empb emplace_back
#define mp make_pair
#define mem(s) memset(s,0,sizeof(s));
const double EPS = 1e-6;
//const int maxn = 280;
int d[33][5];
int n;
int block[33][4];
void get_b(int *v,int i,int h)//Obtaining Length and Width
{
int ptr = 0;
for(int j = 0; j < 3; ++j)
if(j != h)
v[ptr++] = block[i][j];
}
int dp(int x,int y)
{
if(d[x][y] > 0) return d[x][y];
d[x][y] = block[x][y];//Initialization.
int v1[2];//Length and Width of Current Top Surface
get_b(v1,x,y);
for(int i = 0; i < n; ++i)
for(int j = 0; j < 3; ++j)
{
int v[2];//The Length and Width of the Cuboid of the Tower
get_b(v,i,j);
if(v[0] < v1[0] && v[1] < v1[1])
d[x][y] = max(d[x][y], block[x][y]+dp(i,j));
else if(v[1] < v1[0] && v[0] < v1[1])
d[x][y] = max(d[x][y], block[x][y]+dp(i,j));
}
return d[x][y];
}
int main()
{
int kase = 1;
while(cin >> n && n){
mem(d);
for(int i = 0; i < n; ++i){
for(int j = 0; j < 3; ++j)
cin >> block[i][j];
//sort(block[i],block[i]+3);
//If sorted here, the state transition equation above can be written one less.
}
//Enumerate all cubes
int tallest = -1;
for(int i = 0; i < n; ++i)
for(int j = 0; j < 3; ++j)
tallest = max(tallest,dp(i,j));
printf("Case %d: maximum height = %d\n",kase++,tallest);
}
return 0;
}