|
| 1 | +package study.week09; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.PriorityQueue; |
| 8 | + |
| 9 | +//Aging |
| 10 | +// 우선순위 큐 |
| 11 | +/** |
| 12 | + * ai 활용했음 |
| 13 | + * 우선순위 큐에서는 weight = order - start 를 이용해서, 실질적인 우선순위를 나타나게 할 수 있다. |
| 14 | + * 현재 우선순위 큐에는 현재 시간 기반 처리할 수 있는(now >= start) 인 프로세스만 pq에 추가하게 한다. |
| 15 | + * 이때 weight, time 기반으로 높은 우선순위를 poll 하고 sb에 추가하는 것을 반복하면 해결. |
| 16 | + * 여기서 order - start가 우선순위로 사용되는 이유는 다음과 같다. |
| 17 | + * (start, order) 가 (0, 5) 인것과 (3, 7) 인 것을 비교한다고 가정 해 보자. |
| 18 | + * 5-0 > 7-3 이므로 (0,5) 가 더 높은 우선순위를 가지는데 이유는, |
| 19 | + * 현재 시간 기반으로 비교가 되는데, now가 최소 3이어야 한다. 3이라 가정하면 start가 0인 프로세스는 그 전까지 선택되지 못했다는 뜻이므로 |
| 20 | + * 5+3 이 되어 우선순위가 8이 되어 있다고 판단 할 수 있는 것이다. |
| 21 | + */ |
| 22 | +public class BOJ_23088 { |
| 23 | + |
| 24 | + static class Process implements Comparable<Process> { |
| 25 | + int id; |
| 26 | + int start; |
| 27 | + int order; |
| 28 | + int time; |
| 29 | + int weight; |
| 30 | + |
| 31 | + public Process(int id, int start, int order, int time) { |
| 32 | + this.id = id; |
| 33 | + this.start = start; |
| 34 | + this.order = order; |
| 35 | + this.time = time; |
| 36 | + this.weight = order - start; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public int compareTo(Process o) { |
| 41 | + if(this.weight != o.weight){ |
| 42 | + return Integer.compare(o.weight, this.weight); // 내림차순 |
| 43 | + } |
| 44 | + |
| 45 | + if(this.time != o.time){ |
| 46 | + return Integer.compare(this.time, o.time); |
| 47 | + } |
| 48 | + |
| 49 | + return Integer.compare(this.id, o.id); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException { |
| 54 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 55 | + StringBuilder sb = new StringBuilder(); |
| 56 | + |
| 57 | + int N = Integer.parseInt(br.readLine()); |
| 58 | + Process[] processes = new Process[N]; |
| 59 | + |
| 60 | + for (int i = 0; i < N; i++) { |
| 61 | + String[] s = br.readLine().split(" "); |
| 62 | + int start = Integer.parseInt(s[0]); |
| 63 | + int order = Integer.parseInt(s[1]); |
| 64 | + int time = Integer.parseInt(s[2]); |
| 65 | + |
| 66 | + processes[i] = new Process(i + 1, start, order, time); |
| 67 | + } |
| 68 | + |
| 69 | + Arrays.sort(processes, (a, b) -> { |
| 70 | + if(a.start != b.start) return Integer.compare(a.start, b.start); |
| 71 | + return Integer.compare(a.id, b.id); |
| 72 | + }); |
| 73 | + |
| 74 | + PriorityQueue<Process> pq = new PriorityQueue<>(); |
| 75 | + |
| 76 | + int now = 0; |
| 77 | + int idx = 0; |
| 78 | + |
| 79 | + // 큐가 비어있지 않거나, 아직 큐에 넣지 않은 프로세스가 남아있는 동안 반복 |
| 80 | + while (idx < N || !pq.isEmpty()) { |
| 81 | + |
| 82 | + // 큐가 비어있다면, CPU가 쉬고 있는 상태이므로 다음 프로세스 도착 시간으로 점프 |
| 83 | + if (pq.isEmpty() && now < processes[idx].start) { |
| 84 | + now = processes[idx].start; |
| 85 | + } |
| 86 | + |
| 87 | + // '현재 시간(now)' 이하에 도착한 모든 프로세스를 우선순위 큐에 삽입 |
| 88 | + while (idx < N && processes[idx].start <= now) { |
| 89 | + pq.offer(processes[idx]); |
| 90 | + idx++; |
| 91 | + } |
| 92 | + |
| 93 | + // 우선순위가 가장 높은 프로세스를 꺼내서 실행 |
| 94 | + Process current = pq.poll(); |
| 95 | + sb.append(current.id).append(" "); |
| 96 | + |
| 97 | + // 1씩 더하지 않고 실행 시간만큼 한 번에 시간을 증가 (비선점형 스케줄링) |
| 98 | + now += current.time; |
| 99 | + } |
| 100 | + |
| 101 | + System.out.println(sb); |
| 102 | + } |
| 103 | +} |
0 commit comments