fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. template <typename typ>
  5. class stack{
  6. typ *tab;
  7. long wybrany, size_in;
  8. void powiekszSie();
  9. public:
  10. stack();
  11. ~stack(){delete []tab;}
  12. int size() {return wybrany;}
  13. void push(const typ &co);
  14. void pop() {wybrany --;}
  15. bool empty() {return wybrany == 0;}
  16. const typ top(){return tab[wybrany - 1];}
  17. };
  18. template <typename typ>
  19. stack<typ>::stack(){
  20. wybrany = 0;
  21. size_in = 0;
  22. tab = new typ[size_in];
  23. }
  24. template <typename typ>
  25. void stack<typ>::push(const typ &co){
  26. if(wybrany == size_in)
  27. powiekszSie();
  28. tab[wybrany++] = co;
  29. }
  30. template <typename typ>
  31. void stack<typ>::powiekszSie(){
  32. typ *nowa = new typ[size_in + 10];
  33. for(int i=0;i<size_in;++i)
  34. nowa[i] = tab[i];
  35. delete []tab;
  36. tab = nowa;
  37. size_in += 10;
  38. }
  39.  
  40. void func(int *a, int n){
  41. stack <int> S, P;
  42. for(int i=0; i<n; i++) S.push(a[i]);
  43.  
  44. for(int i=0, k=0, min; i<n; i++, k++){
  45. min = S.top();
  46. S.pop();
  47. while(k!=S.size()){
  48. if(min > S.top()){
  49. P.push(min);
  50. min = S.top();
  51. }
  52. else
  53. P.push(S.top());
  54. S.pop();
  55. }
  56. for(S.push(min); !P.empty(); P.pop()){ int x=P.top(); S.push(x);}
  57. }
  58. while (!S.empty()){
  59. cout<<S.top()<<" ";
  60. S.pop();
  61. }
  62. }
  63.  
  64. int main(){
  65. int n, *a;
  66. cin>>n;
  67. a = new int[n];
  68. for(int i=0, j; i<n; i++)
  69. cin>>a[i];
  70. func(a, n);
  71. return 0;
  72. }
  73.  
  74.  
Success #stdin #stdout 0s 2964KB
stdin
10
1 4 5 7 3 66 3 4 9 2
stdout
66 9 7 5 4 4 3 3 2 1