1017 Queueing at Bank
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.
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤104) - 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.
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.
- 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
8.2
自己写的代码,基本思路都是没错的,但是有个问题是在求三个窗口最快的结束时间我用的是循环找到最快结束的窗口,但是不知道为什么就出现错误了,还是得多写写题目,提高自己得代码水平!
- #include
- #include
- #include
- using namespace std;
-
- typedef pair<int,int> PII;
- vector
v;//p表示当前机器使用者的开始时间和处理时间,s表示客户的信息 -
- int main(){
- int n,k,num=0,sum=0;
- cin >> n >> k;
- int q[k]={0},w[k]={0};//分别表示当前用户的开始时间和处理业务所需要的时间
-
- int t,h,m,s,r,T;
- for(int i=0;i
- scanf("%d:%d:%d %d",&h,&m,&s,&r);
- t=h*3600+m*60+s;//到达银行时间全部用秒来表示
- T=r*60;
- if(t>=17*60*60) continue;
- v.push_back({t,T});
- num++;
- }
-
- sort(v.begin(),v.end());
-
- int j;
- for(int i=0;i
- auto a=v[i].first,b=v[i].second;
- if(a<=8*3600){
- sum+=8*3600-a;
- a=8*3600;
- }
- int minn=1e9;
- for(j=0;j
- if(q[j]==0 || minn>q[j]+w[j]){
- minn=q[j]+w[j];//找到最快结束的机子
- }
- }
- q[j]=a,w[j]=b;
- cout << minn << endl;
- if(minn<=a) continue;//结束时间<=到达时间,无需等待,直接用
- sum+=minn-a;//前面有人再用着,等待时间
- }
- cout << sum << endl;
- //printf("%.1lf\n",((double)sum)/60/num);
-
- return 0;
- }
看看大佬的代码:
- #include
- #include
- #include
- using namespace std;
-
- const int N=10010;
- struct person{
- int come,time;
- }p[N];
- bool cmp(person a,person b){
- return a.come
- }
- int total=0;
- int main(){
- int n,k,cnt;
- scanf("%d%d",&n,&k);
- for(int i=1;i<=n;i++){
- int hh,ss,mm,tt;
- scanf("%d:%d:%d %d",&hh,&mm,&ss,&tt);
- int sum=hh*3600+mm*60+ss;
- if(sum>61200) continue;
- p[++cnt].time=tt*60;
- p[cnt].come=sum;
- }
- sort(p+1,p+1+cnt,cmp);
- priority_queue<int,vector<int>,greater<int>> q;
- for(int i=1;i<=k;i++) q.push(28800);
- for(int i=1;i<=cnt;i++){
- if(q.top()<=p[i].come){
- q.push(p[i].come+p[i].time);
- q.pop();
- }
- else{
- total+=q.top()-p[i].come;
- q.push(q.top()+p[i].time);
- q.pop();
- }
- }
- (!cnt)?printf("0.0\n"):printf("%.1lf",((double)total/60.0)/(double)cnt);
-
- return 0;
- }
好好学习,天天向上!
我要考研!
-
相关阅读:
SpringBoot MongoDB操作封装
React Native调用摄像头画面及拍照和保存图片到相册全流程
vue(11)
防反接方案,NMOS & PMOS
处理耗时任务
C++之log4cpp库
laravel 异步队列的使用
请求数据,后台返回上千条上万条数据,怎么办
Alibaba(获得店铺的所有商品) API 接口
cad怎么转换成pdf格式?cad转pdf的方法有哪些?
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126778313