首页 > 试题广场 >

Waiting in Line (30)

[编程题]Waiting in Line (30)
  • 热度指数:2868 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.

  • 输入描述:
    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.


    输出描述:
    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.
    示例1

    输入

    2 2 7 5<br/>1 2 6 4 3 534 2<br/>3 4 5 6 7

    输出

    08:07<br/>08:06<br/>08:10<br/>17:00<br/>Sorry
    题目很简单,30分水题
    大意是K个人早上8:00来排队,8点时前N个开始办业务,前N*M个顺次划进黄线内,黄线外的等着
    每一个进入黄线的客户总是选择人数最少的窗口排队,如果出现人数一样的那么选窗口ID小的
    后面跟Q个查询,每个查询输出其办完业务的时间=办业务所用时长+队列前面的人办完业务的时间
    如果前面的人超过17:00还没办完业务那么后面的人输出sorry(前面的人只要在17:00前开始办业务,即使办完的时间在17:00以后,也应输出相应的时间)
    思路
    定义结构体类型存储客户的服务时长stime,完成时间ftime,窗口ID wid(1~N)
    定义数组wftime表示每个窗口最后一个人的完成时间
    注意到客户进入黄线时,其办完业务的时间也就确定了,因此使用priority_queue作为黄线区,每个客户进入黄线时计算出完成时间ftime,重载运算符"<"首先选择ftime最小的如果ftime相同选择wid最小的(注:优先队列默认是大顶堆,返回a>b表示a的值比b小,优先弹出a) 当客户数小于等于N*M时,依次进入每个队列并计算ftime,更新wftime;当客户数大于N*M时,从优先队列中弹出一个客户,新来的客户继承其窗口ID,计算ftime更新wftime即可
    最后判断每个查询的开始时间 ftime - stime + 8:00是否早于17:00,输出相应时间或sorry
    #include <cstdio>
    #include <queue>
    using namespace std;
    const int MAXK=1010;
    const int ONTIME=8*60;
    const int OFFTIME=17*60;
    
    int N,M,K,Q,C;
    int wftime[21]={0};
    struct node{
        int stime,ftime,wid;
        friend bool operator <(const node&a,const node&b){
            if(a.ftime!=b.ftime) return a.ftime>b.ftime;
            return a.wid>b.wid;
        }
    }customer[MAXK],cid;
    priority_queue<node> q;
    void printtime(int ftime,int stime){
        if(ftime-stime+ONTIME>=OFFTIME) printf("Sorry\n");
        else printf("%02d:%02d\n",(ftime+ONTIME)/60,(ftime+ONTIME)%60);
    }
    int main()
    {
        int query,t,winid;
        scanf("%d%d%d%d",&N,&M,&K,&Q);
        C=N*M;
        for(int i=1;i<=K;++i){
            scanf("%d",&t);
            customer[i].stime=t;
            if(q.size()<C){
                winid=customer[i].wid=(i-1)%N+1;
            }else{
                cid=q.top();
                q.pop();
                winid=customer[i].wid=cid.wid;
            }
            customer[i].ftime=t+wftime[winid];
            wftime[winid]=customer[i].ftime;
            q.push(customer[i]);
        }
        for(int i=0;i<Q;++i){
            scanf("%d",&query);
            printtime(customer[query].ftime,customer[query].stime);
        }
        return 0;
    }

    发表于 2018-08-23 18:28:42 回复(1)
    是Sorry不是sorry
    发表于 2019-02-09 21:34:52 回复(0)
    牛客通过,PAT有几个点过不了,求解
    import java.text.DecimalFormat;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    /**
     * 1014. Waiting in Line (30)
     * 
     * @author Jacob
     *
     */
    public class Main{
    	public final static int MAX_TIME = 540;
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int winNum = sc.nextInt(), maxNum = sc.nextInt();
    		int cusNum = sc.nextInt(), checkNum = sc.nextInt();
    		// 初始化顾客类和窗口类
    		Customer[] customers = new Customer[cusNum];
    		Window[] windows = new Window[winNum];
    		int[] checked = new int[checkNum];
    		for (int i = 0; i < cusNum; i++) {
    			customers[i] = new Customer(sc.nextInt());
    		}
    		for (int i = 0; i < winNum; i++) {
    			windows[i] = new Window();
    		}
    		for (int i = 0; i < checkNum; i++) {
    			checked[i] = sc.nextInt();
    		}
    		int cusIndex = 0;// 顾客序号
    		// 窗口初始化:把窗口队列填满
    		while (cusIndex < winNum * maxNum) {
    			customers[cusIndex].begin = windows[cusIndex % winNum].freeTime;
    			windows[cusIndex % winNum].freeTime += customers[cusIndex].serveTime;
    			windows[cusIndex % winNum].queue.offer(windows[cusIndex % winNum].freeTime);
    			customers[cusIndex].leave = windows[cusIndex % winNum].freeTime;
    			cusIndex++;
    		}
    		int index = 0;
    		while (index < MAX_TIME) {
    			for (int j = 0; j < winNum && cusIndex < cusNum; j++) {
    				if (windows[j].queue.peek() == index) {
    					windows[j].queue.poll();
    					customers[cusIndex].begin = windows[j].freeTime;
    					windows[j].freeTime += customers[cusIndex].serveTime;
    					windows[j].queue.offer(windows[j].freeTime);
    					customers[cusIndex].leave = windows[j].freeTime;
    					cusIndex++;
    				}
    			}
    //			System.out.println(i);
    			index++;
    			
    		}
    		 for (int i = 0; i < checkNum; i++) {
    		 if (customers[checked[i] - 1].begin < 540&&customers[checked[i] - 1].leave>0)
    		 System.out.println(customers[checked[i] - 1].getLeaveTime());
    		 else
    		 System.out.println("Sorry");
    		 }
    
    //		for (i = 0; i < cusNum; i++) {
    //			if (customers[i].begin < 540)
    //				System.out.println(customers[i].begin);
    //			else
    //				System.out.println("Sorry");
    //		}
    	}
    
    	/*
    	 * 顾客类
    	 */
    	static class Customer {
    		int begin;// 开始时间
    		int serveTime;// 服务时间
    		int leave;// 离开时间
    		String leaveStandard;
    		
    		public Customer(int serveTime) {
    			this.serveTime = serveTime;
    		}
    		public String getLeaveTime(){
    			int hour=leave/60+8;
    			int minute=leave%60;
    			DecimalFormat df=new DecimalFormat("00");
    			return df.format(hour)+":"+df.format(minute);
    		}
    		
    	}
    
    	static class Window {
    		int count;// 窗口人数
    		int freeTime;// 空闲时间
    		Queue<Integer> queue = new LinkedList<Integer>();// 记录当前窗口中每个顾客的离开时间
    	}
    } 


    发表于 2017-09-01 12:30:47 回复(1)
    组建20个队列对场景进行模拟,每秒处理一次事件 
    注意点: 
    1. 题中要求是新进入队伍的人找最短的队伍,不是耗时最少的队伍,如果是耗时最少的队伍,可以再建一个数组来存储当前队伍的剩余时间 
    2. 题目要求是17点之前没有开始服务的人将不会被服务,不是17点之前开始排队的人,也不是17点立马就关门,因此在17点这个时间点单独处理剩下的人 
    3. 对于17点这个时间点,第一排的人直接把剩余时间加上539放入结果,然后退出循环,其余人都会被Sorry
    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <climits>
    using namespace std;
    struct people{
        int num;
        int timeLeft;
    };
    people t;
    queue<people> mainQ;
    int ans[1001];
    int n,m,k,q;
    queue<people> qu[20];
    void printTime(int t){
        if(t==INT_MAX){
            printf("Sorry\n");
            return;
        }
        int h=t/60;
        int m=t%60;
        printf("%02d:%02d\n",h+8,m);
    }
    int minTimeQ(){
        int mint=INT_MAX;
        int min=-1;
        for(int i=0;i<n;i++){
            if(qu[i].size()<m&&qu[i].size()<mint){
                mint=(int)qu[i].size();
                min=i;
            }
        }
        return min;
    }
    int main(){
        cin>>n>>m>>k>>q;
        for(int i=1;i<=k;i++){
            t.num=i;
            scanf("%d",&t.timeLeft);
            ans[i]=INT_MAX;
            mainQ.push(t);
        }
        for(int minutes=0;minutes<=540;minutes++){
            if(minutes==540){
                for(int i=0;i<n;i++){
                    if(!qu[i].empty()){
                        ans[qu[i].front().num]=539+qu[i].front().timeLeft;
                    }
                }
                break;
            }
            for(int i=0;i<n;i++){
                if(!qu[i].empty()){
                    qu[i].front().timeLeft--;
                    if(qu[i].front().timeLeft==0){
                        ans[qu[i].front().num]=minutes;
                        qu[i].pop();
                    }
                }
            }
            int QueTo=minTimeQ();
            while(QueTo!=-1&&!mainQ.empty()){
                qu[QueTo].push(mainQ.front());
                mainQ.pop();
                QueTo=minTimeQ();
            }
        }
        for(int i=0;i<q;i++){
            int query;
            scanf("%d",&query);
            printTime(ans[query]);
        }
        return 0;
    }

    发表于 2016-06-22 18:10:45 回复(0)
    #include <stdio.h>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <set>
    #include <iomanip>
    using namespace std;
    int N,M,K,Q,query[1005],done[1005]={0};
    typedef struct node{
    	int cost;
    	int window;
    	int done=0;
    	bool sorry=false;
    	bool operator <(const struct node & B)const{
    		if(done!=B.done)
    			return done<B.done;
    		else
    			return window<B.window;
    	}
    }Node;
    
    Node c[1005];
    queue<Node> window[25];
    multiset<Node> line;
    
    int main(){
    	cin>>N>>M>>K>>Q;
    	for(int i=0;i<K;i++){
    		cin>>c[i].cost;
    	}
    	for(int i=0;i<Q ;i++)
    		cin>>query[i];
    	//以上读取输入 
    	for(int i=0;i<N*M && i<K;i++){//计算在黄线内的顾客的done time 
    		int j=i-N;
    		j=j<0?i:j;
    		c[i].done=c[j].done+c[i].cost;
    		if(c[j].done>=540)	c[i].sorry=true;
    	}
    	for(int i=0;i<N*M && i<K;i++){//初始化队列 
    		c[i].window=i%N;//注意顺序,先改变再插入 
    		window[i%N].push(c[i]);
    	}
    	for(int i=0;i<N & i<K;i++){//初始化窗口服务 
    		line.insert(window[i].front());
    	}
    	auto line_begin=line.begin();
    	int winnum=line_begin->window;
    	for(int i=N*M;i<K;i++){
    		Node back=window[winnum].back();
    		window[winnum].pop();
    		c[i].done=back.done+c[i].cost;
    		if(back.done>=540)	c[i].sorry=true;
    		c[i].window=winnum;
    		window[winnum].push(c[i]);
    		line.erase(line_begin);
    		line.insert(window[winnum].front());
    		line_begin=line.begin();
    		winnum=line_begin->window;
    	}
    	for(int i=0;i<Q;i++){
    		int num=query[i]-1;
    		int s=c[num].done;
    		int h=s/60+8;
    		int m=s%60;
    		if(c[num].sorry==true)	cout<<"Sorry"; 
    		else			cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m;
    		if(i<Q-1)	cout<<endl;
    	}
    } 
    
    牛客网这边测试点都通过了,PAT那边有两个过不了(2和4),求解答
    发表于 2020-01-19 17:24:00 回复(0)
    //有个坑:就是只要顾客开始服务的时间在17:00之前就可以,哪怕他的结束时间超过了5点
    //所以说,英文题目一定要仔细读题
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <string>
    #include <math.h>
    #include <queue>
    
    using namespace std;
    const int INF = 0x3fffffff;
    const int maxn = 1005;
    const int maxc = 21;
    int service_time[maxn];   //每个顾客需要的服务时间
    int query[maxn];    //记录当前需要输出的顾客编号
    int finish_time[maxn];   //记录每一个记录完成的分钟数
    int start_time[maxn];    //记录每个顾客开始服务的时间
    int window_service_time[maxc];    //每个窗口的当前服务时长
    queue<int> inLine[maxc];    //每个窗口黄线内的顾客
    queue<int> outLine;   //黄线后面的顾客
    int main() {
        int n,m,k,q;
        scanf("%d %d %d %d", &n, &m, &k, &q);
    
        fill(window_service_time, window_service_time+n, 0);
        for(int i=1; i<=k; i++) {
            scanf("%d", service_time+i);
        }
        for(int i=0; i<q; i++) {
            scanf("%d", query+i);
        }
    
        for(int i=1; i<=k; i++) {
            if(i > n*m){
                outLine.push(i);
                continue;
            }
            inLine[(i-1)%n].push(i);
        }
    
        for(int i=1; i<=k; i++) {
            int window_num = -1,min_time = INF;
            for(int j=0; j<n; j++) {    //遍历当前窗口,找到最早结束的那个窗口的顾客
                if(!inLine[j].empty()) {
                    int index = inLine[j].front();
                    if(service_time[index]+window_service_time[j]<min_time) {
                        min_time = service_time[index]+window_service_time[j];
                        window_num = j;
                    }
                }
            }
            if(window_num != -1) {
                int customer = inLine[window_num].front();
                inLine[window_num].pop();
                finish_time[customer] = min_time;
                start_time[customer] = window_service_time[window_num];
                window_service_time[window_num] += service_time[customer];
    
                if(!outLine.empty()) {
                    int next_customer = outLine.front();
                    outLine.pop();
                    inLine[window_num].push(next_customer);
                }
            }
        }
    
        for(int i=0;i<q;i++){
            if(start_time[query[i]] < 540){
                int minutes = finish_time[query[i]];
                int hours = minutes/60+8;
                printf("%02d:%02d\n",hours, minutes%60);
            }else{
                printf("Sorry\n");
            }
        }
    }
    



    编辑于 2019-08-24 17:20:40 回复(0)
    //模拟题其实很简单,就是这个阅读理解和英语总坑人,,看清楚题就好了
    //只要在17.00之前的人,只要开始服务了,管他到几点都行;17.00及以后没服务的都sorry
    #include <stdio.h>

    #include <queue>

    using namespace std ;

    queue < int > Q[20];

    int main()

    {

        int n,m,k,q;

        scanf ( "%d%d%d%d" ,&n,&m,&k,&q);

        int cus[1001]={0};

        int done[1001]={0};

        int T[1001]={0};

        int i;

        for (i=1;i<=k;i++)

        {

            scanf ( "%d" ,&cus[i]);

            done[i]=cus[i];

        }

        

        int peo=1;

        i=0;

        while (peo<=n*m&&peo<=k)

        {

            Q [i]. push (peo++);

            i++;

            if (i>=n) i=0;

        }

        int time=0;

        while (peo<=k)

        {

            int pos=0,min=100000;

            for (i=0;i<n;i++)

            {

                if (cus[ Q [i]. front ()]<min)

                {

                    min=cus[ Q [i]. front ()];

                    pos=i;

                }

            }

            time=time+min;

            T[ Q [pos]. front ()]=time;

            for (i=0;i<n;i++)

                cus[ Q [i]. front ()] -= min;

            Q [pos]. pop ();

            Q [pos]. push (peo++);

        }

        for (i=0;i<n;i++)

        {   int t=0;

            while (! Q [i]. empty ())

            {

                t=cus[ Q [i]. front ()];

                T[ Q [i]. front ()]=t+time;

                Q [i]. pop ();

                if (! Q [i]. empty ())

                    cus[ Q [i]. front ()]+=t;

            }

        }

        for (i=0;i<q;i++)

        {

            int check;

            scanf ( "%d" ,&check);

            if (T[check]-done[check]<540)

                printf ( "%02d:%02d\n" ,T[check]/60+8,T[check]%60);

            else

                printf ( "Sorry\n" );

        }

        

    }

    发表于 2017-01-19 14:54:53 回复(0)
    #include<iostream>
    #include<fstream>
    #include<queue>
    #include<vector>
    #include<unordered_map>
    #include<limits.h>
    #include<iomanip>
    using namespace std;
    int chooseShortQueue(vector<deque<int>> &windows)
    {
    	int N = windows.size();
    	int cho = 0;
    	for (int w = 1; w < N; w++)
    	{
    		if (windows[w].size() < windows[cho].size())
    			cho = w;
    	}
    	return cho;
    }
    void printTime(int n)
    {
    	int hh = n / 60;
    	int mm = n % 60;
    	cout << fixed << setw(2) << setfill('0') << hh << ":" << fixed << setw(2) << setfill('0') << mm<<endl;
    }
    int main()
    {
    	int N, M, Q, K;
    	cin >> N >> M >> K >> Q;
    	vector<deque<int>> windows(N, deque<int>());
    	vector<int> leave(K, 0);
    	vector<int> start(K, 0);
    	int cur;
    	int t8 = 8 * 60, t17 = 17 * 60;
    	int now = t8;
    	int curtime;
    	int cho = 0,mcho;
    	for (int i = 0; i < K; i++)
    	{
    		cin >> cur;
    		cho = chooseShortQueue(windows);
    		if (windows[cho].size() == M)//队满	
    		{
    			mcho = 0;
    			for (int w = 1; w < N; w++)
    			{
    				if (windows[w].front() < windows[mcho].front())
    					mcho = w;
    			}
    			curtime = windows[mcho].front();
    			now = curtime;
    			for (int w = 0; w < N; w++)
    			{
    				while (windows[w].front() == curtime)
    					windows[w].pop_front();
    			}
    			cho = chooseShortQueue(windows);
    		}
    		if (windows[cho].size() == 0)
    		{
    			start[i] = now;
    			leave[i] = now + cur;
    		}
    		else
    		{
    			leave[i] = windows[cho].back() + cur;
    			start[i] = windows[cho].back();
    		}
    		windows[cho].push_back(leave[i]);
    	}
    	for (int i = 0; i < Q; i++)
    	{
    		cin >> cur;
    		cur--;
    		if (start[cur]>=t17)
    			cout << "Sorry" << endl;
    		else
    			printTime(leave[cur]);
    	}
    	return 0;
    }
    心好累,题太烦了……注意事项:1、是选择最短的对列入队而不是选择第一个有人离开的队列2、对到来(不是离开)大于等于5点的人,不处理……
    发表于 2016-05-06 21:37:13 回复(1)
    我也来分享一波
    #include<cstdio>
    #include<vector>
    #include<queue>
    using namespace std;
    int n,m,k,q,Min,t=0,opentime=8*60,closetime=17*60,flag;//flag  优先窗口号标记
    queue<int> window[20];
    vector<int> custor,endtime,open;
    int main(){//解题关键  最早离开人的时间 决定排序顺序 不是窗尾决定
    	scanf("%d%d%d%d", &n, &m, &k, &q);
    	endtime.resize(n,8*60),custor.resize(k);
    	vector< priority_queue<int,vector<int>,greater<int> > >Endtime(n);
    	for(int i=0;i<k;i++) scanf("%d", &custor[i]);
    	open=custor;
    	for(int i=0;i<k;i++)//第一次用户加满 及横跨线也满
    		if(Endtime[i%n].size()!=m){
    			open[i]=endtime[i%n];
    			custor[i]+=endtime[i%n];
    			endtime[i%n]=custor[i];
    			Endtime[i%n].push(custor[i]);
    		}
    		else {
    			Min=closetime+1;//更新队首用户的结束时间
    			for(int j=0;j<n;j++)//更新办理队首业务完成的窗口
    				if(Endtime[j].top()<Min) {
    					flag=j;//这里注意  划分组数
    					Min=Endtime[j].top();
    				}
    			open[i]=endtime[flag];
    			custor[i]+=endtime[flag];
    			endtime[flag]=custor[i];
    			Endtime[flag].push(custor[i]);
    			Endtime[flag].pop();
    		}
    	for(int i=0;i<q;i++){
    	 scanf("%d", &flag);
    	open[flag-1]>=closetime ? printf("Sorry\n"):printf("%02d:%02d\n",custor[flag-1]/60%24,custor[flag-1]%60);
    	}
    	return 0; 
    }

    发表于 2021-01-26 13:39:22 回复(0)
    //start记录开始服务时间,8~17为540分钟;
    #include<cstdio>
    (802)#include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn=1010;
    int T[maxn],t[maxn],start[maxn];//T记录服务时间,t为T副本;
    bool vis[maxn]={false};
    vector<int> ve[21];
    int number[21];
    int findmin(int n)
    {
        int min=1;
        for(int i=2;i<=n;i++)
            if(number[min]>number[i])
                min=i;
      //  printf("%d**\n",min);
        return min;
    }
    int main()
    {
        int n=0,m=0,k=0,q=0;
        fill(start,start+maxn,0x3fffffff);
        fill(number,number+21,0);
        scanf("%d%d%d%d",&n,&m,&k,&q);
        for(int i=1;i<=k;i++)   //输入顾客交易时间;
        {
            scanf("%d",T+i);
            t[i]=T[i];
        }
        int id=1;
        for(int i=0;i<=540;i++)
        {
            while(id<=k&&number[findmin(n)]<m)
            {
                int win=findmin(n);
                ve[win].push_back(id);
                number[win]++;
                id++;
            }
            for(int j=1;j<=n;j++)
                if(number[j]!=0)
                {
                    int ID=ve[j][0];
                    if(vis[ID]==false)
                    {
                        start[ID]=i;
                        vis[ID]=true;
                    }
                    T[ID]--;
                    if(T[ID]==0)
                    {
                        ve[j].erase(ve[j].begin());
                        number[j]--;
                    }
                }
        }
        for(int i=0;i<q;i++)
        {
            scanf("%d",&id);
            if(start[id]>=540)
                printf("Sorry\n");
            else
                printf("%02d:%02d\n",(start[id]+t[id])/60+8,(start[id]+t[id])%60);
        }
        return 0;
    }

    发表于 2020-04-01 15:52:15 回复(0)
    注意只有下午五点和五点以后来的人不会被服务,其余的人都会,而且服务到几点就输出几点
    a = list(map(int,input().split()))
    b = list(zip([i for i in range(a[2])],map(int,input().split())))
    c = [[] for i in range(a[0])]       # 模拟窗口队列
    d = {}                              # 记录每人完成的时间
    n = a[0] * a[1]                     # 未进入队列的第一个人索引数
    t = 0                               # 时间戳
    
    for i in range(n):
        if i == a[2]:
            break
        if len(c[i % a[0]]) == min(len(j) for j in c):
            c[i % a[0]].append(list(b[i]))
    c = list(filter(lambda x:x,c))
    
    while c:
        p = min([i[0][1] for i in c])
        if t + p >= 540:
            break
        t += p
        i = 0
        while i < len(c):
            if c[i][0][1] - p:
                c[i][0][1] -= p
            else:
                d[c[i][0][0]] = t
                if n < a[2]:
                    c[i] = c[i][1:] + [list(b[n])]
                    n += 1
                elif len(c[i]) > 1:
                    c[i] = c[i][1:]
                else:
                    del c[i]
                    i -= 1
            i += 1
    
    for i in c:
        d[i[0][0]] = t + i[0][1]
        
    for i in list(map(lambda x:int(x) - 1,input().split())):
        print('{0:02}:{1:02}'.format(d[i] // 60 + 8,d[i] % 60) if i in d else "Sorry")
    


    编辑于 2020-02-21 10:29:31 回复(0)
    #include <cstdio>
    #include <algorithm>
    struct Customer
    {
        int ptime;
        //int w;//窗口号
        int ot;//结束时间
        int st;//开始服务时间
        Customer()
        {
            ot=0;
        }
    }cus[1001];
    struct Window
    {
        int c;//黄线内的顾客数
        int etime[21];//窗口结束服务的时间
    }win[21];
    int main()
    {
        int n,m,k,q;
        scanf("%d%d%d%d",&n,&m,&k,&q);
        int i,j,s;
        for(i=1;i<k+1;i++)
        {
            scanf("%d",&cus[i].ptime);
        }
        int res[1001]={0};
        int f=0;
        /*if(k <= n)
        {
            for(i=1;i<k+1;i++)
            {
                if(res[i] != 0)
                {
                    f=res[i];
                    if(cus[f].ot != 0)
                    printf("%02d:%02d\n",(cus[f].ptime+480)/60,(cus[f].ptime+480)%60);
                }
            }
            return 0;
        }*/
        //初始化完成
        int opent=8*60;
        for(i=0;i<22;i++)
        {
            win[i].c=0;
            for(j=0;j<22;j++)
                win[i].etime[j]=480;
        }
        int shutt=17*60,minend,pos,minc;
        for(i=1;i<k+1;i++)
        {
            minc=m;
            minend=1000000;
            pos=-1;
            for(j=1;j<n+1;j++)
            {
                if(win[j].c < minc)
                {
                    minc=win[j].c;//查看当前队列是否已经排满
                    pos=j;
                }
            }
            if(minc < m)//如果没有排满,就直接排到人数最少的队列后面
            {
                win[pos].etime[minc+1] = win[pos].etime[minc]+cus[i].ptime;//更新新入队的成员的服务结束时间
                cus[i].st=win[pos].etime[minc];//新入队的成员的开始服务时间是上一个成员的服务结束时间
                cus[i].ot = win[pos].etime[minc+1];//结束时间是开始服务时间加上处理时间
                win[pos].c++;//排队的人数增加
            }
            else//如果队伍排满
            {
                for(j=1;j<n+1;j++)//找到队列正在服务的成员最先结束服务的那个窗口
                {
                    if(minend > win[j].etime[1])
                    {
                        minend=win[j].etime[1];
                        pos=j;
                    }
                }
                for(s=1;s<m;s++)
                {
                    win[pos].etime[s] = win[pos].etime[s+1];//队首服务结束后,后面的成员向后移
                }
                //把当前的顾客加入到此队列中
                cus[i].st = win[pos].etime[m];//存储开始服务时间为上一个服务结束的时间
                win[pos].etime[m] = win[pos].etime[m]+cus[i].ptime;//更新当前队列最后一个结束时间
                cus[i].ot = win[pos].etime[m];//结束时间为开始时间加上处理时间
                win[pos].c=m;
            }
                
        }
        int t=0;
        for(j=1;j<q+1;j++)
        {
            scanf("%d",&t);
            if(cus[t].st < shutt)
                printf("%02d:%02d\n",cus[t].ot / 60,cus[t].ot % 60);
            else
                printf("Sorry\n");
        }
        return 0;
    }


    //实在不知道哪里错了,例子和自己的一些举例都通过,pat就不过,自己认为思路没有问题,求大神指点!!!!!!!!!
    发表于 2020-01-30 13:54:17 回复(0)
    //思路:建立N个queue进行模拟
    //进行K次模拟,按次序进入,每个“人”有两个信息,服务时常和离开时间
    //模拟时:来一个走一个,与一起排序有区别吗
    #include <iostream>
    #include <cstdio>
    #include <queue>
    using namespace std;
    const int maxn = 21;
    const int maxk = 1010;
    const int INF = 100000000;
    int St = 0, Ed = (17 - 8) * 60;
    int N, M, K, Q;
    struct person{
    int time;//服务时常
    int left_time;//离开时间
    int start_time;
    };
    person per[maxk];
    queue<int> que[maxn];//队列是per的下标
    int que_last[maxn];

    void new_come(int x){
    int early = INF, u = -1;
    for (int j = 1; j <= N;++j){//比较各队列队首
    int first = que[j].front();
    if(per[first].left_time<early){
    early = per[first].left_time;
    u = j;//找出第u个最先离开的队列
    }
    }
    if(u==-1)
    return;
    per[x].start_time = que_last[u];
    per[x].left_time = que_last[u] + per[x].time;
    que[u].pop();
    que[u].push(x);
    que_last[u] = per[x].left_time;//开始时因为少了这句导致找不出错误
    }
    void Simulate(){
    for (int i = 1; i <= N; ++i){ //前N个人
    per[i].left_time = per[i].time;
    que[i].push(i);
    que_last[i] = per[i].left_time;
    }
    for (int j = 2; j <= M;++j){//前M排
    for (int i = 1; i <= N; ++i){
    int temp = (j - 1) * N + i;//第temp个人
    per[temp].left_time = que_last[i] + per[temp].time;
    que[i].push(temp);
    que_last[i] = per[temp].left_time;
    }
    }
    for (int i = N*M+1; i <= K; ++i)
    {
    new_come(i); //把i送入队
    }
    }

    int main(){
    cin >> N >> M >> K >> Q;
    for (int k = 1; k <= K;++k){
    scanf("%d", &per[k].time);
    }
    Simulate();
    for (int q = 0; q < Q;++q){
    //对每个查询输出结果
    int sb;
    scanf("%d", &sb);
    if(per[sb].start_time>=Ed)printf("Sorry\n");
    else{
    int HH, MM;
    MM = per[sb].left_time % 60;
    HH = 8 + per[sb].left_time / 60;
    printf("%02d:%02d\n",HH,MM);
    }
    }
    return 0;
    }

    发表于 2019-07-28 22:20:06 回复(0)
    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<string>
    #include<queue>
    using namespace std;

    //1014 Waiting in Line
    //8:00am开始,计算第i个客户done的时间
    //使用优先队列,窗口从0开始编号
    //模仿某AC代码,重写

    #define maxn 1001

    struct node {
        int wid;  //所用窗口
        int tktime;
        int stime;
        int endtime;
        bool operator <(const node &b) const {
            if (endtime != b.endtime) {
                return endtime > b.endtime; //优先队列为大根堆
            }
            else {
                return wid > b.wid;//结束时间相同时,优先小窗口
            }
        }
    }customers[maxn], current;

    string to_time(const node &n) {
        //把分钟加在8:00am上,拼成真实时间
        //17:00之后因为银行下班,未能开始服务的输出sorry
        if (n.stime >= 540) return "Sorry";
        char h[3],m[3]; //必须留一位给'\0'不然会越界
        sprintf(h, "%02d", 8 + n.endtime / 60);
        sprintf(m, "%02d", n.endtime % 60);
        string s(h);
        s += ":";//+=m
        s += m;

        return s;
    }

    priority_queue<node> que;//优先队列

    int main(int _argc, char *_argv[], char *_get_initial_narrow_environment[]) {
        int n, m; //n个窗口<=20,每个窗口最多排m人<=10
        int k, q;//k个客户<=1000,q个query请求<=1000
        int w_time[21] = {}; //每个窗口最后一人服务结束时间
        int wid,query;//
        cin >> n >> m >> k >> q;

        unsigned int c = n * m; //黄线内等待区容量
        for (int i = 0; i<k; ++i) {
            cin >> customers[i].tktime;

            if (que.size() < c) {
                //前c个客户,直接进黄线,按序号分配窗口
                //当i=c-1时,q.size()=c-1,为边界条件
                //此时i表示第c个客户,仍然遵守按序号分配的规则
                wid = i % n;//窗口从0开始编号
            
            else{//达到c个之后,出一个才能进一个,始终满足q.size()=c
                current = que.top();//最早完成的出列
                que.pop();
                wid = current.wid; //继承出列者的窗口id
            }
            customers[i].wid = wid; //将进入编号为wid的窗口排队
            customers[i].stime = w_time[wid]; //前一个人的完成时间
            customers[i].endtime = w_time[wid]+ customers[i].tktime; //自己的完成时间
            w_time[wid] = customers[i].endtime; //更新窗口的最后一个人完成时间
            que.push(customers[i]);
        }

        //结束后队列里还剩最后C个人 
        //但是这些人的信息已经算出来了,所以不必去pop出来
        for (int i = 0; i < q; ++i) {
            cin >> query;//本次要查询的客户
            //query是从1~k编号的,而我们的数组是0~k-1
            cout << to_time(customers[query - 1]) << endl;
        }

        return 0;
    }



    发表于 2019-01-15 12:18:33 回复(0)
    #include<iostream>
    #include<vector>
    using namespace std;
    int N, M, K, Q;
    int processTime[1010];
    int result[1010];
    vector<int> queues[22];
    int qSum[22] = { 0 };
    int minFrontQueue(vector<int> *queues,int &minTime)
    {
     int minIndex = 0;
     minTime = 99999999;
     for (int i = 0; i < N; i++)
     {
      if (queues[i].size()<M||(queues[i].front() < minTime))//人没满或队首最小
      {
       minIndex = i;
       if (queues[i].size() <M)
        minTime = 0;
       else minTime = queues[i].front();
      }
     }
     return minIndex;
    }
    int minSizeQueue(vector<int> *queues)
    {
     int minQueueIndex = 0;
     for (int i = 0; i < N; i++)
     {
      if (queues[i].size() < queues[minQueueIndex].size())
      {
       minQueueIndex = i;
      }
     }
     return minQueueIndex;
    }
    int main()
    {
     cin >> N >> M >> K >> Q;
     for (int i = 0; i < K; i++)
     {
      cin >> processTime[i];
     }
     //运算后面等的人,每次选择所有队伍中时间最小的队伍,然后更新其他队伍
     int i =0;
     while (i < K)
     {
      //寻找size最小的队伍插入
      int insertIndex = minSizeQueue(queues);
      if (queues[insertIndex].size()<M)//不满的时候才插入,否则不插并更新队伍
      {
       queues[insertIndex].push_back(processTime[i]);
       qSum[insertIndex] += processTime[i];
       result[i] = qSum[insertIndex];
       i++;
      }
      //更新所有的队伍,找出时间最少的队伍弹出并减去该时间
      int minTime;
      int minFrontQueueIndex = minFrontQueue(queues,minTime);
      for (int j = 0; j < N; j++)
      {
       if (queues[j].size() != 0)
       {
        queues[j][0] -= minTime;
        //处理完毕则弹出
        if (queues[j][0] <= 0)
        {
         queues[j].erase(queues[j].begin());
        }
       }
      }
     }
     //输出
     for (int i = 0; i < Q; i++)
     {
      int a;
      cin >> a;
      int hour = 8;
      int min = 0;
      hour += result[a - 1] / 60;
      min += result[a - 1] % 60;
      if (hour > 17 || (hour == 17 && min > 0))
      {
       cout << "Sorry" << endl;
      }
      else
      {
       printf("%02d:%02d\n", hour,min);
      }
     }
     return 0;
    }

    用例过了,pat那边过了一半,这边一个都没过,实在是不知道为什么了,求解答!!
    发表于 2018-12-28 20:37:06 回复(0)
    import sys
    def tran(t):
        h = t//60
        m = t%60
        return "%02d"%(h+8)+":"+"%02d"%m
    start,end = 8*60,17*60
    n,m,k,l = map(int,input().split())
    cust = list(map(int,input().split()))
    ans = [None for i in range(k)]
    window = [start for i in range(n)]
    people = [False for i in range(n)]
    number = [0 for i in range(n)]
    gestmap = [[sys.maxsize for j in range(n)] for i in range(m)]
    gestindexmap = [[sys.maxsize for j in range(n)] for i in range(m)]
    con = n*m
    while con:
        for j in range(m):
            for i in range(n):
                gestmap[j][i] = cust[i+j*n]
                gestindexmap[j][i] = i+j*n
                number[i]+=1
                con-=1
    query = list(map(int,input().split()))
    con,tcon = 0,0
    i,gestindex = 0,n*m
    while con<end - start:
        if tcon<min(gestmap[0]):
            tcon+=1
            con+=1
        else:
            for i in range(n):
                gestmap[0][i]-=tcon
            for i in range(n):
                if gestmap[0][i]==0:
                    ans[gestindexmap[0][i]] = start+con
                    window[i]+=tcon
                    number[i]-=1
                    if window[i]<=end and number[i]<m:
                        people[i]=True
                    tempindex = 0
                    for j in range(m):
                        if gestmap[j][i]!=sys.maxsize:
                            tempindex += 1
                        else:
                            break
                    for j in range(tempindex-1):
                        gestmap[j][i] = gestmap[j+1][i]
                        gestindexmap[j][i] = gestindexmap[j+1][i]
                    for j in range(n):
                        if number[j]==min(number):
                            tp = j
                            break
                    if gestindex<k and people[tp]:
                        gestmap[tempindex-1][tp] = cust[gestindex]
                        gestindexmap[tempindex-1][tp] = gestindex
                        gestindex += 1
                        number[tp]+=1
                    else:
                        gestmap[tempindex-1][i]=sys.maxsize
                        gestindexmap[tempindex-1][i]=sys.maxsize
            tcon = 0
    for i in range(n):
        if gestmap[0][i]!=sys.maxsize:
            ans[gestindexmap[0][i]] = end+gestmap[0][i]-tcon
    for i in query:
        if ans[i-1]!=None:
            print(tran(ans[i-1]-start))
        else:
            print("Sorry")
    
    通过,其思路是采用时间戳处理。
    发表于 2018-12-23 22:11:16 回复(0)
    思路:使用队列模拟队列,思路在代码注释里面,请仔细看吧,属于理解力题目
    mb,理解了半个小时怎么考试???
    #include <iostream>
    #include <fstream>
    #include <map>
    #include <vector>
    #include <string>
    #include <string.h>
    #include <queue>
    using namespace std;
    
    int nWindows, mInYellows, kCustomers, qQuerys;
    int mp[1002]; // 顾客序号,消耗时间
    queue<int> qWindows[20];//窗口队列
    // 计算每一个客户离开的时间
    
    string Int2StringTime(int i)
    {
        string time;
        int h, m;
        h = i / 60 + 8;
        m = i % 60;
        time = (h / 10) + '0';
        time +=  (h % 10 + '0');
        time += ":";
        time += (m / 10) + '0';
        time += (m % 10) + '0';
        return time;
    }
    
    void Calculate(vector<string> & v)
    {
        // 建立初始状态 建立n条队列
        int cusNum = 1;
        for (int i = 0; i < mInYellows; i++)
        {
            for (int j = 0; j < nWindows; j++)
            {
                qWindows[j].push(cusNum++);
                if (cusNum > kCustomers)
                {
                    break;
                }
            }
            if (cusNum > kCustomers)
            {
                break;
            }
        }
        // 开始每个窗口消耗时间 结束的话填入v队列时间然后mp如果有多余的数据那么补充进去
        for (int j = 1; j < 9 * 60; j++)// 只处理到16:59
        {
            for (int i = 0; i < nWindows; i++)
            {
                if (!qWindows[i].empty())// 队列不为空的话开始 消耗时间
                {
                    mp[qWindows[i].front()]--;//如果mp[x] == 0 表示客户已经处理好了那么 就会填入一个新的数据
                    if (mp[qWindows[i].front()] == 0)
                    {
                        if (cusNum <= kCustomers)
                        {
                            qWindows[i].push(cusNum++);
                        }
                        v[qWindows[i].front()] = Int2StringTime(j);
                        qWindows[i].pop();
                    }
                }
            }
        }
        // 如果还有在窗口正在处理的人处理完毕剩下的不再处理
        for (int i = 0; i < nWindows; i++)
        {
            if (!qWindows[i].empty())
            {
                v[qWindows[i].front()] = Int2StringTime(9 * 60 - 1 + mp[qWindows[i].front()]);
                mp[qWindows[i].front()] = 0;
            }
        }
        // 对于剩下的人赋予超时的时间
        for (int i = 0; i < kCustomers; i++)
        {
            if (mp[i + 1] != 0)
            {
                v[i + 1] = "Sorry";
            }
        }
    
    }
    
    int main()
    {
        //ifstream cin("test.txt");
        
        while (cin >> nWindows >> mInYellows >> kCustomers >> qQuerys)
        {
            memset(mp, 0, 1001);
            int consumeTime;
            for (int i = 0; i < kCustomers; i++)
            {
                cin >> consumeTime;
                mp[i+1] = consumeTime;
            }
            vector<string> customerLeaveTime(kCustomers+1);
            Calculate(customerLeaveTime);
            for (int i = 0; i < qQuerys; i++)
            {
                cin >> consumeTime;
                cout << customerLeaveTime[consumeTime] << endl;
            }
        }
        system("pause");
    }
    

    发表于 2018-08-22 15:02:45 回复(0)
    全部通过
    #include<stdio.h>
    #include<queue>
    #include<vector>
    using namespace std;
    int n,m,k,q;
    int take[1001];
    int done[1001]={0};
    queue<int> win[21];
    int work[21]={0};
    int searchq(){
        int min=m,mini=0;
        for(int i=1;i<=n;i++){
            if(win[i].size()<min){
                min=win[i].size();
                mini=i;
            }
        }
        if(min==m)
            return -1;
        else
            return mini;
    }
    int freewin(){
        int num=0;
        for(int i=1;i<=n;i++){
            if(win[i].size()<m)
                num++;
        }
        return num;
    }
    bool gomin(int curt){
        for(int i=1;i<=n;i++){
            if(work[i]==0)
                continue;
            work[i]--;
            if(work[i]==0){
                int c=win[i].front();
                done[c]=curt;
                win[i].pop();
                if(win[i].size()>0)
                    work[i]=take[win[i].front()];
                else
                    work[i]=0;//nobody in front of the window
            }
        }
        return 1;
    }
    void printt(int c){
        int mins=done[c];
        if(mins==-1){printf("Sorry\n");return;}
        int hour=mins/60;
        int min=mins%60;

        printf("%02d:%02d\n",hour+8,min);
    }
    int main(){
        scanf("%d %d %d %d",&n,&m,&k,&q);

        for(int i=1;i<=k;i++){
            scanf("%d",take+i);    
        }
        int curc=1;
        for(;curc<=n*m&&curc<=k;curc++){
            win[(curc-1)%n+1].push(curc);
        }
        for(int i=1;i<=n&&i<=k;i++)
            {work[i]=take[i];}

        fill(done,done+1001,-1);
        int w=0;int freenum=0;
        for(int t=1;t<=60*9;t++){//serve to 17:00
            gomin(t);
            freenum=freewin();
            if(freenum==0){
                continue;
            }
            for(int j=0;j<freenum;j++){
                w=searchq();
                if(w!=-1){
                    win[w].push(curc);
                    curc++;
                }
            }
        }
        for(int i=1;i<=n;i++){//after 17:00
            if(work[i]>0){
                if(take[win[i].front()]>work[i])
                    done[win[i].front()]=9*60+work[i];
            }
        }

        int tmp;
        for(int i=0;i<q;i++){
            scanf("%d",&tmp);
            printt(tmp);
        }
        getchar();
        getchar();
        return 0;
    }

     
    编辑于 2018-08-12 11:31:10 回复(0)
    #include<iostream>
    #include<queue>
    #include<vector>
    #include<string>
    #include<map>
    #define INT_MAX 1000000;
    
    using namespace std;
    
    int Cid = 1;
    
    int findLeastTimeQue(vector<queue<int>>& queues){
        int smallid = 0;
        int smallestTime = INT_MAX;
        for (int i = 0; i < queues.size(); ++i){
            if (queues[i].size() == 0)
                continue;
            if (queues[i].front() < smallestTime){
                smallestTime = queues[i].front();
                smallid = i;
            }
        }
        return smallid;
    }
    
    void enQueue(vector<queue<int>>& queues, int m, map<int, int>& times, queue<int>& needTime,
        vector<queue<int>>& queueCid){
        int shortest = INT_MAX;
        int shortestId = 0;
        for (int i = 0; i < queues.size(); ++i){
            int len = queues[i].size();
            if (len == 0){
                queues[i].push(needTime.front());
                queueCid[i].push(Cid++);
                needTime.pop();
                return;
            }
            if (len < shortest){
                shortest = len;
                shortestId = i;
            }
        }
        if (shortest == m){//所有队列都满了,寻找所有队头时间最少的出队列,然后再让新的客户进入该队列
            int smallid = findLeastTimeQue(queues);
    
            int ansTime = queues[smallid].front();
            int ansId = queueCid[smallid].front();
            queues[smallid].pop(); queueCid[smallid].pop();
            times[ansId] = ansTime;//刚办理完的客户出队列,然后记录时间
            int lastTime = queues[smallid].back();
            queues[smallid].push(needTime.front() + lastTime);
            queueCid[smallid].push(Cid++);
            needTime.pop();//新客户进队列
        }
        else{//还有剩余
            int lastTime = queues[shortestId].back();
            queues[shortestId].push(needTime.front() + lastTime);
            queueCid[shortestId].push(Cid++);
            needTime.pop();
        }
    }
    
    string format(int time){
        int hour = 8 + time / 60;
        int min = time % 60;
        string ans;
        if (hour > 9){
            if (min > 9)
                ans = to_string(hour) + ":" + to_string(min);
            else
                ans = to_string(hour) + ":0" + to_string(min);
        }
        else{
            if (min > 9)
                ans = "0" + to_string(hour) + ":" + to_string(min);
            else
                ans = "0" + to_string(hour) + ":0" + to_string(min);
        }
        return ans;
    }
    
    void trans(vector<queue<int>>& queues, map<int, int>& times, int num, vector<queue<int>>& queueCid){
        for (int i = 0; i < num; ++i){
            int smallid = findLeastTimeQue(queues);
            int time = queues[smallid].front();
            int id = queueCid[smallid].front();
            queues[smallid].pop(); queueCid[smallid].pop();
            times[id] = time;
        }
    }
    
    int main(){
        int n, m, k, q;
        cin >> n >> m >> k >> q;
        vector<queue<int>> queues(n);//n条队列
        vector<queue<int>> queuesCid(n);//记录每个客户id
        map<int, int> times;//客户id:结束时间
        queue<int> needTime;
        for (int i = 0; i < k; ++i){
            int time;
            cin >> time;
            needTime.push(time);
            enQueue(queues, m, times, needTime, queuesCid);
        }
        vector<int> query;
        for (int i = 0; i < q; ++i){
            int id;
            cin >> id;
            query.push_back(id);
        }
        int num = k
    
    m*n>0 ? m*n : k;
        trans(queues, times, num,queuesCid);
        for (int j = 0; j < query.size(); ++j){
            int id = query[j];
            if (times[id]>540){
                cout << "Sorry" << endl;
            }
            else{
                cout << format(times[id]) << endl;
            }
        }
    }
    

    没有一个ac是什么情况,看了好久了,求解答

    发表于 2018-06-04 20:22:14 回复(0)

    注意如果需要记录两个时间:每个用户开始办理业务的时间和结束办理的时间,因为是否能够在银行关门前办理看的是开始时间。

    #include <iostream>
    #include <deque>
    #include <vector>
    #include <iomanip>
    using namespace std;
    
    deque<int> queue[25];
    vector<int> process_times;
    vector<int> query_list;
    int finish_time[1005] = { 0 };
    int start_time[1005] = { 0 };
    
    int main()
    {
        int N, M, K, Q;
        cin >> N >> M >> K >> Q;
        int time, number;
        int cur_time = 8 * 60;
        int end_time = 17 * 60;
        int index = 0;
        int waiting_num = 0;
        for (int i = 0; i < K; i++) {
            cin >> time;
            process_times.push_back(time);
        }
        for (int i = 0; i < Q; i++) {
            cin >> number;
            query_list.push_back(number);
        }
        while (index < K) {
            if (waiting_num < M * N) {
                //不满队的时候入队
                int shortest_queue = 0;
                for (int i = 0; i < N; i++) {
                    if (queue[i].size() < queue[shortest_queue].size()) {
                        shortest_queue = i;
                    }
                }
    
                start_time[index] = queue[shortest_queue].empty() ? cur_time 
                    : finish_time[queue[shortest_queue].back()];
                finish_time[index] = start_time[index] + process_times[index];
                queue[shortest_queue].push_back(index);
                index++;
                waiting_num++;
            }
            else
            {
                //满队的时候要出队
                cur_time = finish_time[queue[0].front()];
                for (int i = 0; i < N; i++) {
                    if (finish_time[queue[i].front()] < cur_time)
                        cur_time = finish_time[queue[i].front()];
                }
                for (int i = 0; i < N; i++) {
                    if (finish_time[queue[i].front()] == cur_time) {
                        queue[i].pop_front();
                        waiting_num--;
                    }
                }
            }
        }
        for (int i = 0; i < Q; i++) {
            if (start_time[query_list[i] - 1] >= end_time)
                cout << "Sorry" << endl;
            else 
                cout << setw(2) << setfill('0')    << finish_time[query_list[i] - 1] / 60 
                << ":" << setw(2) << setfill('0') << finish_time[query_list[i] - 1] % 60 << endl;
        }
    
        return 0;
    }
    编辑于 2018-02-09 20:50:54 回复(0)