PAT A1017 优先队列

31次阅读

共计 1061 个字符,预计需要花费 3 分钟才能阅读完成。

这道题有点像优先队列的思想,简而言之就是挑选最小的入队处理,如果有多个队列就进行多个队列的处理;
借鉴的思想是采用记录每个队列中的任务完成时间,然后在读入任务的时候进行轮询,选择结束时间最小的那个队列,然后进行处理和等待时间的计算;
代码如下:

#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
int come, time;
} tempcustomer;
bool cmp1(node a, node b) {
return a.come < b.come;
}
int main() {
int n, k;
scanf(“%d%d”, &n, &k);
vector<node> custom;
for(int i = 0; i < n; i++) {
int hh, mm, ss, time;
scanf(“%d:%d:%d %d”, &hh, &mm, &ss, &time);
int cometime = hh * 3600 + mm * 60 + ss;
if(cometime > 61200) continue;
tempcustomer = {cometime, time * 60};
custom.push_back(tempcustomer);
}
sort(custom.begin(), custom.end(), cmp1);
vector<int> window(k, 28800);
double result = 0.0;
for(int i = 0; i < custom.size(); i++) {
int tempindex = 0, minfinish = window[0];
for(int j = 1; j < k; j++) {
if(minfinish > window[j]) {
minfinish = window[j];
tempindex = j;
}
}
if(window[tempindex] <= custom[i].come) {
window[tempindex] = custom[i].come + custom[i].time;
} else {
result += (window[tempindex] – custom[i].come);
window[tempindex] += custom[i].time;
}
}
if(custom.size() == 0)
printf(“0.0”);
else
printf(“%.1f”, result / 60.0 / custom.size());
system(“pause”);
return 0;
}

正文完
 0