fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Rectangle {
  8. int h, w, index;
  9. };
  10.  
  11. // Compare function for sorting
  12. bool compare(const Rectangle &a, const Rectangle &b) {
  13. if (a.h == b.h) return a.w < b.w;
  14. return a.h < b.h;
  15. }
  16.  
  17. int main() {
  18. int n;
  19. cin >> n;
  20.  
  21. vector<Rectangle> rects(n);
  22.  
  23. for (int i = 0; i < n; i++) {
  24. int h, w;
  25. cin >> h >> w;
  26. if (h > w) swap(h, w); // Ensure h ≤ w
  27. rects[i] = {h, w, i + 1}; // Store original index
  28. }
  29.  
  30. // Sort rectangles
  31. sort(rects.begin(), rects.end(), compare);
  32.  
  33. // LIS on widths
  34. vector<int> dp(n, 1), prev(n, -1);
  35. int max_len = 1, last_index = 0;
  36.  
  37. for (int i = 0; i < n; i++) {
  38. for (int j = 0; j < i; j++) {
  39. if (rects[j].w < rects[i].w) { // Only compare widths
  40. if (dp[j] + 1 > dp[i]) {
  41. dp[i] = dp[j] + 1;
  42. prev[i] = j;
  43. }
  44. }
  45. }
  46. if (dp[i] > max_len) {
  47. max_len = dp[i];
  48. last_index = i;
  49. }
  50. }
  51.  
  52. // Retrieve the sequence
  53. vector<int> sequence;
  54. for (int i = last_index; i != -1; i = prev[i]) {
  55. sequence.push_back(rects[i].index);
  56. }
  57.  
  58. reverse(sequence.begin(), sequence.end());
  59.  
  60. // Output results
  61. cout << max_len << endl;
  62. for (int i : sequence) cout << i << " ";
  63. cout << endl;
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0.02s 5288KB
stdin
Standard input is empty
stdout
1
3517