HDU 4001 To Miss Our Children Time(2011年大连网络赛 A 贪心+dp)
开始还觉得是贪心呢。。。
给你三类积木叫你叠楼房,给你的每个积木包括四个值:长 宽(可以互换) 高 类型d
d=0:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面
d=1:你只能把它放在地上或者放在 长 宽 小于等于 自己的积木上面,但是这儿其面积必须大于下面积木的面积
d=2:你只能把它放在地上或者放在 长 宽 小于 自己的积木上面
最后给你n块积木,问你最高可以堆多高
如果直接遍历的话每块积木都可以放在任意位置(满足条件),无法使用dp。但是注意这儿有个条件就是当d变大时,堆积的条件变得越来越苛刻了,你可以看到是:两个都小于等于,只能有一个等于,两个都小于。所以可以排序,当然首先让每块积木长大于宽,再按照长 宽从小到大,d从大到小排序,这样进行dp,从前到后遍历每个位置的时候再向前便利到开头求一个最大值(满足条件)就可以了。可以遍历的依据是:没有遍历到的位置一定不能当放在当前的积木的下方
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vector> #include<string> #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1E-8 /*注意可能会有输出-0.000*/ #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型 #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化 #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0 #define mul(a,b) (a<<b) #define dir(a,b) (a>>b) typedef long long ll; typedef unsigned long long ull; const int Inf=1<<28; const double Pi=acos(-1.0); const int Max=1010; struct node { ll aa1,bb1,cc1,dd1; }sto[Max]; ll dp[Max]; bool cmp(struct node p1,struct node p2)//**排序** { if(p1.aa1==p2.aa1) { if(p1.bb1==p2.bb1) return p1.dd1>p2.dd1; else return p1.bb1<p2.bb1; } else return p1.aa1<p2.aa1; } int Jud(int i,int j) { if(!sto[i].dd1) { if(sto[i].aa1>=sto[j].aa1&&sto[i].bb1>=sto[j].bb1) return 1; } if(sto[i].dd1==1ll) { if(sto[i].aa1>=sto[j].aa1&&sto[i].bb1>sto[j].bb1|| sto[i].aa1>sto[j].aa1&&sto[i].bb1>=sto[j].bb1 ||sto[i].aa1>sto[j].aa1&&sto[i].bb1>sto[j].bb1) return 1; } if(sto[i].dd1==2ll) { if(sto[i].aa1>sto[j].aa1&&sto[i].bb1>sto[j].bb1) return 1; } return 0; } ll Solve(ll n) { ll manx=0ll; for(int i=0;i<n;i++) { dp[i]=sto[i].cc1; for(int j=0;j<i;j++) { if(Jud(i,j)) { dp[i]=max(dp[i],dp[j]+sto[i].cc1); } } } for(int i=0;i<n;i++) manx=max(manx,dp[i]); return manx; } int main() { ll n; while(~scanf("%I64d",&n)&&n) { for(int i=0;i<n;i++) { scanf("%I64d %I64d %I64d %I64d",&sto[i].aa1,&sto[i].bb1,&sto[i].cc1,&sto[i].dd1); if(sto[i].aa1<sto[i].bb1) swap(sto[i].aa1,sto[i].bb1); } sort(sto,sto+n,cmp); printf("%I64d\n",Solve(n)); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决