zoukankan      html  css  js  c++  java
  • PAT A1017 Queueing at Bank (25 分)——队列

    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 (104​​) - 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 <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <string>
    #include <set>
    #include <map>
    using namespace std;
    const int maxn = 110;
    int n,k;
    queue<int> q[maxn];
    struct person {
        int arr;
        int arr_const;
        int start;
        int pro;
        int wait=0;
    };
    bool cmp(person p1, person p2) {
        return p1.arr < p2.arr;
    }
    vector<person> v;
    int h, m, s, process;
    int main(){
        int count = 0,time = 0;
        cin >> n >> k;
        for (int i = 0; i < n; i++) {
            person p;
            scanf("%d:%d:%d %d", &h, &m, &s, &process);
            getchar();
            time = 3600 * h + 60 * m + s;
            p.arr = time;
            p.start = time;
            p.arr_const = time;
            p.pro = process*60;
            p.wait = 0;
            if (time > 17 * 3600)continue;
            v.push_back(p);
            count++;
        }
        sort(v.begin(), v.end(), cmp);
        int now = 8*3600;
        for (int i = 0; i < count; i++) {
            if (v[i].arr < now) {
                v[i].wait = now - v[i].arr;
                v[i].start = now;
                v[i].arr = now;
            }
        }
        int now_time = 8 * 3600;
        int fast_k = 0;
        for (int i = 0; i < count - k; i++) {
            int fast = 360000;
            for (int j = 0; j < i+k; j++) {
                if (v[j].start + v[j].pro < fast) {
                    fast = v[j].start + v[j].pro;
                    fast_k = j;
                }
            }
            v[fast_k].start = 360000;
            now_time = fast;
            if (now_time > v[i + k].arr) {
                v[i + k].wait += now_time - v[i + k].arr;
                v[i + k].start = now_time;
            }
        }
    
        float mean = 0;
        for (int i = 0; i < count; i++) {
            mean += v[i].wait;
        }
        if (count == 0)printf("0.0");
        else {
            mean /= count;
            printf("%.1f", mean/60);
        }
        system("pause");
        return 0;
    }

    注意点:又是一道逻辑挺简单的题,还是花了1个多小时才ac,前半部分思路是没问题的,后面窗口等待走歪了,一直在顾客上做文章,其实只要把每个窗口的开始服务时间更新,到的比窗口服务时间早的就等待,否则直接开始不用等。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    一个用php抓取网页中电子邮箱的实例
    jQuery中ajax加载文本
    phantomjs介绍(js网页截屏、javascript网页解析渲染工具)
    delphi 如何获取大于2G的物理内存大小[delphi]
    DELPHI——过程与函数[delphi]
    Delphi初始化和析构 initialization和finalization
    Delphi执行DOS命令显示到文本框
    MYSQL基础集合函数(count,sun,avg,max,min)
    MYSQL基础链接查询(内连接查询,外连接查询)
    MYSQL基础合并查询结果(UNION)
  • 原文地址:https://www.cnblogs.com/tccbj/p/10388117.html
Copyright © 2011-2022 走看看