백준 2535번 아시아 정보올림피아드 JAVA

YB·2025년 2월 11일

링크텍스트

설명

Comparator 정렬을 통해 점수를 내림차순한 후 각 나라 번호와 학생 번호를 출력하였다.
시간복잡도: O(NLogN), 공간복잡도: O(N)

코드

import java.util.*;
import java.io.*;

class Main {
	public static void main (String[] args) throws IOException{
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayList<Result> al = new ArrayList<>();

		int n = Integer.parseInt(br.readLine());

		for(int i=0;i<n;i++){
			StringTokenizer st = new StringTokenizer(br.readLine());

			int countryNum = Integer.parseInt(st.nextToken());
	    	int studentNum = Integer.parseInt(st.nextToken());
	    	int score = Integer.parseInt(st.nextToken());

			al.add(new Result(countryNum, studentNum, score));
		}

		Collections.sort(al, new Comparator<Result>(){
			@Override
			public int compare(Result o1, Result o2){
				return o2.score - o1.score;
			}
		});

		int [] medalCount = new int[101];
		int count = 0;

		for (Result r : al){
			if(medalCount[r.countryNum]<2){
				System.out.println(r.countryNum + " " + r.studentNum);
				medalCount[r.countryNum]++;
				count++;
			}
			if(count==3) break;
		}
	}
	
	public static class Result{
	    int countryNum;
	    int studentNum;
	    int score;
	    
	    Result(int countryNum, int studentNum, int score){
	        this.countryNum = countryNum;
	        this.studentNum = studentNum;
    	    this.score = score;
	    }
	}
}

profile
안녕하세요

0개의 댓글