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  the using  namespace STD;
 . 3  
. 4  int n-, W, H;
 . 5  int MAXN; // number of envelopes 
. 6  int P; // minimum envelope index 
. 7  int listp [ 5005 ];
 . 8  int DP [ 5005 ];
 . 9  struct the env {
 10      int W, H, NUM;
 . 11 } Node [ 5005 ];
 12 is  
13 is  BOOL CoMP (A the env, the env B) {
 14      IF (AW =! BW)
15          return AW> BW;
 16      return AH> BH; // envelope descending order 
. 17  }
 18 is  
. 19  int main () {
 20 is      Scanf ( " % D% D% D " , & n-, & W, & H);
 21 is      
22 is      for ( int I = 0 ; I <n-; I ++ ) {
 23 is          Scanf ( " % D% D " , & Node [I] .W, & Node [I] .h);
 24          Node [I] .num = I + 1 ; // record the original envelope subscript 
25      }
 26     Sort (Node, + n-Node, CoMP); // envelope width from small to large press 
27      for ( int I = 0 ; I <n-; I ++) { // Initialization 
28          DP [I] = 0 ;
 29          listp [I ] = - . 1 ;
 30      }
 31 is      MAXN = 0 ;
 32      P = - . 1 ;
 33 is      
34 is      for ( int I = 0 ; I <n-; I ++ ) {
 35          IF (Node [I] .W> W && Node [I] .h> H) { // I can discharge channel 
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 }
 
  

  Code does not need much comments, node structure for storing width and height of each envelope, and its original position (sorted after use, but needs to output the first few envelopes, marking it in the structure), where Note di write about the comp function, used as a sort of standard structure. This Is My problem is that the longest sequence in the end how to record, my initial idea is to reuse an array list, list [i] = j, used to represent the first node [i] .num envelopes can be placed in the first node [j] .num one envelope, but because I started the definition of small to large functions, it is also a small envelope refers to large; and in such dp question, I can only find the longest sequence in the final when recording the maximum envelope index, and this process of return refers to small from the big, really baffled me, then I have to try an array of reverse means we have failed. But in fact I was the improvement is very simple, as long as these envelopes, sorted in descending order, from the largest, has been discharged to a minimum, and is the longest sequence can be placed in the envelope of the card, and then began to think of the list with a the envelope value of the envelope of the method, which may eventually from small envelopes, to output large envelope

 
 

Guess you like

Origin www.cnblogs.com/0424lrn/p/12210888.html
Recommended