用堆或者priority_queue应该更好。
解决此题关键是要注意每次输出后各个query的period如何变化:输出的那个要恢复为原来的;其余的则减去已经逝去的时间。
代码:
#include <cstdlib> #include <algorithm> #include <iostream> #include <vector> using namespace std; const int MAX_SIZE = 1005; struct Query { int q_num; int period; int ole_period; //保存原来的period }; Query q[MAX_SIZE]; /*排序规则*/ bool cmp(const Query &a, const Query &b) { if(a.period < b.period) return true; else if(a.period == b.period) { if(a.q_num < b.q_num) return true; else return false; } else return false; } int main() { int index = 0; string s; while(cin >> s, s != "#") { cin >> q[index].q_num >> q[index].period; q[index].ole_period = q[index].period; index++; } int k; cin >> k; for(int i = 0; i < k; i++) { sort(q, q + index, cmp); /*输出具有最小period的query*/ cout << q[0].q_num << endl; for(int j = 1; j < index; j++) q[j].period -= q[0].period; /*输出后,要恢复q[0]的period*/ q[0].period = q[0].ole_period; } return 0; }
本文介绍了一种利用堆或priority_queue的数据结构来优化查询调度的方法。重点在于每次输出后更新各个查询的时间周期:已输出的查询恢复初始周期,其余查询减去相应时间。通过这种方式,确保了调度的有效性和准确性。
536

被折叠的 条评论
为什么被折叠?



