I'm so stupid...I have Waed so many times on this problem...ios
这是神奇的Dilworth定理的一个应用,能够自行google一下,很是amazing的一个theoryc++
以木头的一个性质(例如长度升序排列),以后将思路转化为了最长降低子序列(这里的降低是针对另外一个性质),DP求解。this
实际上至关于把题目中,不须要setup time时的条件看做是一个偏序,而后这就是一个显然对Dilworth定理。google
#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\n", ans); } return 0; }