[백준] PS/Java [실랜디]

[백준 2535] 아시아 정보올림피아드 - JAVA

SH3542 2025. 3. 10. 21:37

https://www.acmicpc.net/problem/2535

 

구현 문제

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

class Main {

  static class P {

    int country, num, score;

    P(int country, int num, int score) {
      this.country = country;
      this.num = num;
      this.score = score;
    }
  }

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int N = Integer.parseInt(br.readLine());

    P[] a = new P[N];

    StringTokenizer st;
    for (int i = 0; i < N; i++) {
      st = new StringTokenizer(br.readLine());
      a[i] = new P(Integer.parseInt(st.nextToken()),
          Integer.parseInt(st.nextToken()),
          Integer.parseInt(st.nextToken()));
    }

    Arrays.sort(a, Comparator.comparing(p -> p.score, Comparator.reverseOrder()));

    Map<Integer, Integer> m = new HashMap<>();

    int cnt = 0;
    for (int i = 0; i < N; i++) {

      P p = a[i];

      if (!m.containsKey(p.country)) {
        m.put(p.country, 1);
        System.out.println(String.format("%d %d", p.country, p.num));
        cnt++;
      } else if (m.get(p.country) < 2) {
        m.put(p.country, 2);
        System.out.println(String.format("%d %d", p.country, p.num));
        cnt++;
      }

      if (cnt == 3) {
        break;
      }
    }
  }
}

'[백준] PS > Java [실랜디]' 카테고리의 다른 글

[백준 2161] 카드1 - JAVA  (0) 2025.03.11
[백준 2491] 수열 - JAVA  (0) 2025.03.10
[백준 2034] 반음 - JAVA  (0) 2025.03.10
[백준 1992] 쿼드트리 - JAVA  (0) 2025.03.10
[백준 2303] 숫자 게임 - JAVA  (0) 2025.03.09