HDU 4001 DP LIS

/*******************************************************************************
   大連網絡賽的DP水題,但是當時我們隊沒過,其實本質跟HDU第一頁的那個Monkey and Banana沒啥區
別。。。一開始用棧存儲,YY是普通的LIS,結果無限WA,現在想來,這題的block排完序後,也談不上什麼
單不單調的,用棧是不靠譜。。。具體解法就是先把block排下序,width從小到大,length從小到大,kind
從大到小,這樣是爲了保證DP時不出現無法構造Skyscraper,這個可以體會一下,然後這裏可以設個哨兵,方
便後面的比較,接着就是DP了,DP數組記錄的是第i個block能得到的最高的高度,然後通過比較i之前的block
就能得到最後的結果了~
*******************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
typedef __int64 LL;		//VC
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<double> VF;
typedef vector<string> VS;

template <typename T>
T sqa(const T & x)
{
	return x * x;
}
template <typename T>
T gcd(T a, T b)
{
	if (!a || !b)
	{
		return max(a, b);
	}
	T t;
	while (t = a % b)
	{
		a = b;
		b = t;
	}
	return b;
};

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-7;
const double PI = acos(-1.0);

#define  ONLINE_JUDGE

const int MAXN = 1004;

int n;
LL dp[MAXN];
struct Node
{
	int width;
	int length;
	int thickness;
	int kind;
	Node()
	{
		CLR(this, 0);
	}
	LL area()
	{
		return SC(LL, width) * SC(LL, length);
	}
}blocks[MAXN];

bool cmp(const Node & na, const Node & nb)
{
	if (na.width != nb.width)
	{
	return na.width < nb.width;
	}
	if (na.length != nb.length)
	{
	return na.length < nb.length;
	}
	return na.kind > nb.kind;
}
bool judge(Node & pre, Node & crt)
{
	switch (crt.kind)
	{
	case 0:
		return crt.width >= pre.width && crt.length >= pre.length;
		break;
	case 1:
		return crt.width >= pre.width && crt.length >= pre.length && crt.area() > pre.area();
		break;
	case 2:
		return crt.width > pre.width && crt.length > pre.length;
		break;
	default:
		break;
	}
	return false;
}
void ace()
{
	const Node unit = Node();
	while (cin >> n, n)
	{
		for (int i = 0; i < n; ++i)
		{
			cin >> blocks[i].width >> blocks[i].length
				>> blocks[i].thickness >> blocks[i].kind;
		}
		for (int i = 0; i < n; ++i)
		{
			if (blocks[i].width > blocks[i].length)
			{
				swap(blocks[i].width, blocks[i].length);
			}
		}
		blocks[n++] = unit;		//哨兵~~~
		sort(blocks, blocks + n, cmp);
		CLR(dp, 0);
		for (int i = 1; i < n; ++i)
		{
			dp[i] = blocks[i].thickness;	//細節
			for (int j = i - 1; j >= 0; --j)
			{
				if (!judge(blocks[j], blocks[i]))
				{
					continue ;
				}
				dp[i] = max(dp[i], dp[j] + blocks[i].thickness);
			}
		}
		LL res = 0;
		for (int i = 0; i < n; ++i)
		{
			res = max(res, dp[i]);
		}
		cout << res << endl;
	}
	return ;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	ace();
	return 0;
}
/*
3
10 10 12 0
10 10 12 1
10 10 11 2

2
10 10 11 1
10 10 11 1

3
4 8 1 0
4 9 2 1
5 6 4 1

2
4 8 1 0
5 7 2 0
*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章