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
2 1 1 2 2 2 2
1 1
3 3 3 5 4 12 11 9 8
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;
}