Algorithm
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.
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).
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.
2 1 1
2 2
2 2
1
1
3 3 3
5 4
12 11
9 8
3
1 3 2
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <vector>
#include <algorithm>
struct envelope{long order; long width; long height;};
bool widthSort(envelope A, envelope B){return A.width < B.width;}
int main(){
long n, cardWidth, cardHeight; scanf("%ld %ld %ld", &n, &cardWidth, &cardHeight);
std::vector<envelope> envVec;
for(long k = 0; k < n; k++){
long w, h; scanf("%ld %ld\n", &w, &h);
if(w <= cardWidth || h <= cardHeight){continue;}
envelope current; current.order = k + 1; current.width = w; current.height = h;
envVec.push_back(current);
}
std::sort(envVec.begin(), envVec.end(), widthSort);
long totalEnv(envVec.size());
long maxLength(totalEnv > 0), maxFinal(0);
std::vector<long> endingLength(totalEnv, maxLength);
std::vector<long> previous(totalEnv, 0);
for(long p = 0; p < totalEnv; p++){
for(long q = p + 1; q < totalEnv; q++){
if((envVec[p].width < envVec[q].width) && (envVec[p].height < envVec[q].height) && (endingLength[q] < endingLength[p] + 1)){
previous[q] = p;
endingLength[q] = endingLength[p] + 1;
if(endingLength[q] > maxLength){maxLength = endingLength[q]; maxFinal = q;}
}
}
}
printf("%ld\n", maxLength);
std::vector<long> usedEnv(maxLength, 0);
long current = maxFinal; for(long k = 0; k < maxLength; k++){usedEnv[maxLength - k - 1] = envVec[current].order; current = previous[current];}
for(long k = 0; k < maxLength; k++){printf("%ld ", usedEnv[k]);} puts("");
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 2
2 2
Output
1
Demonstration
Codeforces Solution-D. Mysterious Present-Solution in C, C++, Java, Python