題目鏈接:?http://poj.org/problem?id=2051
思路分析:
優先級問題,使用優先隊列求解;當執行某個任務后,再增加一個任務到隊列中,
該任務的優先級為執行任務的時間加上其時間間隔,如此反復直到求出前K個執行任務。
?
代碼:
#include <iostream> #include <queue> using namespace std;struct Argu {int QNum;int period;int time;bool operator<(const Argu &rhs) const{if (time > rhs.time)return true;if (time == rhs.time)return QNum > rhs.QNum;return false;} };int main() {int k;char tmp[10];priority_queue<Argu> heap;while (scanf("%s", tmp) != EOF && strcmp(tmp, "#") != 0){Argu command;scanf("%d%d", &command.QNum, &command.period);command.time = command.period;heap.push(command);}scanf("%d", &k);for (int i = 0; i < k; ++i){Argu ans;ans = heap.top();heap.pop();ans.time += ans.period;heap.push(ans);printf("%d\n", ans.QNum);}return 0; }
?