728x90
반응형
c++에 있는 stack을 활용
시간 복잡도는 O(n)으로 상당히 좋은 코드
#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <stack>
#include <unordered_set>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N;
cin >> N;
vector<int> ans(N);
for (int i = 0; i < N; i++)
cin >> ans[i];
stack<int> st;
string push_and_pop;
int current_number = 1;
for (int i = 0; i < N; i++)
{
int check = ans[i];
while (current_number <= check)
{
st.push(current_number);
push_and_pop += '+';
push_and_pop += '\n';
current_number++;
}
if (st.top() == check)
{
st.pop();
push_and_pop += '-';
push_and_pop += '\n';
}
else
{
cout << "NO" << endl;
return 0;
}
}
cout << push_and_pop;
return 0;
}
728x90
반응형
'Baekjoon > Silver' 카테고리의 다른 글
Baekjoon 1764.듣보잡 c++[Silver IV] (0) | 2024.12.18 |
---|---|
Baekjoon 11723.집합 c++ [Silver V] (0) | 2024.12.13 |
Baekjoon 2839.ŠEĆER c++ [Silver IV] (1) | 2024.12.09 |
Baekjoon 1920.수 찾기 c++ [Silver V] (0) | 2024.12.09 |
Baekjoon 11651. 좌표 정렬하기 c++ [Silver V] (0) | 2024.11.28 |