hdu1069java
给你n个方块,其中每个方块具有它的长宽高(方块可以任意旋转放置),方块数量不限。现在你要堆一个高塔,上面方块的长和宽必须严格小于下面方块的长和宽。问你能堆起来的最大高度。
先将方块以长和宽按从小到大排序,然后从小到大以此为底,求出最大高度。dp[i] = max(dp[j])+i.height (j.x<i.x&&j.y<i.y),最后的结果为max(dp)。
import java.util.Arrays; import java.util.Scanner; class Block implements Comparable<Block>{ int x, y, z; Block(int _x, int _y, int _z) { x = _x; y = _y; z = _z; } @Override public int compareTo(Block arg0) { if(x != arg0.x) return x - arg0.x; return y - arg0.y; } } public class hdu1069 { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc = new Scanner(System.in); int cas = 1; while (sc.hasNext()) { int n = sc.nextInt(); if (n==0) { break; } Block[] bk = new Block[6*n]; for (int i = 0; i < n; i++) { int x = sc.nextInt(); int y = sc.nextInt(); int z = sc.nextInt(); bk[6*i] = new Block(x, y, z); bk[6*i+1] = new Block(x, z, y); bk[6*i+2] = new Block(y, x, z); bk[6*i+3] = new Block(y, z, x); bk[6*i+4] = new Block(z, x, y); bk[6*i+5] = new Block(z, y, x); } Arrays.sort(bk); int[] dp = new int[6*n]; //设以谁为底 int ans = 0; for (int i = 0; i < bk.length; i++) { int max = 0; for (int j = 0; j < i; j++) { if ((bk[j].x >= bk[i].x)||(bk[j].y >= bk[i].y)) { continue; } max = Math.max(max, dp[j]); } dp[i] = max+bk[i].z; ans = Math.max(ans, dp[i]); } System.out.println("Case "+cas+": maximum height = "+ans); cas++; } sc.close(); } }