注意一点,在17点之前(包括17点)到来的都应该得到服务。
代码按秒推进。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
typedef struct{
int cost;
int arr_time;
int start_time;
}Customer;
int window[105];
Customer cus[10005];
bool cmp(Customer c1, Customer c2)
{
return c1.arr_time <= c2.arr_time;
}
int find_avai(int k)
{
for(int i=0; i < k; i ++)
{
if(window[i] == -1)
return i;
}
return -1;
}
int main()
{
int n, m, k, q, i;
memset(window, -1, sizeof(window));
int start_time = 8 * 3600, end_time = 17 * 3600;
scanf("%d %d", &n, &k);
int hh, mm, ss, cost;
int index = 0;
for(i = 0; i < n; i ++)
{
scanf("%d:%d:%d %d", &hh, &mm, &ss, &cost);
if(cost > 60) cost = 60;
int time = hh * 3600 + mm * 60 + ss;
if(time <= end_time)
{
cus[index].arr_time = time;
cus[index].cost = cost * 60;
cus[index++].start_time = -1;
}
}
n = index;
sort(cus, cus+n, cmp);
i = 0;
double sum = 0.0;
int current_time = start_time;
while(i < n)
{
int index = find_avai(k);
while(i < n && cus[i].arr_time<=current_time && index!=-1)
{
window[index] = i;
sum += (current_time-cus[i].arr_time);
index = find_avai(k);
i ++ ;
}
current_time ++ ;
for(int j=0; j<k; j++)
{
if(window[j] != -1)
{
cus[ window[j] ].cost -- ;
if(cus[ window[j] ].cost <= 0)
{
window[j] = -1;
}
}
}
}
printf("%.1f\n", sum/n/60.0);
return 0;
}