알고리즘/백준 BOJ
[ BOJ/C# ] 12789 도키도키 간식드리미
왹져박사
2023. 11. 1. 00:32
https://www.acmicpc.net/problem/12789
12789번: 도키도키 간식드리미
인하대학교 학생회에서는 중간, 기말고사 때마다 시험 공부에 지친 학우들을 위해 간식을 나눠주는 간식 드리미 행사를 실시한다. 승환이는 시험 기간이 될 때마다 간식을 받을 생각에 두근두
www.acmicpc.net
Stack을 이용하여 푸는 문제이다.
고려할 점은, 줄 순서로 들어간 경우에도 대기열을 검사해주어야 한다는 점이다.
또, 생각 없이 stack.Count로 for문을 만들어 오답이 많이 났다..
using System;
using System.IO;
namespace B12789
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int n = int.Parse(sr.ReadLine());
int[] students = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
Stack<int> stack = new Stack<int>();
int index = 1;
for(int i = 0; i < n; i++)
{
if (students[i] == index)
{
index++; //번호 순서가 맞음
if (stack.Count > 0) //중간 검사
{
while(stack.Count > 0)
{
int pop = stack.Pop();
if (pop == index) index++;
else
{
stack.Push(pop);
break;
}
}
}
}
else stack.Push(students[i]); //아니면 대기열로
}
bool result = true;
if(stack.Count > 0) //줄이 사라지고 대기열만 남았을 경우
{
while (stack.Count > 0)
{
int pop = stack.Pop();
if (pop == index) index++;
else
{
result = false;
break;
}
}
}
if (result) sw.Write("Nice");
else sw.Write("Sad");
sr.Close();
sw.Flush();
sw.Close();
}
}
}