PAT (Advanced Level) 1017 Queueing at Bank

题目链接:1017 Queueing at Bank

本题审题较为容易,但是实现起来过程实在繁琐。话不多说,具体看代码。

AC代码:

#include 
#include 
#include 
#include 
using namespace std;

struct mytime
{
    int h,m,s,service_time;
};
bool cmp(mytime a,mytime b)
{
    if(a.h != b.h)
        return a.h < b.h;
    else if(a.m != b.m)
        return a.m < b.m;
    else
        return a.s < b.s;
}
int length_time(mytime a,mytime b)
{
    int t = (a.h - b.h)*3600 + (a.m - b.m)*60 + (a.s - b.s);
    return t;
}
mytime update_time(mytime a,int m)
{
    a.m += m;
    if(a.m >= 60)
    {
        a.m -= 60;
        a.h++;
    }
    return a;
}

int main()
{
    int n,k,con = 0,sum_time = 0,service_window;
    cin >> n >> k;
    vector<mytime> windows(k);
    mytime customers[n],temp,begin_time;
    begin_time.h = 8,begin_time.m = 0,begin_time.s = 0;
    for(int i = 0; i != n; ++i)
    {
        scanf("%d:%d:%d %d",&temp.h,&temp.m,&temp.s,&temp.service_time);
        if(temp.h < 17)//若到达时间晚于17点,直接舍弃该数据
        {
            customers[con++] = temp;
        }
    }
    cout << con << '\n';
    sort(customers,customers + con,cmp);
    int j = 0;
    while(j != con)
    {
        if(customers[j].h < 8)//若到达时间早于8点,则总等待时间加上该时间到8点的时长并更新到达时间为8点
        {
            sum_time += length_time(begin_time,customers[j]);
            customers[j].h = 8,customers[j].m = 0,customers[j].s = 0;
        }
        ++j;
    }

    j = 0;
    temp.h = 24,temp.m =0,temp.s = 0;
    while(j != con)
    {
        if(j < k)//不需等待
        {
            windows[j] = update_time(customers[j],customers[j].service_time);//服务完成时间
            if(length_time(temp,windows[j]) > 0)
            {
                temp = windows[j];
                service_window = j; //最早完成服务的窗口
            }
        }
        else
        {
            if(length_time(customers[j],temp) >= 0)//到达时有空窗口
            {
                windows[service_window] = update_time(customers[j],customers[j].service_time);
            }
            else//到达时无空窗口
            {
                sum_time += length_time(temp,customers[j]);
                windows[service_window] = update_time(temp,customers[j].service_time);
            }
            temp = windows[0],service_window = 0;//初始化
            for(int i = 0; i != k; ++i)
            {
                if(length_time(temp,windows[i]) > 0)
                {
                    temp = windows[i];
                    service_window = i; //最早完成服务的窗口
                }
            }
        }
        ++j;

    }
    float avg_waittime = sum_time * 1.0 / 60 / con;
    printf("%.1f",avg_waittime);
    return 0;
}

你可能感兴趣的:(PTA)