1. 문제

13414번: 수강신청



2. 개요

다음과 같은 규칙으로 수강신청을 진행하려고 한다.

  1. 수강신청 버튼이 활성화 된 후, 수강신청 버튼을 조금이라도 빨리 누른 학생이 대기목록에 먼저 들어간다.
  2. 이미 대기열에 들어가 있는 상태에서 다시 수강신청 버튼을 누를 경우 로 밀려난다.

    대기목록의 맨 뒤

  3. 잠시 후 수강신청 버튼이 비활성화 되면, 대기목록에서 가장 앞에 있는 학생부터 자동으로 수강신청이 완료되며, 수강 가능 인원이 꽉 찰 경우 나머지 대기목록은 무시하고 수강신청을 종료한다.

과목의 수강가능인원, 수강신청 인원의 수, 수강신청한 학생의 학번이 주어질 때, 최종적으로 수강신청에 성공한 학생들의 학번을 순서대로 출력하는 문제.



3. 풀이 및 코드

3-1. 풀이

입력이 들어오는 대로 딕셔너리로 학번, 수강신청 대기순서를 저장한다.

만약 이미 딕셔너리에 존재하는 학번이라면 값을 일단 가장 크게 갱신한다.

입력이 끝났다면, 딕셔너리를 값에 대해 정렬하고 K번 이내의 모든 값들을 출력한다.

3-2. Python

import sys

K, L = map(int, sys.stdin.readline().split())

register = dict()

for i in range(L):
    snum = sys.stdin.readline().rstrip()
    register[snum] = i + 1

register = dict(sorted(register.items(), key = lambda x : x[1]))

result = list(register.keys())

cnt = 1
for r in result:
    if cnt > K:
        break

    print(r)
    cnt += 1

3-3. C#

namespace boj_13414
{
    internal class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            string[] input = sr.ReadLine().Split();

            int K = int.Parse(input[0]), L = int.Parse(input[1]);

            Dictionary<string, int> register = new();

            for (int i = 0; i < L; i++)
            {
                register[sr.ReadLine().TrimEnd()] = i + 1;
            }

            int cnt = 1;
            foreach (KeyValuePair<string, int> r in register.OrderBy(x => x.Value))
            {
                if (cnt > K)
                    break;

                sw.WriteLine(r.Key);
                cnt++;
            }

            sw.Close();
        }
    }
}

Comments