dp--C - Mysterious Present

C - Mysterious Present

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 nwh (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 
 
  
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n, w, h;
 5 int maxn;//信封数 
 6 int p;//最小信封的下标 
 7 int listp[5005];
 8 int dp[5005];
 9 struct env{
10     int w, h, num;
11 }node[5005];
12 
13 bool comp(env a,env b){
14     if(a.w != b.w)
15         return a.w > b.w;
16     return a.h > b.h;//信封从大到小排序 
17 }
18 
19 int main(){
20     scanf("%d %d %d",&n, &w, &h);
21     
22     for(int i=0; i<n; i++){
23         scanf("%d %d",&node[i].w, &node[i].h);
24         node[i].num = i+1;//记录信封原下标 
25     }
26     sort(node, node+n, comp);//信封按宽从小到大排序 
27     for(int i=0; i<n; i++){//初始化 
28         dp[i] = 0;
29         listp[i] = -1;
30     }
31     maxn = 0;
32     p = -1;
33     
34     for(int i=0; i<n; i++){
35         if(node[i].w>w && node[i].h>h ){//i可以放信
36             dp[i] = 1;
37             if(p==-1){
38                 p = i;
39                 maxn = 1;
40             }    
41         }
42         
43         if(dp[i] > 0) {//i可以放信 
44             for(int j=0; j<i; j++){  
45                 if(node[j].w>node[i].w && node[j].h>node[i].h)//j可以放i 
46                     if(dp[i] < dp[j] + 1){
47                         dp[i] = dp[j] + 1;
48                         listp[i] = j;//i放进j 
49                         if(dp[i] > maxn){
50                             maxn = dp[i];
51                             p = i;
52                         }    
53                     }
54             } 
55         }
56         
57     }
58     printf("%d\n",maxn);
59     for(int i=p; maxn;){
60         if(i==-1)    break;
61         printf("%d ",node[i].num);
62         i = listp[i];
63     }
64 }
 
  

  代码不需要太多的注释,结构体node用来存放每个信封的宽和高以及它的初始位置(后用到排序,但又需要输出是第几个信封,便在结构体内标记),这里注意di一下写的comp函数,用来作为结构体排序的标准。此题难道我的是这个最长子序列到底如何记录,我最初的想法,就是再用一个数组list,list[i] = j,用来表示第node[i].num个信封可以放到第node[j].num个信封里,但由于我一开始定义的是从小到大的函数,所以信封也是从小指到大的;而在这类dp题中,我只能在最后找到那个最长子序列的时候,记录下最大信封的下标,而这个从大返回指小的过程,真的把我难倒了,我有尝试再用一个数组反向指,都失败了。但其实我后来的改进很简单,只要将这些信封,从大到小排序,从最大的,一直排到最小的,并且是能放入卡片的序列最长的信封,再配合一开始想到的list的信封值向信封的方法,最后可实现从小信封,输出到大信封

 
 

猜你喜欢

转载自www.cnblogs.com/0424lrn/p/12210888.html