우선 순위 큐를 사용하는 문제이다.
1. 디스크가 놀고 있다면, 먼저 들어온 거 실행하기.
2. 현재 실행 할 수 있는 디스크 중 가장 짧은 실행시간을 가지고 있는 것을 실행. -> 우선 순위 큐를 사용해서 하면된다.
하나의 job을 실행하고, 현재 실행 시간을 체크하고, 실행 시간 중에 실행 할 수 있는 job을 우선 순위 큐로 선택하여 실행해준다.
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
struct cmp{
bool operator()(pair<int, int> a, pair<int, int> b){
if(a.second > b.second) return true;
return false;
}
};
int solution(vector<vector<int>> jobs) {
int answer = 0;
priority_queue<pair<int,int>,vector<pair<int,int> >,cmp> pq;
int time = 0;
int cnt = 0;
int i = 0;
int start = -1;
while(i < jobs.size()){
for(int i = 0 ; i < jobs.size() ; i++){
if(jobs[i][0] > start && jobs[i][0] <= time){
pq.push(make_pair(jobs[i][0], jobs[i][1]));
}
}
if(!pq.empty()){
cout << start << " " << time << " " << pq.top().first << " " << pq.top().second << "\n";
start = time;
time += pq.top().second;
answer += (time - pq.top().first);
pq.pop();
i++;
}
else{
time++;
}
}
return answer/jobs.size();
}