PAT_1017. Queueing at Bank

来源:互联网 发布:网络直播的定义 编辑:程序博客网 时间:2024/04/28 10:08

这题只要到达时间在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!"*/
0 0