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;
}

猜你喜欢

转载自blog.csdn.net/qq_24572475/article/details/82974563