• PTA--1014 Waiting in Line(模拟队列)


    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.
    • Customeri​ will take Ti​ 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 customers 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 Specification:

    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 Specification:

    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:

    1. 2 2 7 5
    2. 1 2 6 4 3 534 2
    3. 3 4 5 6 7

    Sample Output:

    1. 08:07
    2. 08:06
    3. 08:10
    4. 17:00
    5. Sorry

    题意:有N个窗口,每个窗口一次最多排M个人,超出NM的人需要某个窗口少于M个人时候可以进入,且进入时会选择当前人数最少的窗口,如果相同,那么选编号小的窗口,总共有K个人,每个人有对应所需时间,最后有Q次询问,每次询问输出当前ID的人的完成时间。

    每个窗口早上8:00开始,17:00结束,如果某个人在17:00被受理,那么输出Sorry

    解析:模拟队列,可以用双端队列,十分方便,我们可以把第一批的人先处理,后面暴力每个时间点,对应更新即可。

    1. #include
    2. using namespace std;
    3. const int N=1005;
    4. int w[N],ans[N],T[N];
    5. void solve()
    6. {
    7. int n,m,k,Q;
    8. deque<int> q[30],qq[30];//qq为实时队列
    9. scanf("%d%d%d%d",&n,&m,&k,&Q);
    10. for(int i=1;i<=k;i++) scanf("%d",&w[i]);//每个人所需时间
    11. int num=1,ren=0;//分别记录窗口编号和当前放了几个人
    12. for(int i=1;i<=k;i++)
    13. {
    14. if(q[num].size()
    15. {
    16. q[num].push_back(i);
    17. ren++;//第一波安放了几个人
    18. if(++num==n+1) num=1;//下一轮
    19. }
    20. }
    21. for(int i=1;i<=n;i++) qq[i]=q[i];//拷贝到实时队列
    22. for(int i=1;i<=n;i++)
    23. {
    24. if(qq[i].size()==0) break;
    25. T[i]=w[qq[i].front()];//当前窗口倒计时
    26. }
    27. while(ren//还没有放置好所有人
    28. {
    29. for(int j=1;j<=n;j++)
    30. {
    31. if(--T[j]==0)//空窗,更新人员
    32. {
    33. qq[j].pop_front();
    34. ren++;
    35. qq[j].push_back(ren);
    36. q[j].push_back(ren);
    37. if(ren==k) break;//全部安置好
    38. T[j]=w[qq[j].front()];//更新窗口时间
    39. }
    40. }
    41. }
    42. for(int i=1;i<=n;i++)
    43. {
    44. int sum=8*60;
    45. while(q[i].size())
    46. {
    47. int id=q[i].front();
    48. q[i].pop_front();
    49. sum+=w[id];//完成时间
    50. ans[id]=sum;
    51. }
    52. }
    53. while(Q--)
    54. {
    55. int id;
    56. scanf("%d",&id);
    57. if(ans[id]-w[id]>=17*60) printf("Sorry\n");
    58. else printf("%02d:%02d\n",ans[id]/60,ans[id]%60);
    59. }
    60. }
    61. int main()
    62. {
    63. int t=1;
    64. while(t--) solve();
    65. return 0;
    66. }
  • 相关阅读:
    Java版本+企业电子招投标系统源代码+支持二开+Spring cloud
    如何查看Android 包依赖关系
    【QT】不使用QT6PDF模块,200行代码编写一个可编辑PDF的阅读器
    对话式人工智能的数据采集方案
    Apache配置ssl证书-实现https访问
    1026 程序运行时间
    华为HCIE云计算之FA桌面云业务发放
    彻底理解适配器模式
    HTML常用标签的使用
    【用unity实现100个游戏之15】开发一个类保卫萝卜的Unity2D塔防游戏4(附项目源码)
  • 原文地址:https://blog.csdn.net/qq_63739337/article/details/134077033