【题意】
还是银行服务客户的问题,和1014有点像,区别在于:
1. 这一次所有的客户不是一开始都到达的,到达时间可能比开门时间08:00:00早,也可能比关门时间17:00:00晚。前者需要等到开门,而后者不能得到服务。每个窗口前只能站一个人(1014黄线内可以不止一个,即每个服务队列容量可以大于1);
2. 17:00:00之前到的客户,即使开始被服务时间已经晚于17:00:00,也可以得到服务;
3. 每个客户被服务时间不超过60min;
【思路】
直接排除到达时间晚于17:00:00的客户。然后和1014一样,依然模拟时间的推进,每次推进的时候
维护一个最短推进时间,在所有窗口正在服务的客户的最短剩余服务时间和未到达客户的最短空等时间中再取一个最小值;
【注意点】
1. 必须注意临界情况!!!
#PAT最喜欢搞这种恶心的临界情况了# 若没有一个在17:00:00点或之前到达的客户,则平均等待时间为0.0;
2. 推进时间轴时,维护一个最短推进时间,在所有窗口正在服务的客户的最短剩余服务时间和未到达客户的最短空等时间中再取一个最小值。刚开始一直WA就是没有在最短剩余服务时间和最短空等时间之间再取一个最小值导致的
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef struct{
int arriveTime;
int processTime;
int startTime;
int waitTime;
}customer;
bool cmp(customer c1, customer c2){
return c1.arriveTime<c2.arriveTime;
}
int main(int argc, char const *argv[])
{
queue<customer> windows[100];
vector<customer> customers;
customer tmp;
int start,deadline;
int n,k,validCnt;
string arriveTime;
int processTime;
start = 8*3600;
deadline = 17*3600;
validCnt=0;
cin >> n >> k;
for(int i = 0; i < n; ++i){
cin >> arriveTime >> processTime;
tmp.arriveTime = ((arriveTime[0]-'0')*10+arriveTime[1]-'0')*3600
+((arriveTime[3]-'0')*10+arriveTime[4]-'0')*60
+(arriveTime[6]-'0')*10+arriveTime[7]-'0';
tmp.processTime = processTime*60;
tmp.waitTime = 0;
if(tmp.arriveTime<=deadline){
validCnt++;
customers.push_back(tmp);
}
}
sort(customers.begin(), customers.end(), cmp);
int currentTime = start;
float totalWait = 0.0;
vector<customer>::iterator it = customers.begin();
while(it!=customers.end()){
int min = 3601;
int nullTime = 3601;//柜台空等时间
for(int i=0; i<k; i++){
if(!windows[i].empty()){
if(windows[i].front().processTime<min){
min = windows[i].front().processTime;
}
}
else if(it!=customers.end()){
if((*it).arriveTime<=currentTime){
(*it).startTime = currentTime;
(*it).waitTime = (*it).startTime-(*it).arriveTime;
totalWait += (*it).waitTime;
windows[i].push(*it);
it++;
if(windows[i].front().processTime<min){
min = windows[i].front().processTime;
}
}
else if((*it).arriveTime-currentTime<min){
min = (*it).arriveTime-currentTime;
}
}
else
break;
}
currentTime += min;
for(int i=0; i<k; i++){
if(!windows[i].empty()){
windows[i].front().processTime -= min;
if(windows[i].front().processTime==0){
windows[i].pop();
}
}
}
}
if(validCnt==0){
totalWait = 0.0;
}
else{
totalWait = totalWait/validCnt;
}
totalWait /= 60;
printf("%.1f", totalWait);
customers.clear();
system("pause");
return 0;
}