스택과 큐 자료구조에 대해 이해하고 있어야 하는 문제입니다.
한쪽 길은 큐, 다른 한쪽 길은 스택의 성질을 가지고 있다는 것을 알고 있다면 해결할 수 있습니다.
#include <iostream>
int main()
{
int n;
std::cin >> n;
int queue_1_top = n - 1;
int stack_2_top = -1;
int* queue_1 = new int[n];
int* stack_2 = new int[n];
for (int i = n - 1; i >= 0; i--)
{
std::cin >> queue_1[i];
stack_2[i] = 0;
}
int min_num = 1;
while(queue_1_top != -1)
{
if (stack_2[stack_2_top] == min_num)
{
min_num++;
stack_2[stack_2_top] = -1;
stack_2_top--;
}
else if (queue_1[queue_1_top] != min_num)
{
stack_2_top++;
stack_2[stack_2_top] = queue_1[queue_1_top];
queue_1[queue_1_top] = -1;
queue_1_top--;
}
else
{
min_num++;
queue_1[queue_1_top] = -1;
queue_1_top--;
}
}
bool result = true;
while (stack_2_top != -1)
{
if (stack_2[stack_2_top] == min_num)
{
min_num++;
stack_2[stack_2_top] = -1;
stack_2_top--;
}
else
{
result = false;
break;
}
}
if (result)
std::cout << "Nice";
else
std::cout << "Sad";
return 0;
}