https://www.acmicpc.net/problem/8979
rank 구현은 맨날 헷갈림
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
class Main {
static class Country {
int key, g, s, b;
Country(int key, int g, int s, int b) {
this.key = key;
this.g = g;
this.s = s;
this.b = b;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
Country[] arr = new Country[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
arr[i] = new Country(
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())
);
}
int[] r = new int[N + 1];
Arrays.sort(arr, Comparator
.comparingInt((Country c) -> -c.g)
.thenComparingInt(c -> -c.s)
.thenComparingInt(c -> -c.b)
);
int rank = 1;
int depth = 0;
Country prev = null;
for (Country c : arr) {
if (null != prev && prev.g == c.g && prev.s == c.s && prev.b == c.b) {
depth++;
r[c.key] = rank;
} else {
r[c.key] = rank += depth;
depth = 1;
}
prev = c;
}
System.out.println(r[K]);
}
}
'[백준] PS > Java' 카테고리의 다른 글
[백준 2118] 두 개의 탑 - JAVA (0) | 2025.02.06 |
---|---|
[백준 2230] 수 고르기 - JAVA (1) | 2025.02.06 |
[백준 25395] 커넥티드 카 실험 - JAVA (0) | 2025.02.05 |
[백준 31230] 모비스터디 - JAVA (0) | 2025.02.02 |
[백준 25969] 현대 모비스 자율 주행 시스템 - JAVA (1) | 2025.01.29 |