https://www.acmicpc.net/problem/1713
시뮬레이션 문제 이다.
풀이 방법
1. 추천 받는 학생의 목록을 입력 받는다.
2. 이미 사진틀에 있는 학생을 추천하면 추천 횟수만 추가한다.
3. 사진틀이 가득 찼을 경우에는 추천 횟수가 가장 작은 학생을 찾는다. 이렇게 찾는 학생을 사진틀에서 삭제하고, 추천 횟수를 0으로 초기화한다.
4. 학생을 추가하고, 해당 학생의 추천 횟수를 추가한다.
5. 이렇게 정리된 사진틀의 학생를 오름차순으로 정렬하고, 출력한다.
정답코드
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int M = Integer.parseInt(br.readLine());
int student[] = new int[101];
List<Integer> frame = new ArrayList<Integer>();
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0 ; i<M ; i++) {
int num = Integer.parseInt(st.nextToken());
if(frame.contains(num)) { // 사진틀에 있는 학생이 추천받을 경우
student[num]++;
} else { // 사진틀이 가득찬 경우
if(frame.size() == N) {
int minValue = 10001;
int candidate = 0;
for(int j=0 ; j<frame.size() ; j++) { // 추천 횟수가 가장 작은 학생 찾기
int currentStudent = frame.get(j);
if(student[currentStudent] < minValue) {
minValue = student[currentStudent];
candidate = currentStudent;
}
}
// 추천이 가장 작은 학생 삭제
frame.remove(Integer.valueOf(candidate));
student[candidate] = 0;
}
// 학생 추가
frame.add(num);
student[num]++;
}
}
Collections.sort(frame);
for(int i : frame) {
System.out.print(i+" ");
}
}
}
'코딩테스트 > 백준(Beakjoon)' 카테고리의 다른 글
[백준] 1022 소용돌이 이쁘게 출력하기(JAVA) (1) | 2024.10.03 |
---|---|
[백준] 2579 계단 오르기 (JAVA) 풀이 - DP (0) | 2024.09.10 |
[백준] 1074 Z (JAVA) 풀이 (0) | 2024.09.05 |
[백준] 14891 톱니바퀴 (JAVA) 풀이 (0) | 2024.08.21 |
[백준] 5212 지구온난화 (JAVA) 풀이 (0) | 2024.08.19 |