1. 程式人生 > >1017 Queueing at Bank (我自己寫的模擬時間的版本)

1017 Queueing at Bank (我自己寫的模擬時間的版本)

Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤) - the total number of customers, and K (≤) - the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS

- the arriving time, and P - the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. 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.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input:

7 3
07:55:00 16
17:00:01 2
07:59:59 15
08:01:00 60
08:00:00 30
08:00:02 2
08:03:00 10

Sample Output:

8.2

思路:
  1、設定了視窗個大小的佇列,但是根據題意可知,佇列大小都是1,在輸入的時候直接去除不符合題意的資料,然後把到達時間換算成秒,進行排序。
  2、如果一個客戶在八點前到達且視窗有空閒,則等待時間為8點減去其到達時間;如果一個客戶到來時視窗有空閒,則其等待時間為0;否則該客戶的等待時間為
  其所接受服務視窗的前一個客戶的開始接受服務的時間+前一個客戶對應的服務時間減去當前客戶的到達時間
  3、這種方法很不好,易錯且複雜。
#include<iostream>
#include<sstream>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdio>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;

struct Node
{
    int hour,minute,second;
    long long time;
    int cost;
};

struct Data
{
    Node node;
    long long  cost;
};


bool cmp(Node& A,Node &B)
{
    return A.time<B.time;
}

int main()
{
    int n,k;
    cin>>n>>k;
    vector<Node> node(n);
    for(int i=0; i<n; i++)
    {
        scanf("%d:%d:%d",&node[i].hour,&node[i].minute,&node[i].second);
        node[i].time=node[i].hour*60*60+node[i].minute*60+node[i].second;
        scanf("%d",&node[i].cost);
    }
    sort(node.begin(),node.end(),cmp);
    long long cost[n];
    long long int busy[k];
    //memset(busy,0,sizeof(busy));
    for(int i=0; i<k; i++)
        busy[i]=-1;
    int i=0;
    long long int minTemp;
    long long int time=8*60*60;
    bool flag=true;
    bool over=false;
    queue<Data> qu[k];
    do
    {
        over=false;
        for(int j=0; j<k; j++)
        {
           // cout<<busy[j]<<" ";
            Data temp=qu[j].front();
            if(qu[j].size()==1)
            {
                qu[j].front().cost-=minTemp;
                if(qu[j].front().cost==0)
                {
                    qu[j].pop();
                  //  cout<< "q   "<<qu[j].size()<<endl;
                }
            }
            if(qu[j].size()==0&&i<n)
            {
                if(node[i].time<17*60*60+1)
                {
                    Data data;

                    data.cost=node[i].cost;

                    //busy[j]=node[i].cost;
                    if(node[i].time<=8*60*60&&flag)
                    {
                        cost[i]=8*60*60-node[i].time;
                        node[i].time=8*60*60;
                        data.node=node[i];
                    }
                    else
                    {
                        //cout<<node[i].hour<<" "<<node[i].minute<<" "<<node[i].second<<endl;
                        //cout<<temp.node.hour<<" "<<temp.node.minute<<" "<<temp.node.second<<endl;
                        if(node[i].time<=temp.node.time+temp.node.cost*60)
                        {
                            cost[i]=temp.node.time+temp.node.cost*60-node[i].time;
                            node[i].time=temp.node.time+temp.node.cost*60;
                            data.node=node[i];
                        }
                        else
                        {
                            cost[i]=0;
                            //node[i].time=temp.node.time+temp.node.cost*60;
                            data.node=node[i];
                        }

                    }
                    qu[j].push(data);
                    //cout<<" cost "<<i<<" "<<cost[i]/60<<endl;
                }
                else
                {
                    cost[i]=-1;
                }

                i++;
                //cout<<i<<endl;
            }

        }
        //cout<<endl;
        flag=false;
        minTemp=100000;
        for(int j=0; j<k; j++)
        {

            if(qu[j].size()!=0)
            {
                minTemp=min(qu[j].front().cost,minTemp);
                over=true;

            }
        }
        //cout<<minTemp<<endl;
        time+=minTemp*60;
    }
    while(over);



    long long int sum=0;
    int num=0;
    for(int i=0; i<n; i++)
    {
       // cout<<cost[i]<<endl;
        if(cost[i]!=-1)
        {
            sum+=cost[i];
            num++;
        }
    }
    printf("%.1f\n",sum/(num*60.0));
    return 0;
}