题目描述
分析:复杂模拟,模拟银行等待,使用优先队列进行模拟
#include<vector>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<cLIMITS>
using namespace std;
struct customer{
int arrive;
int process;
}cs;
bool cmp(const customer& c1,const customer& c2){
return c1.arrive<c2.arrive;
}
int main(){
int open=8*60*60;
int close=17*60*60;
int n,k;
scanf("%d%d",&n,&k);
priority_queue<int,vector<int>,greater<int>> window;
vector<customer> cus;
double wait=0;
while(n--){
int h,m,s;
scanf("%d:%d:%d %d",&h,&m,&s,&cs.process);
cs.arrive=(h*60+m)*60+s;
cs.process*=60;
if(cs.arrive<=close){
cus.push_back(cs);
}
}
sort(cus.begin(),cus.end(),cmp);
for(int i=0;i<cus.size();i++){
if(cus[i].arrive<open){
wait+=open-cus[i].arrive;
cus[i].arrive=open;
}
if(window.size()==0){
window.push(cus[i].arrive+cus[i].process);
}
else{
int time=window.top();
while(time<=cus[i].arrive){
window.pop();
if(!window.empty())
time=window.top();
else{
time=INT_MAX;
}
}
if(window.size()<k){
window.push(cus[i].arrive+cus[i].process);
}
else{
time=window.top();
window.pop();
wait+=time-cus[i].arrive;
window.push(time+cus[i].process);
}
}
}
wait/=cus.size()*60.0;
printf("%.1f",wait);
return 0;
}