queue模擬題 注意題目所求得是平均等待時間 還有 如果顧客在8點之前到達,需等到8點才能得到服務 顧客如果在17點之後到達,銀行不為其提供服務,但一旦到達時間在17點之前,即使結束時間在17點之後 ,銀行也要為其服務 [cpp] #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<stdio.h> #include<string.h> using namespace std; #define S 8*60*60 #define E 17*60*60 #define INF 0x7fffffff struct node { int time; int process; int begin; int leave; }; int max(int a,int b) { return a>b?a:b; } int cmp(struct node a,struct node b) { return a.time<b.time; } int main() { int n,k,i,h,m,s,j,min,index,t; while(scanf("%d%d",&n,&k)!=EOF) { vector<struct node >cus(n); //vector<queue<int> >winque(k); vector<int>now(k,S); for(i=0;i<n;i++) { scanf("%d:%d:%d%d",&h,&m,&s,&cus[i].process); cus[i].process*=60; cus[i].time=(h*60+m)*60+s; } sort(cus.begin(),cus.end(),cmp); for(i=0;i<n;i++) { min=INF; for(j=0;j<k;j++) if(min>now[j]) { min=now[j]; index=j; } cus[i].begin=max(now[index],cus[i].time); cus[i].leave=cus[i].begin+cus[i].process; now[index]=cus[i].leave; } t=0; s=n; for(i=0;i<n;i++) { //printf("%d %d %d %d\n",i,cus[i].time,cus[i].begin,cus[i].leave); if(cus[i].time<=E)// Anyone arrives early will have to wait in line till 08:00, //and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average. //任何人只要在五點之前到達 銀行就要為其服務 { t+=cus[i].begin-cus[i].time; //printf("%d\n",t); } else s--; } if(s)printf("%.1lf\n",t/60.0/s);//求 等待 時間的平均值 else printf("0.0\n"); } return 0; }