hdoj1069 Monkey and Banana(Java版)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069
题目大意:给你n种能翻转的的石头,问你这些石头能叠起来的最大高度,每种石头无限。
因为这题要求上一块石头的x,y要严格小于底下的石头,所以每一种石头的每一种形态只能用一次
每一块石头有6中形态,也就是6种长宽高的全排列方式。将6n块石头的长度进行从小到大的排列
这样相当于在求6
n块石头高度的最大上升子序列和
代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/*
 * 因为这题要求上一块石头的x,y要严格小于底下的石头,所以每一种石头的每一种形态只能用一次
 * 每一块石头有6中形态,也就是6种长宽高的全排列方式。将6*n块石头的长度进行从小到大的排列
 * 这样相当于在求6*n块石头高度的最大上升子序列和
 */
public class Main {
	static class rectangle implements Comparable<rectangle>{
		int x,y,height;
		rectangle(int x,int y,int height){
			this.x=x;
			this.y=y;
			this.height=height;
		}
		@Override
		public int compareTo(rectangle r) {
			if(this.x>r.x)
				return 1;
			else if(this.x<r.x)
				return -1;
			else
				return this.y-r.y;
		}
	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n;
		int cnt=0;
		while(true){
			n=sc.nextInt();
			if(n==0)
				break;
			cnt++;
			ArrayList<rectangle> arr=new ArrayList<>();
			for(int i=0;i<n;i++){
				int x=sc.nextInt();
				int y=sc.nextInt();
				int z=sc.nextInt();
				arr.add(new rectangle(x, y, z));
				arr.add(new rectangle(x, z, y));
				arr.add(new rectangle(y, x, z));
				arr.add(new rectangle(y, z, x));
				arr.add(new rectangle(z, x, y));
				arr.add(new rectangle(z, y, x));
			}
			Collections.sort(arr);
			
			int a[]=new int[6*n];
			for(int i=0;i<6*n;i++){
				a[i]=arr.get(i).height;
			}
			int sum=a[0];
			for(int i=0;i<arr.size();i++){
				int tmp=arr.get(i).height;
				for(int j=0;j<i;j++){
					if(arr.get(i).x>arr.get(j).x&&arr.get(i).y>arr.get(j).y){
						a[i]=Math.max(a[i], a[j]+tmp);
					}
				}
				if(sum<a[i]){
					sum=a[i];
				}
			}
			System.out.println("Case "+cnt+": maximum height = "+sum);
		}
		sc.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值