문제링크 : https://www.acmicpc.net/problem/8979
import java.io.*;
import java.util.*;
public class BOJ8979 {
static int n, k, ans;
static class Node implements Comparable<Node>{
int nation;
int x; int y; int z;
int rank;
Node(int nation, int x, int y, int z, int rank){
this.nation = nation;
this.x = x;
this.y = y;
this.z = z;
this.rank = rank;
}
@Override
public int compareTo(Node node){
if (node.x == this.x) {
if (node.y == this.y) {
return node.z - this.z;
} else {
return node.y - this.y;
}
}else{
return node.x-this.x;
}
}
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
ArrayList<Node> info = new ArrayList<>();
for (int i=0; i<n; i++){
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
info.add(new Node(a, b, c, d, 0));
}
Collections.sort(info);
info.get(0).rank = 1;
for (int i=1; i<info.size(); i++){
Node now = info.get(i);
int prevX = info.get(i-1).x;
int prevY = info.get(i-1).y;
int prevZ = info.get(i-1).z;
if (info.get(i).nation == k){
ans = i;
}
if (now.x == prevX && now.y == prevY && now.z==prevZ){
info.get(i).rank = info.get(i-1).rank;
}else{
info.get(i).rank = i+1;
}
}
System.out.println(info.get(ans).rank);
}
}
해설:
1. 메달 별로 점수를 책정할까 생각했는데, 단순히 정렬을 이용하면 되는 문제였다.
2. Comparable을 이용해서 정렬을 해주었다.
3. 이후에는 조건별로 처리해주면된다. 만약 2등이 두명이라면, 이후는 3등이 아니라 4등임을 유념해야한다.
'Algorithm > Java' 카테고리의 다른 글
[백준] 17281 ⚾ (야구) (0) | 2024.04.17 |
---|---|
[백준] 14719 빗물 (1) | 2024.04.10 |
[백준] 1864 스카이라인 쉬운거 (0) | 2024.04.08 |
[백준] 2146 다리 만들기 (0) | 2024.03.25 |
[백준] 14658 하늘에서 별똥별이 빗발친다 (0) | 2024.03.14 |