您的位置:首页 > 理论基础 > 数据结构算法

uva 1203 - Argus (数据结构:优先队列+水题)

2014-07-16 11:31 435 查看
优先队列的水题

给出指令和时间,每隔一段这样的时间就输出对应的指令

如果同时有多个指令,从小到大依次输出知道输出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: