Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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
Archives
Today
Total
관리 메뉴

알고리즘 공부방

백준 1946 신입 사원(JAVA) 본문

알고리즘

백준 1946 신입 사원(JAVA)

head89 2022. 12. 18. 00:53

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

 

1946번: 신입 사원

첫째 줄에는 테스트 케이스의 개수 T(1 ≤ T ≤ 20)가 주어진다. 각 테스트 케이스의 첫째 줄에 지원자의 숫자 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개 줄에는 각각의 지원자의 서류심사 성

www.acmicpc.net

문제 유형: 정렬, 그리디 알고리즘

 

문제 풀이

먼저 서류전형이든 면전전형으로 오르차순 정렬을 하고, 첫 번째 면접(서류) 전형의 순위를 min 값으로 정의한 다음

두 번째부터 min을 비교하며 더 작으면 min를 재정의, 결과값을 1증가 시켜주면 된다

 

전체 코드

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


public class Main {
    static class Standing implements Comparable<Standing>{
        int docStanding;
        int interviewStanding;

        public Standing(int docStanding, int interviewStanding){
            this.docStanding = docStanding;
            this.interviewStanding = interviewStanding;
        }
        @Override
        public int compareTo(Standing o1){
            return this.docStanding - o1.docStanding;
        }

    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        while (T-- > 0){
            int n = Integer.parseInt(br.readLine());
            List<Standing> list = new ArrayList<>();
            for(int i = 0;i<n;i++){
                StringTokenizer st = new StringTokenizer(br.readLine());
                int docStanding = Integer.parseInt(st.nextToken());
                int interviewStanding = Integer.parseInt(st.nextToken());
                list.add(new Standing(docStanding, interviewStanding));
            }

            Collections.sort(list);

            int cnt = 1;
            int min = list.get(0).interviewStanding;

            for(int i = 1;i<n;i++){
                if(list.get(i).interviewStanding < min){
                    cnt++;
                    min = list.get(i).interviewStanding;
                }
            }
            sb.append(cnt).append("\n");
        }
        System.out.println(sb);
    }
}