POJ 1065 & ZOJ 1025

I'm so stupid...I have Waed so many times on this problem...

这是神奇的Dilworth定理的一个应用,可以自行google一下,非常amazing的一个theory

以木头的一个性质(例如长度升序排列),之后将思路转化为了最长下降子序列(这里的下降是针对另一个性质),DP求解。

实际上相当于把题目中,不需要setup time时的条件看作是一个偏序,然后这就是一个显然对Dilworth定理。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn= 5e3+5;
const int INF= 0x7f7f7f7f;
struct Wod{
	int l, w;
	Wod()= default;
	Wod(int ll, int ww) : l(ll), w(ww) {}
	bool operator < (const Wod& a) const{
		return l< a.l || (l== a.l && w< a.w);
	}
}wds[maxn];
int lds[maxn], flg[maxn];
int ans= -INF;

void Init()
{
	memset(flg, 0, sizeof(flg));
	memset(lds, 0, sizeof(lds));
	memset(wds, 0, sizeof(wds));
}
int main()
{
	int T, n;
	scanf("%d", &T);

	while (T--){
		Init();
		scanf("%d", &n);

		int l, w;
		for (int i= 1; i<= n; ++i){
			scanf("%d %d", &l, &w);
			wds[i]= Wod(l, w);
		}
		sort(wds+1, wds+n+1);

		for (int i= 1; i<= n; ++i){
			for (int j= 1; j< i; ++j){
				if (wds[j].w > wds[i].w && lds[j] > lds[i]){
					lds[i]= lds[j];
				}
			}
			++lds[i];
			ans= max(ans, lds[i]);
		}
		printf("%d
", ans);
	}

	return 0;
}
原文地址:https://www.cnblogs.com/Idi0t-N3/p/12499522.html