Algorithm/Simulation, Implement
[백준][BOJ 3758] - KCPC
Lazy_developer
2023. 10. 5. 15:58
문제
https://www.acmicpc.net/problem/3758
3758번: KCPC
입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 테스트 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터의 첫 번째 줄에는
www.acmicpc.net
문제 접근
- 여러 조건에 따라 데이터를 정렬하는 문제
- 데이터만 잘 저장해 놓으면 나머지는 조건에 따라 정렬식을 구현하면 되는 문제였다. 필자의 경우 람다를 활용하여 정렬식을 구현하였다.
정답 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int testCase = 0; testCase < T; testCase++){
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int teamId = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] result = new int[N][K]; // team id, problem number, [seq, score]
int[] submitCount = new int[N];
int[] lastSubmitIdx = new int[N];
for (int i = 0; i < M; i++){
st = new StringTokenizer(br.readLine());
int team = Integer.parseInt(st.nextToken());
int problemNo = Integer.parseInt(st.nextToken());
int score = Integer.parseInt(st.nextToken());
if (result[team - 1][problemNo - 1] < score){
result[team - 1][problemNo - 1] = score;
}
submitCount[team - 1]++;
lastSubmitIdx[team - 1] = i;
}
int[][] scoreSum = new int[N][2]; // [team, scoreSum]
for (int team = 0; team < N; team++){
scoreSum[team][0] = team;
for (int problemNo = 0; problemNo < K; problemNo++){
scoreSum[team][1] += result[team][problemNo];
}
}
Arrays.sort(scoreSum, (x, y) -> {
if (x[1] == y[1]){
if (submitCount[x[0]] == submitCount[y[0]]){
return lastSubmitIdx[x[0]] - lastSubmitIdx[y[0]];
} else{
return submitCount[x[0]] - submitCount[y[0]];
}
} else{
return y[1] - x[1];
}
});
int rank = 1;
for (int i = 0; i < N; i++){
if (scoreSum[i][0] != teamId - 1){
rank++;
} else{
System.out.println(rank);
break;
}
}
}
}
}