• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
mengxm
博客园    首页    新随笔    联系   管理    订阅  订阅

poj3636

dilworth定理应用

#include <stdio.h>
#include <algorithm>
using namespace std;
int a,b,v[20000];
struct node
{
    int w,h;
}m[20000];
bool cmp(node a,node b)
{
    if(a.w==b.w) return a.h>b.h;
    else return a.w<b.w;
}
int f()
{
    int cnt=0;
    for(int i=0;i<b;++i)
        if(!v[i])
        {
            v[i]=1;
            ++cnt;
            int tmp=m[i].h;
            for(int j=i+1;j<b;++j)
                if(!v[j]&&tmp<m[j].h)
                {
                    v[j]=1;
                    tmp=m[j].h;
                }
        }
    return cnt;
}
int main()
{
    scanf("%d",&a);
    for(int i=0;i<a;++i)
    {
        scanf("%d",&b);
        for(int j=0;j<b;++j)
            scanf("%d %d",&m[j].w,&m[j].h),v[j]=0;
        sort(m,m+b,cmp);
        printf("%d\n",f());
    }
    return 0;
}

  二分加速(求解不上升子序列)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a,b,v[20000],len;
struct node
{
    int w,h;
}m[20000];
bool cmp(node a,node b)
{
    if(a.w==b.w) return a.h>b.h;
    else return a.w<b.w;
}
int search(int key)
{
    int a=0,b=len,t;
    while(a<b)
    {
        t=(a+b)>>1;
        if(v[t]<key) b=t;
        else a=t+1;
    }
    return a;
}
int f()
{
    len=0;
    v[0]=m[0].h;
    for(int i=1;i<b;++i)
    {
        int t=search(m[i].h);
        if(v[t]<m[i].h) v[t]=m[i].h;
        else v[++len]=m[i].h;
    }
    return len+1;
}
int main()
{
    scanf("%d",&a);
    for(int i=0;i<a;++i)
    {
        scanf("%d",&b);
        for(int j=0;j<b;++j)
            scanf("%d %d",&m[j].w,&m[j].h);
        sort(m,m+b,cmp);
        printf("%d\n",f());
    }
    return 0;
}

  

posted @ 2012-02-08 03:25  mengxm  阅读(184)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3