码迷,mamicode.com
首页 > 其他好文 > 详细

1017 Queueing at Bank (我自己写的模拟时间的版本)

时间:2019-01-05 22:52:12      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ios   win   algorithm   eof   minutes   rri   map   return   available   

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;
}

 

 

1017 Queueing at Bank (我自己写的模拟时间的版本)

标签:ios   win   algorithm   eof   minutes   rri   map   return   available   

原文地址:https://www.cnblogs.com/zhanghaijie/p/10226379.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!