POJ 1065 & ZOJ 1025

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;
}
相關文章
相關標籤/搜索