Map을 이용한 indexing 문제였다.
처음에 문제를 이해하는데 N / 10 이라는 조건이 헷갈렸지만 30명이 있을 때 3명은 똑같은 점수를 받을 수 있으니 마지막으로 구한 idx에 3으로 나눠야 grade배열에 index로 접근할 수 있다는 의미였다.
나는 여기서 Map을 이용했는데 key가 아닌 value로 정렬하는 법이 핵심이였다.
key로 정렬하는법은 TreeMap을 이용하면 간편하지만 value로 하기 위해선
list를 만들고 entitySet(), keySet()으로 초기화 시켜준다음 정렬하는게 포인트였다.
List<Integer> key_list = new ArrayList<>(map.keySet());
Collections.sort(key_list, (o1, o2) -> (map.get(o2).compareTo(map.get(o1))));
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int T = Integer.parseInt(st.nextToken());
for (int tc = 1; tc < T+1; tc++) {
st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
String[] grade = {"A+","A0","A-","B+","B0","B-","C+","C0","C-","D0"};
Map<Integer, Double> map = new HashMap<>();
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());
map.put(i+1, a * 0.35 + b * 0.45 + c * 0.2);
}
List<Integer> key_list = new ArrayList<>(map.keySet());
Collections.sort(key_list, (o1, o2) -> (map.get(o2).compareTo(map.get(o1))));
int idx = key_list.indexOf(K) / (N/10);
System.out.println("#"+tc+" "+grade[idx]);
}
}
}
'Algorithm > Algorithm Problem' 카테고리의 다른 글
백준 14238 출근 기록(DFS+DP) ★ (0) | 2022.12.16 |
---|---|
백준 1062 가르침(조합) (0) | 2022.12.06 |
SWEA 1859 백만 장자 프로젝트(그리디) ★★ (0) | 2022.11.03 |
백준 3687 성냥개비(DP, 그리디) (0) | 2022.10.31 |
백준 7682 틱택토(구현) (0) | 2022.10.25 |