๐Ÿ‹ ๋ฌธ์ œ๋งํฌ


https://www.acmicpc.net/problem/1874

๐Ÿ‰ Code


๋ฉ”๋ชจ๋ฆฌ : 143944 KB

์‹œ๊ฐ„ : 32 ms


N = int(input())
arr=[]
result=[]
temp=[]

for _ in range(N):
    arr.append(int(input()))

j = 0
for i in range(1, N+1):
    temp.append(i)
    result.append('+')
    while (temp and temp[-1] == arr[j]):
        temp.pop()
        j += 1
        result.append('-')

if not temp: # ๋น„์–ด ์žˆ์œผ๋ฉด
    for i in result:
        print(i)
else:
    print("NO")

๐Ÿ’ข ์‹œ๊ฐ„์ดˆ๊ณผ

#include <iostream>
#include <vector>

int N;
std::vector<int> arr;
std::vector<char> result;
std::vector<int> temp;

void input_faster()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
	std::cin >> N;
    for(int i = 0 ; i < N ; i++){
        int a;
        std::cin >> a;
        arr.push_back(a);
    }
}

void solve()
{
    int j = 0;
    for (int i = 1 ; i < N + 1 ; i++){
        temp.push_back(i);
        result.push_back('+');
        while (!temp.empty() && temp.back() == arr[j]){
            temp.pop_back();
            j++;
            result.push_back('-');
        }
    }
}

void print_val()
{
    if (temp.empty()){
        for (int i = 0 ;i < result.size();i++)
            std::cout << result[i] << std::endl;
    }
    else
        std::cout << "NO";
}

int main()
{
	input_faster();
	input();
	solve();
	print_val();
	return (0);
}

๐Ÿฅ ๋ฉ”๋ชจ