實質上是一個有截止時間的多任務調度問題,多個窗口,多個顧客,一個等待隊列,截止時間爲17點,算平均等待時間。思路是進行模擬,以i來模擬時間一秒一秒,每一秒都遍歷所有窗口,若有窗口空閒且有顧客在等待,就服務。差一個點沒過,不知是何種情況。需要考慮的情況有:
1. 全部都在17點以後到,這樣結果爲0.0
2.一個窗口服務完後,再遇到顧客,得服務
3.若顧客17點前到,但17點前無法得到服務,該顧客要不要算在等待的裏面?
代碼:
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int NUM=10005;
struct customer
{
int arrive;
int process;
}cust[NUM];
int window[105],server[105];
bool operator<(const customer &x, const customer &y)
{
return x.arrive<y.arrive;
}
// int cmp(const customer &x, const customer &y)
// {
// return
// }
int main()
{
int n,k,i,j;
int hh,mm,ss,begin,end;
freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin);
cin>>n>>k;
for(i=0;i<n;i++){
cin>>hh;
getchar();
cin>>mm;
getchar();
cin>>ss;
cin>>cust[i].process;
cust[i].arrive=hh*3600+mm*60+ss;
}
begin=8*3600;
end=17*3600;
sort(cust,cust+n);
double sum=0.0f;
int index=0;
for(j=0;j<k;j++){
window[j]=begin;//每個窗口的空閒時間都是8點
server[j]=0;//窗口都空閒
}
for(i=begin;i<=end;i++){//i來模擬每一秒的時間
for(j=0;j<k;j++){//詢問每個窗口
if(window[j]==i&&i!=begin)
server[j]=0;
if(server[j]==0&&cust[index].arrive<=i&&index<n){//有窗口空閒且有顧客在等待
window[j]=i+cust[index].process*60;//服務
sum+=i-cust[index].arrive;//加上其等待時間
//printf("%d\n",i-cust[index].arrive);
index++;
server[j]=1;//窗口設爲忙
}
}
if(index==n||cust[index].arrive>end)//顧客服務完了或顧客到達時間超過17點了
break;
}
if(index>0){
sum=sum/(60*index);
printf("%.1lf",sum);
} else {//所有顧客都17點後纔來
printf("0.0");
}
return 0;
}
若要看AC代碼,據說用優先隊列特別簡單,請參考:
http://linest.iteye.com/blog/1423588