https://www.acmicpc.net/problem/1946
1946번: 신입 사원
첫째 줄에는 테스트 케이스의 개수 T(1 ≤ T ≤ 20)가 주어진다. 각 테스트 케이스의 첫째 줄에 지원자의 숫자 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개 줄에는 각각의 지원자의 서류심사 성
www.acmicpc.net
풀이
문제를 보았을 때 알 수 있는 힌트
문제에 적어도 하나는 다른 지원자보다 떨어지지 않기만 하면 된다고 한다.
이것을 통해 하나의 성적을 정렬 하고 1순위에 있는 다른 성적을 기준으로하여 다음 순위가 작은지 큰지 확인하면 되는 문제이다.
예를 들어
3 2
1 4
4 1
2 3
5 5
이렇게 2가지의 숫자가 있다면 한쪽을 정렬해준다.
1 4
2 3
3 2
4 1
5 5
이렇게 한쪽으로 정렬을 하고 1순위는 당연히 합격을 하고 2순위의 사람의 성적을 봤을때 1순위의 나머지 성적보다 좋기에 합격이 된다. 이렇게 확인을 하면 총 4명이 합격을 하게 된다.
이렇게 생각을하고 지원자라는 클래스를 만들어서 서류와 면접의 순위를 가지게 하여 문제를 풀었다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
static Employee[] employees;
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t =Integer.parseInt(br.readLine());
for (int tc = 0; tc < t; tc++) {
int n = Integer.parseInt(br.readLine());
employees = new Employee[n];
for (int i = 0; i < n; i++) {
employees[i] = new Employee();
String[] temp =br.readLine().split(" ");
employees[i].paper = Integer.parseInt(temp[0]);
employees[i].meeting = Integer.parseInt(temp[1]);
}
Arrays.sort(employees);
int min = employees[0].meeting;
int count = 1;
for (int i = 1; i < n; i++) {
if(employees[i].meeting<min) {
min = employees[i].meeting;
count++;
}
if(min ==1) {
break;
}
}
sb.append(count).append("\n");
}
System.out.println(sb);
}
static class Employee implements Comparable<Employee>{
int paper;
int meeting;
public Employee() {
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
if(this.paper == o.paper) {
return this.meeting - o.meeting;
}
return this.paper - o.paper;
}
}
static void print() {
for (Employee employee : employees) {
System.out.println(employee.paper+":"+employee.meeting);
}
System.out.println();
}
}
이렇게 풀면 2936ms 가 나온다. 당연히 잘 풀었겠구나 생각했는데 좀더 단순하게 풀수있는 문제였다.
한쪽의 수를 배열 값으로 그냥 넣고 진행해도 되는 문제였다.
객체를 배웠다고 객체를 활용했지만.. 효율성이 떨어졌다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static int[] employees;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t =Integer.parseInt(br.readLine());
for (int tc = 0; tc < t; tc++) {
int n = Integer.parseInt(br.readLine());
employees = new int[n];
for (int i = 0; i < n; i++) {
String[] temp =br.readLine().split(" ");
employees[Integer.parseInt(temp[0])-1] = Integer.parseInt(temp[1]);
}
int min = employees[0];
int count = 1;
for (int i = 1; i < n; i++) {
if(employees[i]<min) {
min = employees[i];
count++;
}
}
System.out.println(count);
}
}
}
코드 수도 줄고 속도도 988ms로 빨라졌다.
클래스 생성하여 문제를 풀수있다는 생각에 도움은 되었지만 굳이 안쓸수있는 곳은 안쓰는 연습도 해야겠다.
https://github.com/dydwkd486/coding_test/blob/main/baekjoon/java/baekjoon1946/Main.java
GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리
코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.
github.com
'Coding Test > baekjoon' 카테고리의 다른 글
[백준 12100] 2048 (Easy)- JAVA (solved.ac - 골드 2) (0) | 2022.08.20 |
---|---|
[백준 3109] 빵집- JAVA (solved.ac - 골드 2) (0) | 2022.08.18 |
[백준 11651] 좌표 정렬하기 2 - JAVA (solved.ac - 실버 5) (0) | 2022.07.25 |
[백준 9375] 패션왕 신해빈 - python (solved.ac - 실버 3) (0) | 2022.07.02 |
[백준 11404] 플로이드 - python (solved.ac - 골드 4) (0) | 2022.07.01 |