[백준 2535] 아시아 정보올림피아드 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
학생들의 성적과 국가 정보를 바탕으로 메달 수상자를 선정하는 문제입니다.
각 학생은 소속 국가와 점수를 가지고 있으며, 모든 학생을 점수 기준으로 정렬하여 상위 3명
을 선발해야 합니다.
단, 한 국가가 받을 수 있는 메달은 최대 2개
로 제한되기 때문에,
같은 국가의 학생이 모두 상위권에 있더라도 메달을 받을 수 있는 수는 제한됩니다.
접근법
- 모든 참가자의 정보를
(국가 번호, 학생 번호, 점수)
형태로 저장합니다. - 점수를 기준으로 내림차순 정렬하여, 성적이 높은 순서대로 메달 후보를 검사합니다.
- 국가별 메달 수를 별도로 관리하면서, 각 국가에서 이미
2개
의 메달을 받은 경우는 제외하고 진행합니다. - 최종적으로
3명
의 메달 수상자가 결정되면 출력을 종료합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
int n = int.Parse(Console.ReadLine());
var list = new List<(int nation, int student, int score)>();
int maxNation = 0;
for (int i = 0; i < n; i++) {
var tokens = Console.ReadLine().Split().Select(int.Parse).ToArray();
list.Add((tokens[0], tokens[1], tokens[2]));
maxNation = Math.Max(maxNation, tokens[0]);
}
var sorted = list.OrderByDescending(x => x.score).ToList();
var cnt = new int[maxNation + 1];
int awarded = 0;
foreach (var entry in sorted) {
if (cnt[entry.nation] < 2) {
Console.WriteLine($"{entry.nation} {entry.student}");
cnt[entry.nation]++;
awarded++;
if (awarded == 3) break;
}
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
bool comp(vi& l1, vi& l2) {
return l1[2] > l2[2];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vvi line(n, vi(3));
int maxNation = 0;
for (auto& row : line) {
cin >> row[0] >> row[1] >> row[2];
maxNation = max(maxNation, row[0]);
}
sort(line.begin(), line.end(), comp);
vi cntMedal(maxNation, 0);
for (int i = 0, cnt = 0; i < n && cnt < 3; i++) {
int nation = line[i][0] - 1;
if (cntMedal[nation] < 2) {
cntMedal[nation]++;
cout << line[i][0] << " " << line[i][1] << "\n";
cnt++;
}
}
return 0;
}