Coding - Algo/Java
[백준] 10825번:국영수 (Java 자바)
jainn
2021. 11. 12. 00:23
728x90
문제
https://www.acmicpc.net/problem/10825
10825번: 국영수
첫째 줄에 도현이네 반의 학생의 수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 한 줄에 하나씩 각 학생의 이름, 국어, 영어, 수학 점수가 공백으로 구분해 주어진다. 점수는 1보다 크거나 같고, 1
www.acmicpc.net
풀이 및 소스코드
우선순위큐로 풀면 되는 문제이다.
자바에서는 우선순위큐를 풀기위해서는 Comparable 이라는 인터페이스를 implements 하면 된다.
메소드에 원하는 정렬을 정의하면 되는데,
this.mat : 인덱스가 앞서는 원소, o.mat : 인덱스가 뒤에있는 원소라고 생각하면 된다.
앞에꺼 빼기 뒤에꺼 즉, this.mat - o.mat 이 양수면 둘이 뒤집힌다. 즉 앞에있는 것이 클 때 ( ex. 6과 4 ) 뒤집힌다는 소리는 오름차순이라고 생각하면 된다.
this.mat - o.mat 이 음수면 가만히 있는다.
정리 : comparable 사용시, 앞-뒤 계산 후 결과 값이 양수면 오름차순정렬, 음수면 내림차순정렬 !
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
PriorityQueue<student> q = new PriorityQueue<student>();
int n = Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) {
st = new StringTokenizer(br.readLine());
String name = st.nextToken();
int kor = Integer.parseInt(st.nextToken());
int eng = Integer.parseInt(st.nextToken());
int mat = Integer.parseInt(st.nextToken());
q.add(new student(kor, eng, mat, name));
}
while(!q.isEmpty()) {
student tmp = q.poll();
sb.append(tmp.name).append("\n");
}
System.out.println(sb);
}
}
class student implements Comparable<student>{
int kor, eng, mat;
String name;
@Override
public int compareTo(student o) {
if(this.kor == o.kor) {
// 국어점수가 같으면
if(this.eng == o.eng) {
// 영어점수도 같으면
if(this.mat==o.mat) {
// 모든 점수가 같으면
return this.name.compareTo(o.name);
}
// 수학점수가 감소하는 순서로
return o.mat-this.mat;
}
// 영어 점수가 증가하는 순서로
return this.eng-o.eng;
}
// 국어 점수가 감소하는 순서로
return o.kor-this.kor;
}
public student(int kor, int eng, int mat, String name) {
super();
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.name = name;
}
}
반응형