1. 程式人生 > >poset_Dilworth定理及其應用

poset_Dilworth定理及其應用

引用三篇比較好的文章,是對鏈,反鏈,偏序集,Dilworth的講解和證明

推薦四個可以用此方法做出的習題

對於邊界問題,需要帶數試一試

HDU 1257

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
#define MAXN 20005
int num[MAXN],dp[MAXN];
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        for(int i = 0;i < n;i++)
            scanf("%d",&num[i]);
        memset(dp,0x3f,sizeof(dp));
        int ans = 0;
        for(int i = 0;i < n;i++)
        {
            int ind = lower_bound(dp,dp + n,num[i]) - dp;
            dp[ind] = num[i];
            ans = max(ans,ind);
        }
        printf("%d\n",ans + 1);
    }
}

poj 1065
#include<iostream>
#include<cstring>
#include<cstdio>
#include <functional>
#include<algorithm>
using namespace std;
typedef pair<int,int>pii;
#define MAXN 50010
int dp[MAXN];
pii num[MAXN];
int main()
{
    int _,n;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d",&n);
        for(int i = 0;i < n;i++)
            scanf("%d%d",&num[i].first,&num[i].second);
        sort(num,num + n);
        memset(dp,0,sizeof(dp));
        int ans = 0;
        for(int i = 0;i < n;i++)
        {
            int ind = lower_bound(dp,dp + n,num[i].second,greater<int>()) - dp;
            dp[ind] = num[i].second;
            ans = max(ans,ind);
        }
        printf("%d\n",ans + 1);
    }
}

poj 1548
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
typedef pair<int,int>pii;
#define x first
#define y second
#define MAXN 900
pii num[MAXN];
int dp[MAXN];
int main()
{
    int n;
    while(scanf("%d%d",&num[0].x,&num[0].y),num[0].x + 1 | num[0].y + 1)
    {
        n = 1;
        while(scanf("%d%d",&num[n].x,&num[n].y),num[n].x | num[n].y)
            n++;
        sort(num,num + n);
        memset(dp,0,sizeof(dp));
        int ans = 0;
        for(int i = 0;i < n;i++)
        {
            int ind = lower_bound(dp,dp + n,num[i].y,greater<int>()) - dp;
            dp[ind] = num[i].y;
            ans = max(ans,ind);
        }
        printf("%d\n",ans + 1);
    }
}

HDU 1677 POJ 3636
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
struct pii
{
    int x,y;
    bool operator < (const pii & p)const{
        return x == p.x ? y > p.y : x < p.x;
    }
};
#define MAXN 20005
pii num[MAXN];
int dp[MAXN];
int main()
{
    int n,_;
    scanf("%d",&_);
    while(_--)
    {
        scanf("%d",&n);
        for(int i = 0;i < n;i++)
            scanf("%d%d",&num[i].x,&num[i].y);
        sort(num,num + n);
        memset(dp,0,sizeof(dp));
        int ans = 0;
        for(int i = 0;i < n;i++)
        {
            int ind = upper_bound(dp,dp + n,num[i].y,greater<int>()) - dp;
            dp[ind] = num[i].y;
            ans = max(ans,ind);
        }
        printf("%d\n",ans + 1);
    }
}