본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 1966 프린터 큐

by 왹져박사 2023. 10. 21.
728x90

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

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

처음에는 Queue의 형식을 int로만 하고 index를 따로 더하고 초기화하며 계산했지만, 유동적인 크기를 계속해서 계산해주어야 했기 때문에 문제가 많았다. 해결하기 위하여 Queue의 형식을 (int, int)로 바꾸고 초기의 인덱스 번호를 Item2에 넣어주어 고정시켜 해결하였다!

using System;
using System.IO;
using System.Text;

namespace B1966
{
    class Program
    {
        static Queue<(int, int)> q = new Queue<(int, int)>();
        static int GetNum(int[] arr, int m)
        {
            int count = 0;
            for(int i = 0; i < arr.Length; i++) q.Enqueue((arr[i], i));
            int max;
            while (q.Count > 0)
            {
                max = q.Max(x => x.Item1);
                var deq = q.Dequeue();
                //가장 앞에 있는 문서의 중요도가 가장 높다면
                if (deq.Item1 == max)
                {
                    count++;
                    if (deq.Item2 == m) break;
                }
                else q.Enqueue(deq);
            }
            return count;
        }
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();

            int t = int.Parse(sr.ReadLine());
            for (int i = 0; i < t; i++)
            {
                int[] nm = Array.ConvertAll(sr.ReadLine().Split(' '), int.Parse);
                string str = sr.ReadLine();
                int result = 0;
                if (str.Count() == 1) sb.Append("1\n");
                else
                {
                    int[] arr = Array.ConvertAll(str.Split(' '), int.Parse);
                    result = GetNum(arr, nm[1]);
                    sb.Append(result + "\n");
                }
                q.Clear();
            }
            sw.Write(sb);
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90