아이디어
어떤 지원자의 서류 성적이 N 등이라면 이 지원자가 합격하기 위해서는 서류 기준 1 ~ N - 1 등의 면접 성적 중 가장 높은 성적보다 더 좋은 성적을 받아야 한다.
그렇지 않으면 다른 지원자 중 자신보다 서류 성적도, 면접 성적도 높은 지원자가 존재하게 되기 때문이다.
따라서 서류 성적 기준으로 지원자를 정렬하고 면접 성적을 비교하며 나보다 서류 성적이 높은 지원자 중 가장 좋은 면접 성적과의 비교를 통해 합/불 여부를 결정한다.
풀이코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder result = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
int N = Integer.parseInt(br.readLine()); // 지원자 수
List<Applicant> applicants = new ArrayList<>();
for (int j = 0; j < N; j++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int paper = Integer.parseInt(st.nextToken());
int interview = Integer.parseInt(st.nextToken());
Applicant applicant = new Applicant(paper, interview);
applicants.add(applicant);
}
applicants.sort((a1, a2) -> a1.paper - a2.paper);
int cnt = 1;
int interview = applicants.get(0).interview;
for (int j = 1; j < applicants.size(); j++) {
Applicant now = applicants.get(j);
if (now.interview < interview) {
cnt++;
interview = now.interview;
}
}
result.append(cnt).append("\n");
}
System.out.println(result);
}
}
class Applicant {
int paper;
int interview;
Applicant(int paper, int interview) {
this.paper = paper;
this.interview = interview;
}
}
'Algorithm > 백준 알고리즘' 카테고리의 다른 글
11722번. 가장 긴 감소하는 부분 수열 (0) | 2023.11.02 |
---|---|
2644번. 촌수계산 (0) | 2023.11.02 |
20152번. Game Addiction (1) | 2023.11.01 |
16401번. 과자 나눠주기 (0) | 2023.11.01 |
1080번. 행렬 (1) | 2023.11.01 |