uva 1203 - Argus (数据结构:优先队列+水题)
2014-07-16 11:31
435 查看
优先队列的水题
给出指令和时间,每隔一段这样的时间就输出对应的指令
如果同时有多个指令,从小到大依次输出知道输出K个指令
用最大公约数优化了下,时间每次加1的话应该也不会超时
代码如下:
给出指令和时间,每隔一段这样的时间就输出对应的指令
如果同时有多个指令,从小到大依次输出知道输出K个指令
用最大公约数优化了下,时间每次加1的话应该也不会超时
代码如下:
#include <queue> #include <cstdio> #include <iostream> using namespace std; string str; int ins[1100], times[1100]; priority_queue<int, vector<int>, greater<int> > q; int gcd(int a, int b) { return b ? gcd(b, a%b) : a; } int main(void) { int cnt, time_gcd, K, num, cur_time, i; cnt = 0; time_gcd = 1; while(cin >> str && str!="#") { cin >> ins[cnt] >> times[cnt]; time_gcd = gcd(time_gcd, times[cnt]); cnt++; } cin >> K; while(!q.empty()) q.pop(); num = 0; cur_time = 0; while(true) { cur_time += time_gcd; for(i=0; i<cnt; ++i) { if(cur_time % times[i] == 0) { q.push(ins[i]); } } while(!q.empty()) { cout << q.top() << endl; q.pop(); num++; if(num == K) break; } if(num == K) break; } return 0; }
相关文章推荐
- UVA 1203 Argus【优先队列】
- I - Argus UVA - 1203 --优先队列
- 指南 第三章 例题3 UVALive 3135 Argus(优先队列的应用)
- UVALive3135 UVA1203 POJ2051 ZOJ2212 Argus【优先队列+优先搜索】
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
- UVALive 3135--Argus+自己定义优先队列的优先规则
- uva1203 - Argus (排序、优先级队列)
- 【优先队列之多路归并】UVALive 3135 Argus
- UVALive - 3135 Argus【优先队列】
- uva 11995 栈,队列,优先队列,等基本数据结构的应用与理解
- UVALive - 3135 - Argus (优先队列!!)
- UVA 136 - Ugly Numbers【巧用优先队列】
- 数据结构【三】:简单优先队列PriorityQueue
- UVa-136-Ugly Number STL 优先队列的基础使用
- 用不同的数据结构实现 优先队列 的时间复杂度分析
- Argus--[优先队列]
- 数据结构实现之最小索引优先队列
- 数据结构Java实现07----队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
- UVA11995----数据结构水题