HDU1069 Monkey and Banana (DP)

HDU1069 Monkey and Banana (DP)

原題地址:http://acm.hdu.edu.cn/showproblem.php?pid=1069

題意

有n個小塊,每個塊能擺成6種方案,每個小塊提供無數個,下一層的長寬都必須嚴格大於上一層的長與寬。問最高能夠疊多高。

思路

將每一塊積木進行分解成六種不同的狀態,然後對它們按照長寬進行排序,這樣就保證了下一層的長寬大於上一層的,然後我們在此基礎上選取不同的積木,看選取哪一個積木可以使最後的高度最大。

AC代碼


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 *
 * @author wanyu
 * @Date: 2018-02-19
 * @Time: 20:55
 * To change this template use File | Settings | File Templates.
 * @desc
 */
public class Main {
    private static Block blocks[];

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        int index = 1;
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) in.nval;
            if (n == 0) break;
            blocks = new Block[n * 6];
            for (int i = 0; i < n; i++) {
                int[] temp = new int[3];
                for (int j = 0; j < 3; j++) {
                    in.nextToken();
                    temp[j] = (int) in.nval;
                }
                Decompose(i * 6, temp);
            }
            Arrays.sort(blocks);
            int max = LIS();
            System.out.println("Case " + index + ": maximum height = " + max);
            index++;
        }
    }

    private static int LIS() {
        int[] dp = new int[blocks.length];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = blocks[i].z;
        }
        int max = 0;
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < i; j++) {
                if (blocks[i].x < blocks[j].x && blocks[i].y < blocks[j].y) {
                    //長寬嚴格遞減
                    dp[i] = Math.max(dp[i], dp[j] + blocks[i].z);//選取不同的積木,看可以組合出的最大高度
                }
                max = Math.max(dp[j], max);
            }
        }
        return max;
    }

    private static void Decompose(int index, int data[]) {//將一個矩形分解
        for (int x = 0; x <= 2; x++) {
            for (int y = 0; y <= 2; y++) {
                if (y == x) continue;
                for (int z = 0; z <= 2; z++) {
                    if (z == y || z == x) continue;
                    blocks[index++] = new Block(data[x], data[y], data[z]);
                }
            }
        }
    }
}

class Block implements Comparable<Block> {
    int x;//長
    int y;//寬
    int z;//高

    public Block(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @Override
    public int compareTo(Block block) {
        if (this.x < block.x) {
            return 1;
        } else if (this.x == block.x) {
            if (this.y < block.y) {
                return 1;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章