Advertisement
sve_vash

Untitled

Sep 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <fstream>
  4.  
  5. struct num {
  6. int value, index;
  7. };
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12. int n;
  13. ifstream inp("input.txt");
  14. inp >> n;
  15. num a[n];
  16. stack<num> st;
  17.  
  18. for (int i = 0; i < n; ++i) {
  19. inp >> a[i].value;
  20. a[i].index = i;
  21. if (st.empty() || st.top().value > a[i].value) {
  22. st.push(a[i]);
  23. }
  24. else {
  25. while (!st.empty() && st.top().value < a[i].value) {
  26. a[st.top().index].value = a[i].value;
  27. st.pop();
  28. }
  29. st.push(a[i]);
  30. }
  31. }
  32. inp.close();
  33. ofstream out("output.txt");
  34. for (int i = 0; i < n; ++i) {
  35. out << a[i].value << ' ';
  36. }
  37. out.close();
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement