[백준] PS/Java

[백준 8979] 올림픽 - JAVA

SH3542 2025. 2. 5. 19:55

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]);
  }
}