-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNGECirculated.cpp
More file actions
48 lines (41 loc) · 961 Bytes
/
NGECirculated.cpp
File metadata and controls
48 lines (41 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
vector<int> NGECirc(const vector<int> &nums) {
int n = nums.size();
vector<int> nge(n, -1);
vector<int> extendedArr(2 * n);
for (int i = 0; i < n; i++) {
extendedArr[i] = nums[i];
extendedArr[i + n] = nums[i];
}
stack<int> st;
for (int i = (2 * n) - 1; i >= 0; i--) {
while (!st.empty() && st.top() <= extendedArr[i]) {
st.pop();
}
if (i < n) {
if (!st.empty()) {
nge[i % n] = st.top();
}
}
st.push(extendedArr[i]);
}
return nge;
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<int> nums(n);
for (int &i : nums) {
cin >> i;
}
vector<int> ans = NGECirc(nums);
for (int &a : ans) {
cout << a << " ";
}
return 0;
}