POJ-2051-Argus-sort水过

用堆或者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; }   

你可能感兴趣的:(struct,String,query)