백준

[백준] 1713번 후보 추천하기 (java, 시뮬레이션, 구현)

해랑쓰 2021. 4. 20. 01:45
728x90
반응형

www.acmicpc.net/problem/1713

 

1713번: 후보 추천하기

첫째 줄에는 사진틀의 개수 N이 주어진다. (1≤N≤20) 둘째 줄에는 전체 학생의 총 추천 횟수가 주어지고, 셋째 줄에는 추천받은 학생을 나타내는 번호가 빈 칸을 사이에 두고 추천받은 순서대로

www.acmicpc.net

각 학생의 추천횟수를 저장하는 배열을 따로 만들어주고 정렬을 위한 학생 객체는 리스트로 만들어줘서 추가, 삭제가 용이하도록 하였다.

 

package algo0419;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Student implements Comparable<Student>{
	int num;
	int total;
	int time;
	public Student(int num, int total, int time) {
		super();
		this.num = num;
		this.total = total;
		this.time = time;
	}
	@Override
	public int compareTo(Student o) {
		if(this.total==o.total) {
			return this.time-o.time;
		}else if(this.total<o.total)return -1;
		else return 1;
	
	}
	
	
}

public class B_1713_후보추천하기 {
	static int n,m;
	static int[] students;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		m=sc.nextInt();
		students=new int[101];//학생 번호 100까지
		ArrayList<Student> list=new ArrayList<>();
		for (int i = 0; i < m; i++) {//1000번 이하 
			int student=sc.nextInt();//추천학생 번호
			if(students[student]>0) {//있다
				students[student]+=1;
				for (int j = 0; j <list.size(); j++) {
					if(list.get(j).num==student) {
						list.get(j).total+=1;
						break;
					}
				}
			}else {//액자에 새로 추가
				if(list.size()>=n) {
					Collections.sort(list);
					int num=list.get(0).num;
					students[num]=0;
					list.remove(0);
				}
				list.add(new Student(student,1,i));
				students[student]=1;
			}	
			
		}
		
		for (int i = 0; i <101; i++) {
			if(students[i]!=0) {
				System.out.print(i+" ");
			}
		}
		
		
	}
}
728x90
반응형