[BOJ] 10866 덱
Algorithm 2023. 1. 17. 22:52반응형
https://www.acmicpc.net/problem/10866
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Deque
{
internal class Program
{
static int[] deque = new int[10001];
static int cnt;
static bool isEmpty;
public static int front= deque.Length / 2, back= (deque.Length / 2)+1;
public static void Main(string[] args)
{
StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));
StringBuilder sb = new StringBuilder();
int rep = int.Parse(sr.ReadLine());
for (var i = 0; i < rep; i++)
{
string[] input = sr.ReadLine().Split();
switch (input[0])
{
case "push_front":
pushFront(int.Parse(input[1]));
break;
case "push_back":
pushBack(int.Parse(input[1]));
break;
case "pop_front":
sb.AppendLine(popFront().ToString());
break;
case "pop_back":
sb.AppendLine(popBack().ToString());
break;
case "size":
sb.AppendLine(size().ToString());
break;
case "empty":
sb.AppendLine(empty().ToString());
break;
case "front":
sb.AppendLine(Front().ToString());
break;
case "back":
sb.AppendLine(Back().ToString());
break;
}
}
sw.WriteLine(sb.ToString());
sw.Close();
sr.Close();
}
static void pushFront(int x)
{
deque[front] = x;
cnt++;
if (front == 0)
{
front = deque.Length - 1;
}
else
{
front--;
}
}
static void pushBack(int x)
{
deque[back] = x;
cnt++;
if (back == deque.Length - 1)
{
back = 0;
}
else
{
back++;
}
}
static int popFront()
{
if (cnt == 0)
{
return -1;
}
int num = deque[front + 1];
cnt--;
front++;
return num;
}
static int popBack()
{
if (cnt == 0)
{
return -1;
}
int num = deque[back - 1];
cnt--;
back--;
return num;
}
static int size()
{
return cnt;
}
static int empty()
{
if (cnt > 0)
{
return 0;
}
return 1;
}
static int Front()
{
if (cnt == 0)
{
return -1;
}
if (front == deque.Length - 1)
{
return deque[0];
}
return deque[front + 1];
}
private static int Back()
{
if (cnt == 0)
{
return -1;
}
if (back == 0)
{
return deque[deque.Length - 1];
}
return deque[back - 1];
}
}
}
반응형
'Algorithm' 카테고리의 다른 글
[BOJ] 2979 트럭 주차 (0) | 2023.01.18 |
---|---|
[BOJ] 10808 알파벳 개수 (0) | 2023.01.17 |
[BOJ] 1158 요세푸스 문제 (0) | 2023.01.17 |
[프로그래머스] 최소 직사각형 (0) | 2023.01.17 |
[BOJ] 10845 큐 (0) | 2023.01.17 |