코테/백준

[백준/JAVA] 3758번: KCPC

imname1am 2024. 7. 16. 23:58
반응형

📖 문제

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

 

 

 

 

💡  풀이 방식

• 정렬

. 문제의 조건에 맞게 정렬한다.

 @Override
public int compareTo(Info i) {
    if (this.score == i.score) {    // 총점 높은 순
        if (this.cnt == i.cnt)      // 총점 같고 제출 횟수 같으면, 마지막 제출 시간 더 빠른 순
            return this.time - i.time;
        return this.cnt - i.cnt;    // 총점 같고, 제출 횟수 다르면, 제출 횟수 적은 순순
    }
    return i.score - this.score;
}

 

 

 

🔺 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.*;
import java.io.*;
 
public class Main {
    static int T, n, k, t, m;
    static int[][] logs;    // 입력 로그
    static List<Info> list; // 팀 객체 리스트
    static StringBuilder sb = new StringBuilder();
 
    static class Info implements Comparable<Info> {
        int id, score, cnt, time;
 
        public Info(int id, int score, int cnt, int time) {
            this.id = id;       // 팀 번호
            this.score = score; // 최종 점수
            this.cnt = cnt;     // 문제 제출 횟수
            this.time = time;   // 마지막 제출 시간
        }
 
        @Override
        public int compareTo(Info i) {
            if (this.score == i.score) {    // 총점 높은 순
                if (this.cnt == i.cnt)      // 총점 같고 제출 횟수 같으면, 마지막 제출 시간 더 빠른 순
                    return this.time - i.time;
                return this.cnt - i.cnt;    // 총점 같고, 제출 횟수 다르면, 제출 횟수 적은 순순
            }
            return i.score - this.score;
        }
    }
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
 
        T = Integer.parseInt(br.readLine());
        while (T-- > 0) {
            st = new StringTokenizer(br.readLine(), " ");
            n = Integer.parseInt(st.nextToken());   // 팀 수
            k = Integer.parseInt(st.nextToken());   // 문제 개수
            t = Integer.parseInt(st.nextToken());   // 우리 팀 ID
            m = Integer.parseInt(st.nextToken());   // 로그 엔트리 수
 
            logs = new int[m][3];
 
            for (int x = 0 ; x < m ; x++) {
                st = new StringTokenizer(br.readLine(), " ");
                int i = Integer.parseInt(st.nextToken());   // 팀 ID
                int j = Integer.parseInt(st.nextToken());   // 문제 번호
                int s = Integer.parseInt(st.nextToken());   // 획득 점수
 
                // 로그에 저장
                logs[x][0= i;
                logs[x][1= j;
                logs[x][2= s;
            }
 
            int[][] saveScore = new int[n+1][k+1]; // 각 팀의 문제 별 점수 저장 배열
            int[] cntSubmit = new int[n+1]; // 제출 횟수
            int[] time = new int[n+1]; // 제출 시간 기록용 배열
 
            for (int i = 0 ; i < m ; i++) {
                int nowId = logs[i][0];
                int nowNum = logs[i][1];
                int nowScore = logs[i][2];
 
                cntSubmit[nowId]++;
                time[nowId] = i; // 마지막 제출 시간 기록
 
                if (nowScore > saveScore[nowId][nowNum]) {
                    saveScore[nowId][nowNum] = nowScore;
                }
            }
 
            // 2. 각 팀 별 총점 계산하기
            list = new ArrayList<>();   // 점수 리스트
            for (int i = 1 ; i <= n ; i++) {
                int sum = 0;
                for (int j = 1; j <= k; j++) {
                    sum += saveScore[i][j];
                }
                
                // 팀 객체 생성 후 리스트에 저장
                list.add(new Info(i, sum, cntSubmit[i], time[i]));
            }
 
            Collections.sort(list); // 3. 조건에 맞게 정렬하기
 
            // 3. 문제에서 주어진 팀 등수 찾기
            for (int i = 0 ; i < n ; i++) {
                if (list.get(i).id == t) {
                    sb.append((i + 1+ "\n");
                }
            }
        }
 
        System.out.println(sb.toString());
    }
}
 
cs

 

 

 


💦 어려웠던 점

- 저장해야 하는 값이 많아서 좀 헷갈렸다..

 

 

1회독 2회독 3회독 4회독 5회독
V        

(참고)

 

[백준] 3785: KCPC (자바)

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

velog.io

 

반응형