Codeforces 4D Mysterious Present (简单DP +路径打印 最长递增子序列问题)

彼得想要为他的澳大利亚朋友制作一条能容纳生日卡片的最大尺寸的信封链。信封链需满足每个信封的宽和高都大于前一个信封,且卡片的宽和高必须小于链中最小信封的宽和高。题目要求输出最大信封链的长度及构成这个链的信封编号。如果卡片无法放入任何信封,则输出0。
摘要由CSDN通过智能技术生成

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 a chain. Chain here is such a sequence of envelopes A = {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 follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-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.

Examples

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 
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 5005
using namespace std;

int n,w,h;
int a,b;
int d[maxn];
/*
题目大意:给定数个二元组,
其包含关系是几何上面的大小比较关系,要求出最长的序列。

按宽度排序,按要求进行最长递增子序列的操作,注意细节。
刚开始要把全部的高和宽减去初始的东西。然后超多的细节,
因为不符合要求的不能进行判定,在dp过程中要进行筛选,
把不符合要求的筛去。在回溯答案的时候也是如此。

*/

struct node
{
    int w,h;
    int id;
    node(){}
};
node seq[maxn];
bool cmp(node x,node y)
{
    if(x.w==y.w) return x.h<y.h;
    return x.w<y.w;
}

int main()
{
    scanf("%d%d%d",&n,&w,&h);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&seq[i].w,&seq[i].h);
        seq[i].w-=w,seq[i].h-=h;
        seq[i].id=i+1;
    }

    sort(seq,seq+n,cmp);

    int pre[maxn];
    for(int i=0;i<=n;i++) pre[i]=i;

    d[0]=1;
    if(seq[0].w<=0||seq[0].h<=0) d[0]=0;
    for(int i=1;i<n;i++)
    {
        if(seq[i].h<=0||seq[i].w<=0) continue;
        for(int j=0;j<i;j++)
        {
            if(seq[i].w==seq[j].w) continue;
            if(seq[j].h>=seq[i].h) continue;
            if(seq[j].h<=0||seq[j].w<=0) continue;
            if(d[j]>d[i])
            {
                pre[i]=j;
                d[i]=d[j];
            }
        }
        d[i]++;
    }


    int tp=-1,index;
    for(int i=0;i<n;i++)
    {
        if(seq[i].w<=0||seq[i].h<=0) continue;
        if(tp<d[i]) tp=d[i],index=i;
    }

    int ans[maxn],cnt=0;

    while(pre[index]!=index)
    {
        if(seq[index].w<=0 || seq[index].h<=0) break;
        ans[cnt++]=seq[index].id;
        index=pre[index];
    }

    if(seq[index].w>0&&seq[index].h>0)  ans[cnt++]=seq[index].id;

    printf("%d\n",cnt);for(int i=cnt-1;i>=0;i--) printf("%d%c",ans[i],i?' ':'\n');///puts("");

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值