fork download
  1. #include <iostream>
  2.  
  3. #include <cstdio>
  4. #include <vector>
  5. #include <stack>
  6. #include <utility>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10. static bool cmp(const pair<int,int> &a, const pair<int,int> &b)
  11. {
  12. return a.first < b.first;
  13. }
  14. int main()
  15. {
  16. int arr_[] = {2225, 1729 ,1835, 951, 1143, 515, 1525, 743, 1025, 1611, 1827, 2203, 1116, 1514, 723};
  17. int dep_[] = {2231, 2003, 2149, 2252, 2352, 2153, 1625, 1049, 1337, 1639, 2151, 2330, 1633, 1611 ,2009};
  18. // Your code here
  19.  
  20.  
  21. stack<int> s;
  22. int n = 15;
  23. vector<int> arr;
  24. vector<int> dep;
  25. for(int i=0;i<n;i++)
  26. {
  27. arr.push_back(arr_[i]);
  28. dep.push_back(dep_[i]);
  29. }
  30. vector< pair<int,int> > v;
  31.  
  32. for(int i=0;i<n;i++)
  33. {
  34. v.push_back({arr[i], dep[i]});
  35. }
  36. sort(v.begin(),v.end(),cmp);
  37.  
  38.  
  39. /* for(int i=0;i<n;i++)
  40.   {
  41.   printf("%5d,", v[i].first);
  42.   }
  43.   printf("\n");
  44.   for(int i=0;i<n;i++)
  45.   {
  46.   printf("%5d,",v[i].second);
  47.  
  48.   }
  49.   printf("\n\n");
  50.  
  51.   */
  52. // stack<int> s;
  53. s.push(v[0].second);
  54. int mx =1;
  55. for(int i=1;i<n;i++)
  56. {
  57.  
  58. while(!s.empty() && s.top() <= v[i].first)
  59. {
  60.  
  61. s.pop();
  62. }
  63. stack<int> tmp;
  64. while(!s.empty() && v[i].second > s.top())
  65. {
  66. tmp.push(s.top());
  67. s.pop();
  68.  
  69. }
  70.  
  71. s.push(v[i].second);
  72.  
  73. while(!tmp.empty())
  74. {
  75. s.push(tmp.top());
  76. tmp.pop();
  77. }
  78. mx = max(mx, (int)(s.size()));
  79.  
  80.  
  81. stack<int> z = s;
  82. while(!z.empty())
  83. {
  84. printf("%d,",z.top());
  85. z.pop();
  86. }
  87. printf("\n\n");
  88. // s.push(v[i].second);
  89. }
  90. return 0;
  91. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
2009,2153,

1049,2009,2153,

1049,2009,2153,2252,

1049,1337,2009,2153,2252,

1337,1633,2009,2153,2252,

1337,1633,2009,2153,2252,2352,

1611,1633,2009,2153,2252,2352,

1611,1625,1633,2009,2153,2252,2352,

1625,1633,1639,2009,2153,2252,2352,

2003,2009,2153,2252,2352,

2003,2009,2151,2153,2252,2352,

2003,2009,2149,2151,2153,2252,2352,

2252,2330,2352,

2231,2252,2330,2352,