알고리즘 문제풀이/백준
[백준 8979] 올림픽(JAVA)
소보루:-)
2020. 3. 21. 18:00
https://www.acmicpc.net/problem/8979
8979번: 올림픽
입력의 첫 줄은 국가의 수 N(1 ≤ N ≤ 1,000)과 등수를 알고 싶은 국가 K(1 ≤ K ≤ N)가 빈칸을 사이에 두고 주어진다. 각 국가는 1부터 N 사이의 정수로 표현된다. 이후 N개의 각 줄에는 차례대로 각 국가를 나타내는 정수와 이 국가가 얻은 금, 은, 동메달의 수가 빈칸을 사이에 두고 주어진다. 전체 메달 수의 총합은 1,000,000 이하이다.
www.acmicpc.net
import java.util.PriorityQueue;
import java.util.Scanner;
class Medal implements Comparable<Medal>{
int country;
int g;
int s;
int c;
public Medal(int country,int g, int s, int c) {
super();
this.country =country;
this.g = g;
this.s = s;
this.c = c;
}
@Override
public int compareTo(Medal o) {
if(this.g==o.g) {
if(this.s==o.s) {
return -(this.c-o.c);
}else return -(this.s-o.s);
}else return -(this.g-o.g);
}
}
public class Main_bj_8979_올림픽 {
static int N;//1부터 N까지
static int K;
static int[] cnt;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
PriorityQueue<Medal> pq=new PriorityQueue<Medal>();
N=sc.nextInt();
K=sc.nextInt();
for(int i=0; i<N; i++) {
int index=sc.nextInt();
int g=sc.nextInt();
int s=sc.nextInt();
int c=sc.nextInt();
pq.offer(new Medal(index,g,s,c));
}
int rank=1;
Medal tmp = pq.poll();
if(tmp.country==K) {
System.out.println(rank);
System.exit(0);
}
int same=0;
while(!pq.isEmpty()) {
Medal m = pq.poll();
if(!(tmp.g==m.g && tmp.s==m.s &&tmp.c==m.c)){
tmp=m;
rank++;
if(same>0) {
rank+=same;
same=0;
}
}else {
same++;
}
if(m.country==K) {
System.out.println(rank);
System.exit(0);
}
}
}
}