#include <iostream> #include <vector> #include <stack> #include <queue> #include <math.h> #include <string> using namespace std; int N; int tower[500010]; int result[500010]; stack<pair<int, int>> st; int main(void) { // freopen("B2493_input.txt", "r", stdin); cin >> N; for(int i = 1; i <= N; i++) { cin >> tower[i]; } for(int i = N; i >= 1; i--) { if(st.empty()) { st.push({tower[i], i}); continue; } else { while(!st.empty() && st.top().first < tower[i]) { result[st.top().second] = i; st.pop(); } st.push({tower[i], i}); } } for(int i = 1; i <= N; i++) { cout << result[i] << " "; } return 0; } | cs |
Baekjoon/Stack
- [백준 2493] 탑 (Stack) (C/C++) (★) 2020.04.09
[백준 2493] 탑 (Stack) (C/C++) (★)
2020. 4. 9. 18:45