728x90
반응형
https://www.acmicpc.net/problem/1874
1874번: 스택 수열
1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.
www.acmicpc.net
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
int arr[100000];
for(int i = 0; i < n; i++){
cin >> arr[i];
}
vector<char> ans;
stack<int> st;
st.push(1);
int num = 2;
int i = 0;
ans.push_back('+');
while(i < n){
if(st.empty() || st.top() < arr[i]){
st.push(num);
num++;
ans.push_back('+');
} else if(st.top() == arr[i]){
st.pop();
ans.push_back('-');
i++;
} else{
cout << "NO";
return 0;
}
}
for(int i = 0; i < ans.size(); i++){
cout << ans[i] << '\n';
}
return 0;
}
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[BOJ] 13549번 숨바꼭질 3 (C++) (0) | 2022.09.05 |
---|---|
[BOJ] 2109번 순회강연 (C++) (0) | 2022.08.21 |
[BOJ] 2573번 빙산 (C++) (0) | 2022.08.17 |
[BOJ] 13904번 과제 (C++) (0) | 2022.08.16 |
[BOJ] 2665번 미로 만들기 (C++) (0) | 2022.08.15 |