fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. int n;
  6. int a[100005], f[100005];
  7.  
  8. bool cmp(int a, int b){
  9. return (a>b);
  10. }
  11.  
  12. int main(){
  13. cin >> n;
  14. for (int i = 1;i <= n; i++){
  15. cin >> a[i];
  16. //b[i] = a[i];
  17. }
  18. // sort(a+1, a+n+1, cmp);
  19. //string s="";
  20. int j = n;
  21. for (int i = 1; i <= n; i++){
  22. if (a[i]!=j) f[a[i]] = 1;
  23. else{
  24. cout << j << " ";
  25. j--;
  26. while(f[j]){
  27. cout << j << " ";
  28. j--;
  29. }
  30. }
  31. cout << endl;
  32. }
  33. return 0;
  34. }
Success #stdin #stdout 0s 16016KB
stdin
3
3 1 2
stdout
3 

2 1