Algorithm
[BOJ] 1874 스택 수열
일등하이
2023. 1. 16. 02:21
반응형
https://www.acmicpc.net/problem/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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BOJ
{
class Program
{
static void Main()
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
List<int> list = new List<int>();
int number = 0;
Stack<int> stack = new Stack<int>();
int a = int.Parse(sr.ReadLine());
for (int i = 0; i < a; i++)
{
list.Add(int.Parse(sr.ReadLine()));
}
for (int i = 1; i <= a; i++)
{
stack.Push(i);
sb.Append("+" + "\n");
while (stack.Count != 0 && stack.Peek() == list[number])
{
stack.Pop();
sb.Append("-" + "\n");
number++;
}
}
if (stack.Count == 0)
{
sw.WriteLine(sb);
}
else
{
sw.WriteLine("NO");
}
sr.Close();
sw.Flush();
sw.Close();
}
}
}
반응형