Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stack>
- using namespace std;
- ifstream in;
- ofstream out;
- int main(int argc, char **argv)
- {
- in.open("input.txt", ios_base::in);
- out.open("output.txt", ios_base::out);
- int N;
- in >> N;
- stack<int> st;
- long long int* a = new long long int [N];
- int* right = new int[N];
- for(int i = 0; i < N; ++i )
- right[i] = 0;
- for(int i = 0; i < N; ++i )
- in >> a[i];
- for(int i = 0; i < N; ++i )
- {
- while(!st.empty() && a[i] > a[st.top()])
- {
- right[st.top()] = i;
- st.pop();
- }
- st.push(i);
- }
- for(int i = 0; i < N; ++i )
- {
- if(right[i] != 0)
- out << a[right[i]] << endl;
- else
- out << a[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement