码迷,mamicode.com
首页 > 其他好文 > 详细

hdu(1069)——Monkey and Banana(LIS变形)

时间:2017-06-02 12:37:54      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:data   学习   struct   scan   algorithm   难题   div   name   最大   

题意:

如今给你n个石块,然后它由坐标来表示(x,y,z)。可是它能够有不同的方法,也就是说它的三个坐标能够轮换着来的。

石块的数量不限,可是每次都必须保持上底面的长和宽严格递减,然后问你用这些石块所能拼成的最大高度是多少。

思路:

由于坐标有多种情况。所以我们能够把每次的情况都存下去。

这里须要注意的是。在保存的时候,我们要保持x的坐标是大于y的。这样方便我们之后的排序。

然后就直接求最长递减子序列就好了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
using namespace std;
#define maxn 111
#define inf 99999999
int dp[maxn];
struct node{
	int x,y,z,s;
}a[maxn];
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x>b.x;
	else if(a.x==b.x) return a.y>b.y;
}
int main(){
	int n;
	int j=1;
	while(~scanf("%d",&n)){
		if(n==0) break;
		int t=0;
		for(int i=0;i<n;i++){
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			a[t].x=x>y?x:y; a[t].y=x>y?

y:x; a[t].z=z; t++; a[t].x=y>z?y:z; a[t].y=y>z?z:y; a[t].z=x; t++; a[t].x=x>z?x:z; a[t].y=x>z?z:x; a[t].z=y; t++; } sort(a,a+t,cmp); memset(dp,0,sizeof(dp)); dp[0]=a[0].z; int ans=-1; for(int i=0;i<t;i++){ int res=0; for(int j=0;j<i;j++){ if((a[i].x<a[j].x&&a[i].y<a[j].y)||(a[i].x<a[j].y&&a[i].y<a[j].x)){ res=max(res,dp[j]); } } dp[i]=res+a[i].z; ans=max(ans,dp[i]); } printf("Case %d: maximum height = %d\n",j++,ans); } }


尽管题目简单。可是这道题也是由我自己想出来的呢,由易到难的练下去吧!

我相信我自己的努力,在不久的以后一定能尝到AC难题的喜悦!

@全部学习dp的人,加油!!

hdu(1069)——Monkey and Banana(LIS变形)

标签:data   学习   struct   scan   algorithm   难题   div   name   最大   

原文地址:http://www.cnblogs.com/yutingliuyl/p/6932575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!