貪欲にダイナミックプランニングを組み合わせる-POJ-1069-Monkey and Banana

4767 ワード

Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5395    Accepted Submission(s): 2765
Problem Description
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. 
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked. 
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
 
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
 
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
 
Sample Input

   
   
   
   
1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0

 
Sample Output

   
   
   
   
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342

 
テーマ:
箱が山積みで、長さと幅があり、x,y,z. 上に置いた箱は、长さも幅も下の箱より大きく、片方だけ大きくてはいけません.
箱の方向は自由に置けます.このように,x,y,zには6つの組合せがある.実は3種類とも言えます.
上の箱と比較するので、ここは計算の便利さのため、直接6種類に書きます. 
最大の高さを求めます.
必要な大きな箱が下にあることを考慮します.これは実は 最大増分サブセグメントと問題の性質.
知らない友達はここを見てください.http://blog.csdn.net/gaotong2055/article/details/8985452
ただ、この場合、私たちは先に順番を決める必要があります.大きな箱を前に置く.少し強引に言えば、貪欲な思想を使ったと言える.
x辺の最大の箱が必ずしも一番下に置かれるとは限らないが、例えばy辺が小さいとは限らないが、他の箱の上にも置けないに違いないので、前に置いてもいい.
したがって、xエッジでソートできます.しかし、最後に比較するときは、x 1>x 2&&y 1>y 2に厳格に従います.
次のコードは,アスペクトが等しい場合を最適化していないが,結局データ量はそれほど大きくない.
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

struct Node {
	int x, y, z;
};

Node blocks[100];
int opt[100];
int a, b, c;

bool cmp(const Node & a, const Node & b) {
	if (a.x == b.x)
		return a.y > b.y;
	else
		return a.x > b.x;
}


int main() {
	int n;
	int cnt = 0;
	while (cin >> n, n) {
		cnt++;
		for (int i = 0; i < 6* n; i += 6) {
			cin >> a >> b >> c;
			blocks[i].x = a;
			blocks[i].y = b;
			blocks[i].z = c;

			blocks[i + 1].x = b;
			blocks[i + 1].y = c;
			blocks[i + 1].z = a;

			blocks[i + 2].x = c;
			blocks[i + 2].y = a;
			blocks[i + 2].z = b;

			blocks[i+3].x = a;
			blocks[i+3].y = c;
			blocks[i+3].z = b;

			blocks[i + 4].x = b;
			blocks[i + 4].y = a;
			blocks[i + 4].z = c;

			blocks[i + 5].x = c;
			blocks[i + 5].y = b;
			blocks[i + 5].z = a;

		}

		sort(blocks, blocks + 6 * n, cmp);
		for (int i = 0; i < 6 * n; i++) {
			opt[i] = blocks[i].z;
			for (int j = 0; j < i; j++) {
				if ( (blocks[i].x < blocks[j].x && blocks[i].y < blocks[j].y)) {
					if (opt[i] < opt[j] + blocks[i].z) {
						opt[i] = opt[j] + blocks[i].z;
					}
				}

			}
		}

		int max = 0;
		for (int i = 0; i < 6 * n; i++) {
			if (opt[i] > max)
				max = opt[i];
		}

		cout <<"Case " << cnt <<": maximum height = " << max << endl;

	}

	return 0;
}