当前位置:网站首页>Monkey and Banana
Monkey and Banana
2022-07-30 06:10:00 【beyond+myself】
题目链接
题意:有n种不同类型的砖块,要求上面的砖块的长宽严格小于下面的砖块的长宽,问最多可以有多高。(每种类型的砖块可以有无限个)。刚开始想的时候以为是完全背包模型,题中给的限制也很迷惑,每一种可以无限选择,还有两维的限制,所以我想用f[i][j][k]表示在前i个中选择,第i-1个比第i长宽小的情况。这样很表示起来很复杂,考虑很多的情况。
而且我们可以分析到,实际上背包问题是当前的处理过去之后是不能再回来的,也就是选择过去后就不能再选择,而这个问题实际上是可以回来在选择的。所以背包问题不能解决。我们再看这个题实际上是最长上升子序列的模型,不过转移有两个条件而已。
下面是AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=110;
int dp[N];
struct node
{
int a,b,c;
bool operator <(node &w)
{
if(a!=w.a) return a<w.a;
else return b<w.b;
}
}box[N];
int main()
{
int cou=0;
int n;
while(~scanf("%d",&n)&&n)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
if(a>c) swap(a,c);
if(b>c) swap(b,c);
box[++cnt]={
a,b,c};
box[++cnt]={
a,c,b};
box[++cnt]={
b,c,a};//这里我们可以题中分析出来同一个最多选三次,所以存三个即可
}
sort(box+1,box+1+cnt);//这里贪心就是因为这个题中并没有要求从头开始选的,所以我们尽可能从小的开始选
//找最长上升子序列
int res=0;
for(int i=1;i<=cnt;i++)
{
dp[i]=box[i].c;
for(int j=1;j<i;j++)
{
if(box[i].a>box[j].a&&box[i].b>box[j].b) dp[i]=max(dp[i],dp[j]+box[i].c);
}
res=max(res,dp[i]);
}
printf("Case %d: maximum height = %d\n",++cou,res);
}
return 0;
}
总结:刚开始的时候我就想到了最长上升子序列的模型,但是一看到就以为是二维的直接砍了,根本没有细想实际上是有两个条件,把这个题想复杂了,一直以为是完全背包模型。所以做题再有任何的想法一定要把自己想出的想法认真思考后再定夺,防止一上来就把正确的想法给否了,而且dp问题找到正确的转移方程或是模型后其实是很好表达或理解的。
边栏推荐
猜你喜欢

k8s 部署mysql8(PV和PVC 版本)

LSF提交作业命令--bsub

What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles

预测人们对你的第一印象,“AI颜狗”的诞生

Pioneer in Distributed Systems - Leslie Lambert

物联网网关该怎么选

Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)

2020 数学建模之旅

golang : Zap log integration

When does MySQL use table locks and when does it use row locks?
随机推荐
assert
VR机器人教你如何正确打乒乓球
如何实时计算日累计逐单资金流
Keil compile size and storage instructions
New material under the plastic restriction order - polylactic acid (PLA)
Upload file -- file type, picture type, document type, video type, compressed package type
DP5340 domestic replacement for CM5340 stereo audio A/D converter chip
goto语句
Electron之初出茅庐——搭建环境并运行第一个程序
What new materials are used in the large aircraft C919?
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
限塑令下的新材料——聚乳酸(PLA)
头条二面:MySQL中有几种常见的 SQL 错误用法?
go : go-redis list操作
Table with tens of millions of data, how to query the fastest?
常用的配置
从 Google 离职,前Go 语言负责人跳槽小公司
How to calculate the daily cumulative capital flow one by one in real time
go : 使用gorm查询记录
AI can identify race from X-rays, but no one knows why