Codeforces#4D Mysterious Present

题意给你个名片的大小,和n个信封的大小,然后问你最多能包装多少层,最长上升序列的问题
#include <cstdio>  
#include <string>  
#include <queue>  
#include <iostream>  
#include <algorithm>  
#include <cstring>  
#include <vector>  
#include <cmath>  
#define ll __int64  
using namespace std;  
#define lson l,m,rt<<1  
#define rson m+1,r,rt<<1|1  
struct node  
{  
    int id;  
    int w,h;  
}a[5050];  
int pre[5050],dp[5050];  
bool cmp(node x,node y)  
{  
    if(x.w!=y.w)  
       return x.w<y.w;  
    return x.h<y.h;  
}  
void dfs(int x)  
{  
    if(pre[x]!=-1)  
       dfs(pre[x]);  
    printf("%d ",a[x].id);  
}  
int main()  
{  
    int n,W,H;  
    scanf("%d%d%d",&n,&W,&H);  
    int cnt=0;  
    for(int i=1;i<=n;i++)  
    {  
        int ww,hh;  
        scanf("%d%d",&ww,&hh);  
        if(ww>W&&hh>H)  
        {  
            a[++cnt].w=ww;  
            a[cnt].h=hh;  
            a[cnt].id=i;  
        }  
    }  
    if(cnt==0)  
    {  
        puts("0");  
        return 0;  
    }  
    sort(a+1,a+cnt+1,cmp);  
    for(int i=0;i<=cnt;i++)  
        pre[i]=-1;  
    int maxnum=1,maxpos=1;  
    dp[1]=1;  
    for(int i=2;i<=cnt;i++)  
    {  
        int ma=0,pos=-1;//ma要赋值成0  
        for(int j=i-1;j>=1;j--)  
            if(a[i].w>a[j].w&&a[i].h>a[j].h&&dp[j]>ma)  
            {  
                ma=dp[j];  
                pos=j;  
            }  
        pre[i]=pos;  
        dp[i]=ma+1;  
        if(dp[i]>maxnum)  
        {  
            maxnum=dp[i];  
            maxpos=i;  
        }  
    }  
      
    printf("%d\n",maxnum);  
    if(pre[maxpos]!=-1)  
      dfs(pre[maxpos]);  
    printf("%d\n",a[maxpos].id);  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值