一道水题,优先队列模拟多路归并
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
struct event{
int index;
int zhouqi;
};
vector<struct event> event_v;
struct node{
int index;
int time;
};
bool operator < (const struct node &a, const struct node &b){
if(a.time < b.time)
return false;
else if(a.time > b.time)
return true;
else{
if(a.index < b.index)
return false;
else
return true;
}
}
priority_queue<struct node> pq;
char buf[1000];
int main(void){
int e, p, k, i, j;
struct event event_ins;
struct node node_ins;
freopen("input.dat", "r", stdin);
event_v.clear();
while(!pq.empty()) pq.pop();
while(1){
gets(buf);
if( 0 == strcmp(buf, "#") )
break;
sscanf(buf, "Register %d %d", &(event_ins.index), &(event_ins.zhouqi));
event_v.push_back(event_ins);
}
scanf("%d", &k);
for(i=0; i<event_v.size(); i++){
node_ins.index = event_v[i].index;
node_ins.time = 0;
for(j=0; j<k; j++){
node_ins.time += event_v[i].zhouqi;
pq.push(node_ins);
}
}
for(i=0; i<k; i++){
printf("%d\n", pq.top().index);
pq.pop();
}
return 0;
}