[λ°±μ€,c++] 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_base::sync_with_stdio(false);
cin.tie(0);
int N; cin>>N;
stack<int>st;
vector<char> ans;
int curr = 0;
while(N--){
int num; cin>>num;
if(st.empty()){
if(curr>num){
cout<<"NO";
return 0;
}
int first = curr;
for(int i=0; i<abs(num-first); i++){
ans.push_back('+');
st.push(++curr);
}
ans.push_back('-');
st.pop();
}
else if(st.top()==num){
ans.push_back('-');
st.pop();
}
else if(st.top()>num){
while(true){
if(st.top()==num) break;
ans.push_back('-');
st.pop();
}
}
else if(st.top()<num){
if(curr>num){
cout<<"NO";
return 0;
}
while(true){
if(st.top()==num){
ans.push_back('-');
st.pop();
break;
}
ans.push_back('+');
st.push(++curr);
}
}
}
for(char c: ans) cout<<c<<'\n';
}
νμ΄
μ½λ ꡬν μ체λ μ΄λ ΅μ§ μμΌλ μ΄ λ¬Έμ λ₯Ό μ²μ 보μμ λ λ¬Έμ μμ²΄κ° μ΄ν΄κ° κ°μ§λ₯Ό μμμ΅λλ€. μ μΆλ ₯μ λ³΄κ³ λμΆ© μμμ ν΄μ νμ΄λ³΄μμ§λ§ 무μΈκ° μλͺ»λμλμ§ μ±μ νμ λλ μ€λ΅μ΄ λ΄κ³ κ·Έλ κ² λ―Έμ³κ°λ λμ€ μ§λ¬Έ κ²μνμμ μλ λ¬Έμ μ€λͺ μμμ μ°Ύμμ λ³΄κ² λμμ΅λλ€.
μ μμμ΄ μμμΌλ©΄ μμ§λ λ¬Έμ μ΄ν΄μ‘°μ°¨ λͺ»νμ κ²μ λλ€.(μ μμ λΆκ» κ°μ¬λ립λλ€.) λ¬Έμ μ΄ν΄κ° κ°μ§ μμΌμ λΆλ€μ νλ² μμ²νμλ©΄ μ΄ν΄κ° λ°λ‘ λ κ² κ°μ΅λλ€.