fork(1) download
  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. vector<vector<int> > Solve (vector<int> arr) {
  6. // Write your code here
  7. vector<vector<int>> result;
  8.  
  9. vector<int> temp, temp1;
  10. //vector<int> temp1;
  11. //queue<int> q;
  12. int max=0;
  13. int v_count=0, temp_count;
  14. max = *max_element(arr.begin(), arr.end());
  15.  
  16. for(int i=0; i<arr.size(); i++){
  17. if(arr[i] == max){
  18. temp.push_back(arr[i]);
  19. sort(temp.begin(), temp.end());
  20. v_count++;
  21. vector<int> back_temp = temp;
  22. temp_count = v_count;
  23. int j=0;
  24. while(v_count){
  25. j = temp.size();
  26. //cout << "max = " << temp[j-1] << " j value is = " << j << endl;
  27. if(max == temp[j-1]){
  28. temp1.push_back(temp[j-1]);
  29. temp.pop_back();
  30. v_count--;
  31. max--;
  32. }
  33. else {
  34. temp1.clear();
  35. temp = back_temp;
  36. v_count = temp_count;
  37. break;
  38. }
  39. }
  40. }
  41. else{
  42. temp.push_back(arr[i]);
  43. sort(temp.begin(), temp.end());
  44. v_count++;
  45. }
  46.  
  47. result.push_back(temp1);
  48. temp1.clear();
  49. }
  50. return result;
  51. }
  52.  
  53. int main() {
  54.  
  55. ios::sync_with_stdio(0);
  56. cin.tie(0);
  57. int N;
  58. cin >> N;
  59. vector<int> arr(N);
  60. for (int i_arr = 0; i_arr < N; i_arr++) {
  61. cin >> arr[i_arr];
  62. }
  63.  
  64. vector<vector<int> > out_ = Solve(arr);
  65. for (int i_out_ = 0; i_out_ < out_.size(); i_out_++) {
  66. for (int j_out_ = 0; j_out_ < out_[i_out_].size(); j_out_++) {
  67. cout << out_[i_out_][j_out_] << " ";
  68. }
  69. cout << "\n";
  70. }
  71. }
Success #stdin #stdout 0s 4516KB
stdin
5
4 5  2  1 3
stdout
5 4 


3 2 1