个人学习记录,代码难免不尽人意。
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 (≤10 4) - 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 <cstdio>
#include<set>
#include<string>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
struct node{
int second;//题目输入的到达银行时间,到后面我们需要改成入队时间
int time;
}Node[10010];
bool cmp(node a,node b){
return a.second<b.second;
}
bool cmp1(node a,node b){
return a.time+a.second>b.time+b.second;
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++){
int hour,min,second,time;
scanf("%d:%d:%d %d",&hour,&min,&second,&time);
node a;
a.second=hour*3600+min*60+second;
a.time=time*60;
Node[i]=a;
}
sort(Node,Node+n,cmp);
int tempn=n;
for(int i=0;i<tempn;i++){
if(Node[i].second>17*60*60) n--;
}
int sum=0;
for(int i=0;i<n;i++){
if(Node[i].second<8*60*60) {
sum+=8*60*60-Node[i].second;
Node[i].second=8*60*60;
}
}
if(n<k){//提前处理(这部分代码也可以删去)
double res=(1.0*sum)/60/n;
printf("%.1lf\n",res);
return 0;
}
vector<node> v;//用vector模拟队列。
int num=0;//记录处理人数
while(num!=n){
if(v.size()!=k){
v.push_back(Node[num++]);
sort(v.begin(),v.end(),cmp1);
}
else{
node now=v[v.size()-1];
v.pop_back();
if(Node[num].second<now.second+now.time){//下一个人到达时窗口还满着
sum+=now.second+now.time-Node[num].second;
Node[num].second=now.second+now.time;//让second存储入列时间
}
v.push_back(Node[num]);
//进行排序,让目前最快结束的人在队尾。
sort(v.begin(),v.end(),cmp1);
num++;
}
}
double res=(1.0*sum)/60/n;
printf("%.1lf\n",res);
}
这道题做的好顺利!感动!思路可以看我代码中的注释。