题目传送门
题目大意
给你n个砖块和每个砖块的三个数值,分别为长宽高并且可以任意选择,每个砖块可以使用任意次。
上面的砖块的长宽必须严格小于下面的砖块,求最高可以堆叠砖块到多高?
思路
每个砖块一共有
3
∗
2
∗
1
3*2*1
3∗2∗1种情况,限制长宽后即为3种情况,所以我们可以将三种情况全部存下(任意次数使用)
然后按照长宽由小到大排序
dp每层可以达到的最大高度(每i层之前的最大高度加上本层高度作为下次比较的高度)
注意点:因为存三次,所以数据范围实际为
0
<
=
n
<
=
90
0<=n<=90
0<=n<=90
AC Code
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
// #define TDS_ACM_LOCAL
typedef long long ll;
const double PI=acos(-1.0);
const double e=exp(1.0);
const int M=1e9+7;
const int N=109;
inline int mymax(int x,int y){return x>y?x:y;}
inline int mymin(int x,int y){return x<y?x:y;}
inline int read(){
register int x=0, f=0;
register char ch=getchar();
while(ch<'0'||ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return f?-x:x;
}
inline void write(register int x) {
if(x < 0) {putchar('-');x = -x;}
if(x > 9) write(x/10);
putchar(x % 10 + '0');
}
struct node
{
int l, w, h;
}a[N];
int n, x, y, z, len, k;
int dp[N];
inline bool cmp(node a, node b){
if(a.l==b.l) return a.w<b.w;
return a.l<b.l;
}
void solve(){
while(~scanf("%d", &n) && n)
{
len=0;
for(int i=1; i<=n; i++){
x=read(); y=read(); z=read();
a[++len].l=mymax(y,z); a[len].w=mymin(y,z); a[len].h=x;
a[++len].l=mymax(x,z); a[len].w=mymin(x,z); a[len].h=y;
a[++len].l=mymax(x,y); a[len].w=mymin(x,y); a[len].h=z;
}
sort(a+1, a+1+len, cmp);
dp[1]=a[1].h;
int mx=0, ans=dp[0];
for(int i=2; i<=len; i++){
mx=0;
for(int j=1; j<i; j++)
if(a[j].l<a[i].l && a[j].w<a[i].w)
mx=mymax(dp[j],mx);
dp[i]=a[i].h+mx;
ans=mymax(ans,dp[i]);
}
cout<<"Case "<<++k<<": maximum height = "<<ans<<endl;
}
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
solve();
return 0;
}