PAT甲級1017 (模擬排序)
課前分析
感覺和1016差不多, 就模擬一下幾個佇列, 有空位立即插入並計算等待時間, 沒空位計算最早處理完的使用者的離去時間,並更新為當前時間等等。 (1) vector a(10); //定義了10個整型元素的向量(尖括號中為元素型別名,它可以是任何合法的資料型別),但沒有給出初值,其值是不確定的。 (2)vector a(10,1); //定義了10個整型元素的向量,且給出每個元素的初值為1 (3)vector a(b); //用b向量來建立a向量,整體複製性賦值 (4)vector a(b.begin(),b.begin+3); //定義了a值為b中第0個到第2個(共3個)元素 (5)int b[7]={1,2,3,4,5,9,8};vector a(b,b+7); //從陣列中獲得初值
題目
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 (<=10000) – 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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
int come,time;
}customer;
bool cmp(node a,node b){
return a.come<b.come;
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
vector <node> cus;
for(int i=0;i<n;i++){
int hh,mm,ss,time;
scanf("%d:%d:%d %d",&hh,&mm,&ss,&time);
int cometime=hh*3600+mm*60+ss;
if(cometime>61200) continue;
customer={cometime,time*60};
cus.push_back(customer);
}
sort(cus.begin(),cus.end(),cmp);
vector<int> window(k,28800);//完成任務時間,初始為8小時的秒鐘數
double res=0.0;
for(int i=0;i<cus.size();i++){//每個人每個人地找位置
int tempindex=0,minfinish=window[0];
for(int j=1;j<k;j++){//每個人每次在三個位置中找到最早結束的位置
if(minfinish>window[j]){
minfinish=window[j];
tempindex=j;
}
}
//找到位置後再操作,將視窗結束時間更新
if(window[tempindex]<=cus[i].come){
window[tempindex]=cus[i].come+cus[i].time;
}else {
res+=window[tempindex]-cus[i].come;
window[tempindex]+=cus[i].time;
}
}
//人數不能為0
if(cus.size()==0){
printf("0.0");
}else
printf("%.1f",res/60.0/cus.size());
return 0;
}