알고리즘/백준

[Java] 2456 나는 학급회장이다 - 정렬

Garonguri 2022. 1. 27. 10:30
728x90

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

 

2456번: 나는 학급회장이다

첫째 줄에는 반의 학생들의 수 N (3 ≤ N ≤ 1,000)이 주어진다. 다음 N개의 각 줄에는 각 학생이 제출한 회장후보 3명에 대한 선호 점수가 주어지는 데, 첫 번째 점수는 후보 1번에 대한 점수이고 두

www.acmicpc.net

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
 
public class bj2456 {
    static int[][] candidate;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        Integer N = Integer.parseInt(br.readLine());
        candidate = new int[4][N];
        int[][] sum = new int[4][5];
        for(int i=0;i<N;i++){
            String tmp = br.readLine();
            StringTokenizer st = new StringTokenizer(tmp);
            candidate[1][i] = Integer.parseInt(st.nextToken()); //1번점수
            candidate[2][i] = Integer.parseInt(st.nextToken()); //2번점수
            candidate[3][i] = Integer.parseInt(st.nextToken()); //3번점수
 
            for(int j=0;j<3;j++){
                sum[j+1][4=j+1// 후보 번호가 몇번인지
                sum[j+1][0+= candidate[j+1][i]; //해당 후보가 몇점인지
                sum[j+1][((candidate[j+1][i])%3)+1]++//후보별 점수 개수 세기
            }
        }
 
        Arrays.sort(sum, new Comparator<int[]>(){
            @Override
            public int compare(int[] o1, int[] o2){
                if(o1[0==o2[0]){
                    return o2[1]-o1[1];
                }else{
                    return o2[0]-o1[0];
                }
            }
        });
 
        int[] arr1 = Arrays.copyOfRange(sum[0], 1,4);
        int[] arr2 = Arrays.copyOfRange(sum[1], 1,4);
 
        for(int[] i : sum){
            for(int in : i){
                System.out.print(in + " ");
            }
            System.out.println();
        }
 
        if(sum[0][0== sum[1][0]){
            if (is(arr1, arr2)) {
                System.out.println(0 + " " + sum[0][0]);
            }else{
                System.out.println(sum[0][4+ " " + sum[0][0]);
            }
        }else{
            System.out.println(sum[0][4+ " " + sum[0][0]);
        }
    }
    private static boolean is(int[] a, int[] b){
        int cnt=0;
        for(int s=0;s<3;s++){
            if(a[s] ==b[s]) {
                cnt++;
            }
        }
        if(cnt==3return true;
        else return false;
    }
}
cs

sum 이라는 배열에 각 값을 저장했다.

[후보n이 받은 점수의 총 합] [후보 n이 받은 3점의 개수] [후보 n이 받은 2점의 개수] [후보 n이 받은 1점의 개수] [후보 n의 후보 번호 n]

 

이렇게 저장한 후, sum배열을 내림차순으로 정렬하여

만약 sum[0]의 총합과 sum[1]의 총합이 같으면,

sum[0]의 [1:3] 즉 3,2,1점의 개수를 비교해 모두 같으면 0을 출력, 하나라도 다르면 sum[0]을 출력했다. 끄읕~

728x90