|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +/** |
| 4 | + * 알고리즘: PriorityQueue + 구현 |
| 5 | + * 시간복잡도: 3 ≤ reqs의 길이 ≤ 300 으로 단순구현 가능 |
| 6 | + * 아이디어: |
| 7 | + - 유형별 최소 대기시간 계산에 따라 최적의 멘토 인원 분배 수를 찾고, 최소시간 계산하기 |
| 8 | + - 유형별 멘토 인원 분배 방법 |
| 9 | + - PriorityQueue를 사용해서 여러 작업에서 가장 빨리 완료할 수 있는 작업부터 처리하기 |
| 10 | + - 여러 작업: 유형별 상담 요청 |
| 11 | + - 가장 빨리 완료: 상담종료 시간이 가장 적은 사람부터 처리 |
| 12 | + - 기다리는 시간을 계산하고, 상담 종료 시 다음 상담요청 배치 |
| 13 | + */ |
| 14 | +public class YJ_214288 { |
| 15 | + static class Participant { |
| 16 | + int startTime; |
| 17 | + int endTime; |
| 18 | + int counseling; |
| 19 | + int type; |
| 20 | + public Participant(int startTime, int counseling, int type) { |
| 21 | + this.startTime = startTime; |
| 22 | + this.endTime = startTime+counseling; |
| 23 | + this.counseling = counseling; |
| 24 | + this.type = type; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static List<Participant> participantList = new ArrayList<>(); |
| 29 | + static int getTotalWaitingTime(int k, int n, int[][] reqs) { |
| 30 | + for(int [] r : reqs){ |
| 31 | + participantList.add(new Participant(r[0],r[1],r[2])); |
| 32 | + } |
| 33 | + |
| 34 | + int[] typeMento = new int[k+1]; |
| 35 | + Arrays.fill(typeMento,1); //유형별로 최소 1명의 상담원 배치 |
| 36 | + int remainingMento = n-k; |
| 37 | + |
| 38 | + while(remainingMento > 0){ |
| 39 | + int minTime = Integer.MAX_VALUE; |
| 40 | + int bestDivision = -1; |
| 41 | + //임의로 상담원을 배치해서 최적의 상담원 배치 도출하기 |
| 42 | + for(int i=1; i<typeMento.length; i++){ |
| 43 | + typeMento[i]++; |
| 44 | + int waitingTime = calcWaitingTime(typeMento); |
| 45 | + |
| 46 | + //상담원 배정시 유형별로 가장 적은 대기 시간일 경우 유형 저장하기 |
| 47 | + minTime = Math.min(minTime, waitingTime); |
| 48 | + if (minTime == waitingTime) { |
| 49 | + bestDivision = i; |
| 50 | + } |
| 51 | + typeMento[i]--; |
| 52 | + } |
| 53 | + |
| 54 | + typeMento[bestDivision]++; |
| 55 | + remainingMento--; |
| 56 | + } |
| 57 | + |
| 58 | + return calcWaitingTime(typeMento); |
| 59 | + } |
| 60 | + |
| 61 | + //유형별 대기 시간의 총합 계산 |
| 62 | + private static int calcWaitingTime(int[] typeMento){ |
| 63 | + int totalWaitingTime = 0; |
| 64 | + |
| 65 | + for(int type=1; type<typeMento.length; type++){ |
| 66 | + PriorityQueue<Integer> pq = new PriorityQueue<>(); |
| 67 | + int waitingTime = 0; |
| 68 | + //유형별로 계산 |
| 69 | + for(Participant participant : participantList){ |
| 70 | + if(participant.type != type){ |
| 71 | + continue; |
| 72 | + } |
| 73 | + if(pq.size() < typeMento[type]){ |
| 74 | + pq.offer(participant.endTime); |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + int minEndTime = pq.poll(); |
| 79 | + //대기시간 계산: 최소 상담종료시간 - 현재 사람의 상담 시작시간 |
| 80 | + waitingTime += Math.max(0,minEndTime-participant.startTime); |
| 81 | + //현재 사람의 상담이 종료되는 가장 빠른 시간 |
| 82 | + int fastEndTime = Math.max(minEndTime,participant.startTime)+participant.counseling; |
| 83 | + pq.offer(fastEndTime); |
| 84 | + } |
| 85 | + |
| 86 | + totalWaitingTime += waitingTime; |
| 87 | + } |
| 88 | + |
| 89 | + return totalWaitingTime; |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) { |
| 93 | + int k = 3; |
| 94 | + int n = 5; |
| 95 | + int[][] reqs = {{10, 60, 1}, {15, 100, 3}, {20, 30, 1}, {30, 50, 3}, {50, 40, 1}, {60, 30, 2}, {65, 30, 1}, {70, 100, 2}}; |
| 96 | + System.out.println(getTotalWaitingTime(k, n, reqs)); |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
0 commit comments