PAT甲级 1017 Queueing at Bank (25分)

思路比较简单,时间复杂度也还行,所有时间数据转化为秒。

#include <cstdio>
#include <algorithm>
using namespace std;
struct student{
    int second, process;
}stu[10010], bank[110];
bool cmp(student a, student b)
{
    return a.second < b.second;
}
bool cmp2(student a, student b)
{
    return a.process + a.second < b.process + b.second;
}
int main()
{
    int n, k, hh, mm, ss, p, count = 0, sum = 0;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < k; i++){
        bank[i].second = 8 * 3600;
        bank[i].process = 0;
    }
    for(int i = 0; i < n; i++){
        scanf("%d:%d:%d%d", &hh, &mm, &ss, &p);
        if(hh * 3600 + mm * 60 + ss <= 17 * 3600){
            stu[count].second = hh * 3600 + mm * 60 + ss;
            stu[count].process = p * 60;
            count++;
        }
    }
    sort(stu, stu + count, cmp);
    for(int i = 0; i < count; i++){
        sort(bank, bank + k, cmp2);
        int t1 = stu[i].second, t2 = bank[0].process + bank[0].second;
        if(t1 >= t2) bank[0] = stu[i];
        else{
            sum += t2 - t1;
            bank[0].second = t2;
            bank[0].process = stu[i].process;
        }
    }
    printf("%.1f", sum / 60.0 / count);
    return 0;
}
发布了30 篇原创文章 · 获赞 0 · 访问量 384

猜你喜欢

转载自blog.csdn.net/qq_33942309/article/details/104282852