当前位置: 代码迷 >> 综合 >> Codeforces 4D Mysterious Present (简单DP +路径打印 最长递增子序列问题)
  详细解决方案

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

热度:88   发布时间:2023-11-15 16:28:10.0

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;
}

 

  相关解决方案