1014. Waiting in Line (30)

来源:互联网 发布:矢量数据的拓扑结构 编辑:程序博客网 时间:2024/06/09 13:47

1014. Waiting in Line (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customer[i] will take T[i] minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

Output

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.

Sample Input
2 2 7 51 2 6 4 3 534 23 4 5 6 7
Sample Output
08:0708:0608:1017:00Sorry
类型:模拟

题目大意:n个窗口,每个窗口可以排队m人。有k位用户需要服务,给出了每位用户需要的minute数,所有客户在8点开始服务,如果有窗口还没排满就入队,否则就在黄线外等候。如果有某一列有一个用户走了服务完毕了,黄线外的人就进来一个。如果同时就选窗口数小的。求q个人的服务结束时间。
如果一个客户在17:00以及以后还没有开始服务(此处不是结束服务是开始17:00)就不再服务输出sorry;如果这个服务已经开始了,无论时间多长都要等他服务完毕。


分析:设立结构体,里面包含poptime为队首的人出队(结束)的时间,和endtime为队尾的人结束的时间。poptime是为了让黄线外的人可以计算出哪一个队列先空出人来(poptime最小的那个先有人服务完毕),endtime是为了入队后加上自己本身的服务所需时间可以计算出自己多久才能被服务完毕~且前一个人的endtime可以得知自己是不是需要被Sorry(如果前一个人服务结束时间超过17:00,自己当前入队的人就是sorry),还有一个queue表示所有当前该窗口的排队队列。
对于前m*n个人,也就是排的下的情况下,所有人依次到窗口前面排队。对于m*n之后的人,当前人选择poptime最短的入队,让队伍的第一个人出列),如果前面一个人导致的endtime超过17点就标记自己的sorry为true。
计算时间的时候按照分钟计算,最后再考虑08点开始和转换为小时分钟的形式会比较简便

#include<iostream>#include<queue>#include<vector>using namespace std;struct node{int poptime;int endtime;queue <int> q;};//表示每个窗口的信息 int main(){//freopen("in.txt","r",stdin);int n,m,k,q;int index=1;cin>>n>>m>>k>>q;vector<int> time(k+1);//每个人所需的时间 vector<int> result(k+1);//记录第几个人出队的时间 for(int i=1;i<=k;++i)cin>>time[i];vector<node> window(n+1);//表示窗口的集合 vector<bool> sorry(k+1,false);//true标志无法完成服务 for(int i=1;i<=m;++i)//记录黄线前的站队情况 {for(int j=1;j<=n;++j){if(index<=k){window[j].q.push(time[index]);if(window[j].endtime>=540)sorry[index]=true;window[j].endtime+=time[index];if(i==1)window[j].poptime=window[j].endtime;//poptime记录第一个人出队时间 result[index]=window[j].endtime;index++;}}}while(index<=k){int mintemp=window[1].poptime;int tempwindow=1;for(int i=2;i<=n;++i)//找出最先从队首出来的队首编号 {if(window[i].poptime<mintemp){tempwindow=i;mintemp=window[i].poptime;}}window[tempwindow].q.pop();window[tempwindow].q.push(time[index]);window[tempwindow].poptime+=window[tempwindow].q.front();if(window[tempwindow].endtime>=540)sorry[index]=true;window[tempwindow].endtime+=time[index];result[index]=window[tempwindow].endtime;index++;} for(int i=1;i<=q;++i){int query,minute;cin>>query;minute=result[query];if(sorry[query]==true)cout<<"Sorry"<<endl;else{printf("%02d:%02d\n",(minute + 480) / 60, (minute + 480) % 60);//这里用到c的输出格式//if(minute<120)//{//cout<<"0"<<8+(minute)/60<<":";//if(minute%60<10)//cout<<"0"<<minute%60<<endl;//else//cout<<minute%60<<endl;//}//else//{//cout<<8+minute/60<<":";//if(minute%60<10)//cout<<"0"<<minute%60<<endl;//else//cout<<minute%60<<endl;//}}}return 0;} 

(2)

分析:

这是一道模拟题,所谓模拟题,就是再现生活中的一些场景,比如排队、汽车计费或者其他之类的过程,是相对于其他一些用一些数学能解出最佳答案的题目而言的。

这是一道模拟银行排队的题目,每次顾客都会选择窗口中人数最小的排队(如果人数相同则选择窗口标号最小的)。

这题比较关键和有技巧的一点就是选择时间作为我们的循环变量(当然这只是其中一种方法),也更好理解。每次时间增加后,先查看队头的人是否有服务完成的,如果是则出队,并计算其后顾客(如果有)的结束时间:当前时间+顾客服务时间;再查看是否有人可以排进窗口队伍中。

这里假设顾客都是同时到达银行的,所以相对来说更简单了一些。


注意点:

(1)从5:00前就已经开始服务的,虽然最终的服务时间超过五点,也不是Sorry,而只有从五点之后才会被开始服务的,会是Sorry。这个很关键,不然会有答案错误的案例。

(2)对于时间的选择,最好是用都化成分钟的形式,如果用时间类来做,或者重载了其加号运算符和赋值运算符,会出现超时的情况。

(3)每个顾客进入到一个队伍之后,就不能再离开这个队伍,即使其他队伍中已经没有人了,也只能等当前队伍前面的人结束之后在本队伍接受服务。


案例分析:

2个窗口,每个窗口最多2个人,7个顾客,5次查询。

顾客的时间为1  2  6  4  3  534  2分钟。

查询的顾客为3   4   5   6   7号

流程:

时间事件8:001号顾客进入队伍18:002号顾客进入队伍28:003号顾客进入队伍18:004号顾客进入队伍28:011号顾客离开队伍1
5号顾客进入队伍18:022号顾客离开队伍2
6号顾客进入队伍28:064号顾客离开队伍2
7号顾客进入队伍28:073号顾客离开队伍18:105号顾客离开队伍117:006号顾客离开队伍2超时7号顾客还没开始,Sorry

看到注意点(3)中的,你就知道了,在8:10到17:00的时间里,虽然队伍1中没有一个人,但是因为8:06的时候7号顾客已经选择了队伍2,所以他也只能干瞪眼等到队伍2中6号顾客花了534分钟时间后,而不能换队伍。

所以,查询的3  4   5   6   7号结束服务的时间分别是:

08:07

08:06

08:10

17:00

Sorry

#include<stdio.h>  #include<queue>  using namespace std;  int serve_time[1001];  int ans[1001];  queue<int> Q[21];  int main(void){   int w,cap,cus,k;//分别记录窗口数量、窗口最大人数、顾客数量和查询数量   int i,j;   while(scanf("%d%d%d%d",&w,&cap,&cus,&k) != EOF){    for(i = 1; i <= cus; i ++){     scanf("%d",&serve_time[i]);    }    for(i = 0; i < w; i ++){     if(Q[i].empty() == false)Q[i].pop();    }       int sum = 0;    int count = 1;    for(int ti = 0; ti < 540; ti = ti + 1){//以时间来作为循环,这个很关键     for(i = 0; i < w; i ++){      for(j = 0; j < Q[i].size(); j ++){       if(ti == ans[Q[i].front()]){//如果当前队伍的人服务结束了        Q[i].pop();         sum --;                      if(!Q[i].empty()){//并且计算当前队伍下一个人的结束时间         int tmp = Q[i].front();         ans[tmp] = ti + serve_time[tmp];//结束时间为当前时间+顾客的服务时间        }       }      }     }          while(sum < w * cap && count <= cus){      int min = 0;      for(i = 0; i < w; i++){       if(Q[min].size() > Q[i].size()){        min = i;       }//找到人最少的那个队伍       if(Q[min].size() == 0)  ans[count] = ti + serve_time[count];//如果队伍没人则直接开始服务,并且计算结束时间       if(Q[min].size() < cap && count <= cus){        Q[min].push(count);//否则把让下一个顾客进队        count ++;        sum ++;       }      }     }    }       for(i = 0; i < k; i ++){     int query;     scanf("%d",&query);     if(ans[query] == 0)puts("Sorry");//值为0说明没有被开始服务,只能Sorry     else{      int hour,min;      hour = 8 + ans[query] / 60;      min = ans[query] % 60;      printf("%02d:%02d\n",hour,min);//把时间转换为标准格式并输出     }    }   }   return 0;  }  

#include<iostream>  #include<cstdio>  #include<vector>  #include<queue>  #include<fstream>  using namespace std;  #define INF 0x6FFFFFFF  struct Customer{      int process;      int leave;  };  int main(){  //  freopen("input.txt","r",stdin);      int N,M,K,Q;      cin>>N>>M>>K>>Q;      vector<Customer> cus(K);      for(int i=0;i<K;i++){          cin>>cus[i].process;          cus[i].leave=INF;      }       vector< queue<int> > winQueue(N);//每个窗口的排队队列       vector<int> timeBase(N,0);//每个窗口基准时间      int p;       //首次可进入排队状态的       for(p=0;p<N*M&&p<K;p++){          cus[p].leave=timeBase[p%N]+cus[p].process;          timeBase[p%N]=cus[p].leave;          winQueue[p%N].push(p);      }      //队列之外的,进队状态      for(p;p<K;p++){          int tmp=INF;          int win=-1;          for(int i=0;i<N;i++){              int top=winQueue[i].front();              if(tmp>cus[top].leave){//选出窗口中最早的空闲的时间,进而让对外的人进来排队                   win=i;                  tmp=cus[top].leave;              }          }          cus[p].leave=timeBase[win]+cus[p].process;          timeBase[win]=cus[p].leave;          winQueue[win].pop();          winQueue[win].push(p);      }       for(int i=0;i<Q;i++){          int q;          cin>>q;          q--;          if(cus[q].leave-cus[q].process>=540){              cout<<"Sorry"<<endl;          }else{              printf("%02d:%02d\n",8+cus[q].leave/60,cus[q].leave%60);          }      }   //  fclose(stdin);      return 0;  }  


原创粉丝点击