PAT_1017. Queueing at Bank

這題只要到達時間在17:00:00之前,即使排隊輪到的時間超過17:00:00,仍然可以接受服務

// 1017_Queueing at Bank.cpp : 定義控制檯應用程序的入口點。
//1017. Queueing at Bank (25)
//
//時間限制
//400 ms
//內存限制
//65536 kB
//代碼長度限制
//16000 B
//判題程序
//Standard
//作者
//CHEN, Yue
//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 (<=10000) - the total number of customers, and K (<=100) - 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



#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "string.h"
#include "stdlib.h"
#include "string"
#include "algorithm"
using namespace std;

struct custom{
    string arrive;
    int time;
}cus[10000];

int spare_t[110];

bool cmp(struct custom a, struct custom b){
    return a.arrive<b.arrive;
}

int trans(string a){
    int h = atoi(a.substr(0,2).c_str());
    int m = atoi(a.substr(3,2).c_str());
    int s = atoi(a.substr(6,2).c_str());
    return h*3600+m*60+s;
}

int main()
{
    int n,k;
    while(cin>>n>>k){
        for(int i = 0;i<n;i++){
            cin>>cus[i].arrive>>cus[i].time;
        }
        sort(cus,cus+n,cmp);

        for(int i = 0;i<k;i++)
            spare_t[i] = trans("08:00:00");
        float aver_t = 0.0;      //結果
        int cursor_cus = 0;     //記錄當前隊伍中第一個顧客
        int cursor_win = 0;     //指示k個窗口中最早有空閒時間的
        while(cursor_cus<n){
            for(int i =0;i<k;i++){
                if(spare_t[i]<spare_t[cursor_win])
                    cursor_win = i;
            }

            if(trans(cus[cursor_cus].arrive)<=trans("17:00:00")){
                if(trans(cus[cursor_cus].arrive) >= spare_t[cursor_win]){
                    spare_t[cursor_win] = trans(cus[cursor_cus].arrive) + cus[cursor_cus].time*60;
                }
                else{
                    aver_t += spare_t[cursor_win] - trans(cus[cursor_cus].arrive);
                    spare_t[cursor_win] += cus[cursor_cus].time * 60;
                }
                cursor_cus++;
                cursor_win = 0;
            }
            else
                break;
        }

        if(cursor_cus)
            printf("%.1lf\n",aver_t/((cursor_cus) * 60));   
        else
            cout<<"0.0"<<endl;
    }
    return 0;
}

/*

c_str() 以 char* 形式傳回 string 內含字符串
如果一個函數要求char*參數,可以使用c_str()方法: 
string s = "Hello World!";
printf("%s", s.c_str()); //輸出 "Hello World!"

*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章