📌 문제 링크 - https://www.acmicpc.net/problem/1713
문제
월드초등학교 학생회장 후보는 일정 기간 동안 전체 학생의 추천에 의하여 정해진 수만큼 선정된다. 그래서 학교 홈페이지에 추천받은 학생의 사진을 게시할 수 있는 사진틀을 후보의 수만큼 만들었다. 추천받은 학생의 사진을 사진틀에 게시하고 추천받은 횟수를 표시하는 규칙은 다음과 같다.
- 학생들이 추천을 시작하기 전에 모든 사진틀은 비어있다.
- 어떤 학생이 특정 학생을 추천하면, 추천받은 학생의 사진이 반드시 사진틀에 게시되어야 한다.
- 비어있는 사진틀이 없는 경우에는 현재까지 추천 받은 횟수가 가장 적은 학생의 사진을 삭제하고, 그 자리에 새롭게 추천받은 학생의 사진을 게시한다. 이때, 현재까지 추천 받은 횟수가 가장 적은 학생이 두 명 이상일 경우에는 그러한 학생들 중 게시된 지 가장 오래된 사진을 삭제한다.
- 현재 사진이 게시된 학생이 다른 학생의 추천을 받은 경우에는 추천받은 횟수만 증가시킨다.
- 사진틀에 게시된 사진이 삭제되는 경우에는 해당 학생이 추천받은 횟수는 0으로 바뀐다.
후보의 수 즉, 사진틀의 개수와 전체 학생의 추천 결과가 추천받은 순서대로 주어졌을 때, 최종 후보가 누구인지 결정하는 프로그램을 작성하시오.
입력
첫째 줄에는 사진틀의 개수 N이 주어진다. (1 ≤ N ≤ 20) 둘째 줄에는 전체 학생의 총 추천 횟수가 주어지고, 셋째 줄에는 추천받은 학생을 나타내는 번호가 빈 칸을 사이에 두고 추천받은 순서대로 주어진다. 총 추천 횟수는 1,000번 이하이며 학생을 나타내는 번호는 1부터 100까지의 자연수이다.
출력
사진틀에 사진이 게재된 최종 후보의 학생 번호를 증가하는 순서대로 출력한다.
코드
구현, 시뮬레이션
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// https://www.acmicpc.net/problem/1713
public class Main_1713 {
private static int N; // 사진틀의 개수
private static int M; // 총 추천 횟수
private static int[] numbers; // 추천받은 학생 번호
public static void main(String[] args) throws IOException {
init();
sol();
}//main
private static void sol() {
List<Student> list = new ArrayList<>(); // 사진틀
Set<Integer> numberSet = new HashSet<>(); // 사진틀에 있는 학생 번호
for(int i=0; i<M; i++) {
// 현재 사진이 게시된 학생이 다른 학생의 추천을 받은 경우에는 추천받은 횟수만 증가
if(numberSet.contains(numbers[i])) {
incrementCount(list, numbers[i]);
}
// 첫 게시
else {
// 비어있는 사진틀이 없는 경우 기존 학생 삭제
if (list.size() >= N) {
removeStudent(list, numberSet);
}
// 추천받은 학생 게시
numberSet.add(numbers[i]);
list.add(new Student(numbers[i], 1, i));
}
}//for
// 최종 결과물
List<Integer> result = new ArrayList<>();
for(Student student : list) {
result.add(student.number);
}
Collections.sort(result);
StringBuilder ans = new StringBuilder();
for(int n : result) {
ans.append(n).append(' ');
}
System.out.print(ans);
}//sol
private static void incrementCount(List<Student> list, int num) {
for(Student student : list) {
if (student.number == num) {
student.count++;
break;
}
}
}//incrementCount
private static void removeStudent(List<Student> list, Set<Integer> numberSet) {
// 현재까지 추천 받은 횟수가 가장 적은 학생, 그러한 학생들 중 게시된 지 가장 오래된 사진을 삭제
Collections.sort(list);
numberSet.remove(list.get(0).number);
list.remove(0);
}//removeStudent
private static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine()); // 사진틀의 개수
M = Integer.parseInt(br.readLine()); // 총 추천 횟수
numbers = new int[M]; // 추천받은 학생 번호
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<M; i++) {
numbers[i] = Integer.parseInt(st.nextToken());
}
br.close();
}//init
private static class Student implements Comparable<Student> {
int number; // 학생 번호
int count; // 추천 횟수
int time; // 추천 받은 시간
public Student(int number, int count, int time) {
this.number = number;
this.count = count;
this.time = time;
}
@Override
public int compareTo(Student o) {
if(this.count == o.count) return this.time - o.time;
return this.count - o.count;
}
}//Student
}//class
'Algorithm > 백준' 카테고리의 다른 글
[Java] 백준 2343. 기타 레슨 (0) | 2025.01.30 |
---|---|
[Java] 백준 17266. 어두운 굴다리 (0) | 2025.01.24 |
[Java] 백준 2228. 구간 나누기 (1) | 2025.01.21 |
[Java] 백준 17259. 선물이 넘쳐흘러 (0) | 2025.01.17 |
[Java] 백준 2314. 이세계 게임 (0) | 2025.01.09 |