Advertisement
kokokozhina

dynamic_4

Mar 22nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4.  
  5. using namespace std;
  6.  
  7. struct stack{
  8.     int inf;
  9.     stack* next;
  10. };
  11.  
  12. stack *init(){
  13.     return NULL;
  14. }
  15.  
  16. void push(stack *&h, int x){
  17.     stack *top = new stack();
  18.     top->inf = x;
  19.     top->next = h;
  20.     h = top;
  21. }
  22.  
  23. int top(stack *&h){
  24.     return h->inf;
  25. }
  26.  
  27. int pop(stack *&h){
  28.     int i = h->inf;
  29.     stack *cur = h;
  30.     h=h->next;
  31.     delete cur;
  32.     return i;
  33. }
  34.  
  35. int main()
  36. {
  37. #ifdef _DEBUG
  38.     freopen("input.txt", "r", stdin);
  39.     freopen("output.txt", "w", stdout);
  40. #endif
  41.  
  42.     int n, k;//k is new element
  43.     scanf("%d%d", &n, &k);
  44.  
  45.     stack *a = init();
  46.     stack *res = init();
  47.  
  48.     int mn = 2000000001;
  49.     for(int i = 0; i < n; i++){
  50.         int cur; scanf("%d", &cur);
  51.         push(a, cur);
  52.         if(cur < mn) mn = cur;
  53.     }
  54.     while(a)
  55.     {
  56.         int cur = pop(a);
  57.         push(res, cur);
  58.         if(cur == mn) push(res, k);
  59.     }
  60.     while(res)
  61.         printf("%d ", pop(res));
  62.    
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement