[BOJ][C#] 2535 아시아 정보올림피아드

LimJaeJun·2023년 12월 4일
0

PS/BOJ

목록 보기
55/108

📕 문제

📌 링크

📗 접근 방식

  • 성적이 높은 순서대로 정렬
  • 총 3명만 뽑게 rank 변수 활용
    • rank가 4라면 더이상 찾을 필요 없으니 break
  • 상위 2명씩만 선택하되, 이미 2명이 선택된 경우 해당 학생을 건너 뜀
    • 해당 나라의 dictionary를 탐색하여 몇명이 뽑혔는지 체크

📘 코드

using System.Text;

namespace BOJ_2535
{
    class Program
    {
        static void Main()
        {
            using StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
            using StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));

            StringBuilder sb = new StringBuilder();
            List<(int, int, int)> list = new List<(int, int, int)>();
            Dictionary<int, int> dict = new Dictionary<int, int>();
            int n = int.Parse(sr.ReadLine());
            for (int i = 0; i < n; i++)
            {
                int[] inputs = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);

                list.Add((inputs[0], inputs[1], inputs[2]));
            }

            var sorted = list.OrderByDescending(x => x.Item3).ToList();

            int rank = 1;
            
            for (int i = 0; i < sorted.Count(); i++)
            {
                if (rank == 4) break;
                
                dict.TryAdd(sorted[i].Item1, 0);

                if (dict[sorted[i].Item1] == 2) continue;
                
                dict[sorted[i].Item1]++;
                
                sb.AppendLine($"{sorted[i].Item1} {sorted[i].Item2}");
                rank++;
            
            }

            sw.Write(sb);
        }
    }
}

📙 오답노트

📒 알고리즘 분류

  • 구현
  • 정렬
profile
Dreams Come True

0개의 댓글

관련 채용 정보