动态规划 杭电4001 大连赛区网络赛水题

本文探讨了一个与儿童时代游戏相关的编程挑战,即利用不同类型的立方体构建最高‘摩天大楼’。通过详细解析输入格式、问题描述及样例输入输出,作者介绍了如何使用排序技巧来解决此问题,并提供了AC代码实现。文章深入浅出地解释了通过合理排序和比较不同类型立方体的尺寸与属性,来确定最优堆叠顺序,从而构建出最高的‘摩天大楼’。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        看别人说这是打联赛区网络赛的水题,,水题,,,结果水了几个小时,有木有!!!!!!水不水!!!!!!!由于下午刚做了一道类似的题,所以做这道题的时候就感觉简单了一点。。刚开始wa了几次,主要是排序没排好。个人感觉此类题目最主要的还是排好序,能排好序,这道题就基本上解决了。具体来说这道题,就是按长、宽排序,当长和宽都相等时,则按类型序号排序。因为由题意知,类型序号大的条件更严格,因此,在满足条件的前提下,应该先放类型序号大的。最后在遍历一遍,找最大值即可。需要注意的是数据类型,用_int64即可。

题目:

To Miss Our Children Time

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2375    Accepted Submission(s): 636


Problem Description
Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy, now he grows up. In the dead of night, he often misses something, including a simple game which brings him much happy when he was child. Here are the game rules: There lies many blocks on the ground, little LLL wants build "Skyscraper" using these blocks. There are three kinds of blocks signed by an integer d. We describe each block's shape is Cuboid using four integers ai, bi, ci, di. ai, bi are two edges of the block one of them is length the other is width. ci is 
thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
 

Input
The input has many test cases. 
For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks. 
From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2). 
The input end with n = 0.
 

Output
Output a line contains a integer describing the highest "Skyscraper"'s height using the n blocks.
 

Sample Input
3 10 10 12 0 10 10 12 1 10 10 11 2 2 10 10 11 1 10 10 11 1 0
 

Sample Output
24 11
 

ac代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
struct block{
  int num;
  _int64 length;
  _int64 width;
  _int64 height;
  _int64 area;
}aa[1005];
bool cmp(block a,block b){
	if(a.length==b.length){
		if(a.width==b.width)
			return a.num>b.num;
		else
			return a.width<b.width;
	}
	return a.length<b.length;
}
int main(){
// freopen("4.txt","r",stdin);
  int n;
 while(scanf("%d",&n)&&n){  
	_int64 a,b,dp[1005];
    for(int i=0;i<n;++i){
		scanf("%I64d%I64d%I64d%d",&a,&b,&aa[i].height,&aa[i].num);
		aa[i].length=a>b?a:b;
		aa[i].width=a<b?a:b;
		aa[i].area=aa[i].length*aa[i].width;
	}
	sort(aa,aa+n,cmp);
	for(int i=0;i<n;++i)
		dp[i]=aa[i].height;
	_int64 xmax=0;
	for(int i=1;i<n;++i){
		xmax=0;
		if(aa[i].num==2){
		   for(int j=0;j<i;++j){
			   if(aa[j].length<aa[i].length&&aa[j].width<aa[i].width&&dp[j]>xmax)
				   xmax=dp[j];
		   }
		}
		else if(aa[i].num==1){
			for(int j=0;j<i;++j){
				if(aa[j].length<=aa[i].length&&aa[j].width<=aa[i].width&&aa[j].area<aa[i].area&&dp[j]>xmax)
					xmax=dp[j];
			}
		}
		else{
			for(int j=0;j<i;++j){
				if(aa[j].length<=aa[i].length&&aa[j].width<=aa[i].width&&dp[j]>xmax)
					xmax=dp[j];
			}
		}
		//printf("xmax==%d\n",xmax);
		dp[i]=aa[i].height+xmax;
	}
	xmax=0;
	for(int i=0;i<n;++i)
		if(dp[i]>xmax)
			xmax=dp[i];
	printf("%I64d\n",xmax);
  }
  return 0;
}


转载于:https://www.cnblogs.com/javaspring/archive/2012/01/03/2656423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值