Advertisement
Guest User

Untitled

a guest
May 27th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. ifstream in;
  8. ofstream out;
  9.  
  10.  
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.  
  15. in.open("input.txt", ios_base::in);
  16. out.open("output.txt", ios_base::out);
  17. int N;
  18. in >> N;
  19. stack<int> st;
  20. long long int* a = new long long int [N];
  21. int* right = new int[N];
  22.  
  23. for(int i = 0; i < N; ++i )
  24. right[i] = 0;
  25.  
  26. for(int i = 0; i < N; ++i )
  27. in >> a[i];
  28.  
  29.  
  30. for(int i = 0; i < N; ++i )
  31. {
  32. while(!st.empty() && a[i] > a[st.top()])
  33. {
  34. right[st.top()] = i;
  35. st.pop();
  36. }
  37. st.push(i);
  38. }
  39. for(int i = 0; i < N; ++i )
  40. {
  41. if(right[i] != 0)
  42. out << a[right[i]] << endl;
  43. else
  44. out << a[i] << endl;
  45. }
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement