題目在這里https://pintia.cn/problem-sets/994805342720868352/problems/994805491530579968
#include<iostream>
#include<algorithm>
using namespace std;
struct customer
{
int arrive_t;
int need_t;
int serve_start;
};
customer customers[10000];
int windows[100]; //記錄每個視窗空閑的時間
bool cmp(customer a, customer b) {
return a.arrive_t < b.arrive_t;
}
int chooseAwindow(int arrive_t, int k) { //選擇可用時間最早的那個視窗
int choose = -1;
int earliest = 17 * 60 * 60 + 2;
for (int i = 0; i < k; i++) {
if (windows[i] < earliest) {
choose = i;
earliest = windows[i];
}
}
return choose;
}
int main() {
int n, k;
cin >> n >> k;
int count = 0;
for (int i = 0; i < n; i++) {
int hh, mm, ss, need_t;
scanf_s("%d:%d:%d %d", &hh, &mm, &ss, &need_t);
if (hh * 60 * 60 + mm * 60 + ss > 17 * 60 * 60) continue; //超過17:00:01到達
customers[count].arrive_t = hh * 60 * 60 + mm * 60 + ss;
customers[count].need_t = need_t * 60;
customers[count].serve_start = 0;
count++;
}
n = count; //這是實際能接受服務的顧客數量
if (n == 0) {
printf("0.0");
return 0;
}
sort(customers, customers + n, cmp);
for (int j = 0; j < k; j++)
windows[j] = 8 * 60 * 60; //八點開始服務
double result = 0.0;
for (int i = 0; i < n; i++) {
int choice = chooseAwindow(customers[i].arrive_t, k);
if (customers[i].arrive_t > windows[choice]) { //說明視窗等人
windows[choice] = customers[i].arrive_t + customers[i].need_t;
customers[i].serve_start = customers[i].arrive_t;
}
else { //人等視窗
result += (windows[choice] - customers[i].arrive_t);
customers[i].serve_start = windows[choice];
windows[choice] += customers[i].need_t;
}
}
int waitsum = 0;
for (int i = 0; i < n; i++)
waitsum += (customers[i].serve_start - customers[i].arrive_t);
double avg_wait = waitsum / 60.0 / n; //此處不能寫成 "/(60*n)"
printf("%.1f", result / 60.0 / n);
return 0;
}
已經考慮了17點之前到但17點之后服務的也被計算在內,但是最后一個測驗點過不了
uj5u.com熱心網友回復:
輸入輸出格式邊界條件
……
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/252768.html
標籤:C++ 語言
