简单的优先队列和堆的应用
#include <stdio.h>
#include <string.h>
#include <map>
#include <queue>
using namespace std;
char tmp[1000];
char cmd[1000];
typedef int TASK_INDEX;
typedef int TIME_INTERVAL;
typedef struct task{
int index;
int run_time;
}task;
bool operator < (const task &a, const task &b){
if(a.run_time > b.run_time)
return true;
else if(a.run_time < b.run_time)
return false;
else{
if(a.index > b.index)
return true;
else
return false;
}
}
#pragma warning(disable:4996)
int main(void){
int index, time, k, i;
map<TASK_INDEX, TIME_INTERVAL> record;
priority_queue<task> pq;
task run_task;
//freopen("input.dat", "r", stdin);
while(gets(tmp)){
if(0 == strcmp("#", tmp))
break;
sscanf(tmp, "%s %d %d", cmd, &index, &time);
record[index] = time;
}
scanf("%d\n", &k);
for(map<TASK_INDEX, TIME_INTERVAL>::iterator it=record.begin(); it!=record.end(); it++){
run_task.index = it->first;
run_task.run_time = it->second;
pq.push(run_task);
}
for(i=1; i<=k; i++){
printf("%d\n", pq.top().index);
run_task = pq.top();
run_task.run_time += record[run_task.index];
pq.pop();
pq.push(run_task);
}
return 0;
}