将任务以创建时间、PM的ID排序输入到队列A中,后期对队列A中的任务进行分配。时间k从0开始递增,当前时间k与Idea提出的时间相等时将其从队列A踢出,并加入到任务队列B中。每个任务的完成时刻等于当前时间加上完成任务所需的时间。若程序员没有做工,则从任务队列B中获取任务,工作时间等于任务所需时间,时间k每次增加1,程序员所需工作时间减少1,减完则获取任务。参考了一个Java版本的题解。
#include <stdio.h> #include <iostream> #include <queue> #include <map> #include <string.h> using namespace std; class task { public: int m_id; int m_create_time; int m_proirity; int m_need_time; task(int id, int create_time, int proirity, int need_time) { m_id = id; m_create_time = create_time; m_proirity = proirity; m_need_time = need_time; } bool operator == (const task& other) const { return m_id == other.m_id && m_create_time==other.m_create_time && m_proirity == other.m_proirity && m_need_time == other.m_need_time; } bool operator < (const task& other) const { if(m_proirity == other.m_proirity) { if(m_need_time == other.m_need_time) { if(m_create_time == other.m_create_time) { return m_id > other.m_id; }else { return m_create_time > other.m_create_time; } } else { return m_need_time > other.m_need_time; } } else { return m_proirity > other.m_proirity; } } void print() { cout << m_id << " " << m_create_time << " " << m_proirity << " " << m_need_time << endl; } }; struct cmper { bool operator() (const task& a, const task& b) { if((a.m_create_time) == (b.m_create_time)) { return (a.m_id) > (b.m_id); } else { return (a.m_create_time) > (b.m_create_time); } } }; int main() { priority_queue<task, vector<task>, cmper > m_queue_create; priority_queue<task> m_queue_task; vector<task> m_task_vec; map<task,int> m_task_map; int n,m,p; cin >> n >> m >> p; for(int i=0;i<p;i++) { int a,b,c,d; cin >> a >> b >> c >> d; task t = task(a,b,c,d); m_queue_create.push(t); m_task_map[t] = 0; m_task_vec.push_back(t); } //时间k从0开始递增; int k = 0; int* programer = new int[m]; memset(programer,0,sizeof(int) * m); while(!m_queue_create.empty() || !m_queue_task.empty()) { while(!m_queue_create.empty()) { task t = m_queue_create.top(); if(t.m_create_time == k) { //t.print(); m_queue_task.push(t); m_queue_create.pop(); } else { break; } } //程序员从0到m; for(int i=0;i<m;) { if(programer[i] > 0) { //程序员做工; programer[i]--; i++; }else { //程序员接受任务; if(m_queue_task.empty()) { //无任务可接; i++; }else{ //有任务可接; task t = m_queue_task.top(); programer[i] = t.m_need_time; m_task_map[t] = k+t.m_need_time; m_queue_task.pop(); } } } k++; } for(auto& v:m_task_vec) { cout << m_task_map[v] << endl; } return 0; }