C - Mysterious Present(DP求最长上升子序列)

C -Mysterious Present
Crawling in process... Crawling failed
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make achain. Chain here is such a sequence of envelopesA = {a1,  a2,  ...,  an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the(i  -  1)-th envelope respectively. Chain size is the number of envelopes in the chain.

Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.

Peter has very many envelopes and very little time, this hard task is entrusted to you.

Input

The first line contains integers n, w, h (1  ≤ n ≤ 5000,1 ≤ w,  h  ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follown lines, each of them contains two integer numberswi andhi — width and height of thei-th envelope (1 ≤ wi,  hi ≤ 106).

Output

In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.

If the card does not fit into any of the envelopes, print number 0 in the single line.

Sample Input

Input
2 1 1
2 2
2 2
Output
1
1 
Input
3 3 3
5 4
12 11
9 8
Output
3
1 3 2 

题目大意:

Peter有n个信封每个信封有一定的长(h)和宽(w),每个信封都有一个从1到n的编号。他想用这些信封做成一个链,要求这个链中的第i个信封的长和宽要大于第i-1个信封的长和宽。而且他要在每个信封里装一个长宽分别为H、W的贺卡,要求每个信封的长宽都要大于贺卡的长和宽。求这个链的最大长度?并输出这个链上的每个信封的编号。

解题思路:

用DP求最长上升子序列。

代码如下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 5005
#define INF 0x3f3f3f3f
using namespace std;

struct node
{
    int w,h,d;
}env[MAXN];

long long dp[MAXN];
int num[MAXN],tmp[MAXN];
int n,W,H;

bool cmp(node a,node b)
{
    if(a.w==b.w&&b.h==a.h)
        return a.d<b.d;
    if(a.w==b.w)
        return a.h<b.h;
    return a.w<b.w;
}

int main()
{
    int i,j,k=0,ans=0;
    scanf("%d%d%d",&n,&W,&H);
    for(i=0;i<n;i++)
    {
        scanf("%d%d",&env[i].w,&env[i].h);
        env[i].d=i+1;
    }
    sort(env,env+n,cmp);
    memset(dp,0,sizeof(dp));
    memset(num,-1,sizeof(num));
    bool mark=false;
    for(i=0;i<n;i++)
    {
        if(env[i].w>W&&env[i].h>H)
        {
            mark=true;
            dp[i]=1;
            if(ans<dp[i])
            {
                ans=dp[i];
                k=i;
            }
            for(j=0;j<i;j++)
            {
                if(env[i].w>env[j].w&&env[i].h>env[j].h)
                {
                    if(dp[i]<dp[j]+1)
                    {
                        dp[i]=dp[j]+1;
                        num[i]=j;
                        if(ans<dp[i])
                        {
                            ans=dp[i];
                            k=i;
                        }
                    }
                }
            }
        }
    }
    if(!mark)
    {
        printf("0\n");
        return 0;
    }
    int cnt=0;
    tmp[cnt++]=env[k].d;
    while(num[k]!=-1)
    {
        tmp[cnt++]=env[num[k]].d;
        k=num[k];
    }
    printf("%d\n",ans);
    for(i=cnt-1;i>0;i--)
        printf("%d ",tmp[i]);
    printf("%d\n",tmp[0]);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值