-
Notifications
You must be signed in to change notification settings - Fork 0
/
오큰수.cpp
36 lines (31 loc) · 854 Bytes
/
오큰수.cpp
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
#include <bits/stdc++.h>
int arr[1000000];
int NGE[1000000];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::stack<int> tmxor;
int n;
std::cin >> n;
for (int i=0; i<n; i++)
std::cin >> arr[i];
for (int i=n-1; i>=0; i--){
if (!tmxor.empty()) {
if (tmxor.top() > arr[i]){
NGE[i] = tmxor.top();
}
else { // tmxor.top() <= arr[i]
while (!tmxor.empty() && tmxor.top() <= arr[i])
tmxor.pop();
if (tmxor.empty()) NGE[i] = -1;
else NGE[i] = tmxor.top();
}
}
tmxor.push(arr[i]);
}
for (int i=0; i<n; i++){
if (!NGE[i]) std::cout << -1 << ' ';
else std::cout << NGE[i] << ' ';
}
}