题目链接:Monkey and Banana
题解:Monkey and Banana(最长上升子序列+贪心)
题意:有n种长方体,每种有无限块,求最多能垒多高,要求(长方体可以以任意面为底摆放;上面长方体的长和宽要严格小于下面长方体的长和宽)
- 观察题面,看起来每种有无限块,但是块面是严格小于的,即同一种面不能重叠。
- 同理可推得一块长方体最多用3次。那么就可以把长方体的3个块面存起来排序,线性dp即可。
代码:
#include<iostream>
#include<map>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100;
ll dp[N];
struct node{
int a,b,c;
}blo[N];
int cnt;
bool cmp(node & x,node & y)
{
if(x.a!=y.a) return x.a<y.a;
return x.b<y.b;
}
int main()
{
int n,pos=1;
while(scanf("%d",&n) && n)
{
int a,b,c; cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&a,&b,&c);
if(a>b) swap(a,b);
if(b>c) swap(b,c);
if(a>b) swap(a,b);
blo[cnt++]={a,b,c};
blo[cnt++]={a,c,b};
blo[cnt++]={b,c,a};
}
for(int i=0;i<N;i++) dp[i]=0;
sort(blo,blo+cnt,cmp);
ll res=0;
for(int i=0;i<cnt;i++)
{
dp[i]=blo[i].c;
for(int j=0;j<i;j++)
if(blo[j].a<blo[i].a && blo[j].b<blo[i].b) dp[i]=max(dp[i],dp[j]+blo[i].c);
res=max(res,dp[i]);
}
printf("Case %d: maximum height = %lld\n",pos,res);
pos++;
}
return 0;
}
该博客介绍了一种利用最长上升子序列和贪心策略解决长方体堆叠问题的方法。题目要求在确保长方体严格不重叠的情况下,找出能垒起的最大高度。通过将长方体的不同面存储并排序,然后使用线性动态规划找到最优解。代码示例中展示了如何实现这一过程,并给出了详细的解题思路。
7213

被折叠的 条评论
为什么被折叠?



