solution Of 1014. Waiting in Line (30)

来源:互联网 发布:独立分销商城源码 编辑:程序博客网 时间:2024/05/01 14:07

1014. Waiting in Line (30)

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 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output
08:07
08:06
08:10
17:00
Sorry


结题思路 :
题意要求我们模拟完成对客户的服务。
要求1:能够在15:00之前得到服务的即使他的结束时间是15:45,也会产生服务;
要求2:对于满员的情况,在查找最早产生空余位置的窗口的sum值尽可能得大(case4);

程序步骤:
第一步、在有空位的前提下将客户安排到窗口队列;
第二步、在窗口满员的情况下开始产生服务,腾出空位;
第三步、对于最后留在窗口队列里面的客户进行一次批处理。

具体程序(AC)如下:

#include<iostream>#include<cstdlib>#include<vector>#include<queue>using  namespace std;const int endTime=540;//8点开始计时的分int tp,w,v,tq;struct window{    int curTime;    int curV;    queue<int> line;};struct visitor{    int win;    int need;    int end;};vector<window> wins;vector<visitor> vis;int findAviableWin()//返回人数最少的窗口号{    int i;    int seat=-1;    int tot=v;    for(i=0;i<w;++i)        if(wins[i].curV<tot)        {            tot=wins[i].curV;            seat=i;        }    return seat;}void batchServe(){//clear 队列中的客户    int i,j;    int visNo;    for(i=0;i<w;++i){        while(!wins[i].line.empty()){            visNo=wins[i].line.front();            if(wins[i].curTime>=endTime)                break;            wins[i].curTime+=vis[visNo].need;            vis[visNo].end=wins[i].curTime;            wins[i].line.pop();            --wins[i].curV;        }    }}int finishEarliest(){//找到最早有空闲位置的窗口    int i;    int seat=0;    int sum=0xfffffff;    for(i=0;i<w;++i){        if(wins[i].curTime+vis[wins[i].line.front()].need<sum){            sum=wins[i].curTime+vis[wins[i].line.front()].need;            seat=i;        }    }    return  seat;}void process(){    int i;    int win=-1;    int no;    bool have=false;    for (i = 0; i < tp; ++i)    {        if(!have)            win=findAviableWin();        if(win!=-1)//若有空闲位就将客户排入黄线内        {            vis[i].win=win;            wins[win].line.push(i);            ++wins[win].curV;            have=false;        }        else{//无空闲位,这时开始处理客户,腾出空位            have=true;            win=finishEarliest();            if(wins[win].curTime>=endTime)                break;            no=wins[win].line.front();            wins[win].curTime+=vis[no].need;            vis[no].end=wins[win].curTime;            wins[win].line.pop();            --wins[win].curV;//已经服务过的客户出队            --i;//重新进行处理        }    }    batchServe();}int main(int argc, char const *argv[]){    /* code */    scanf("%d%d%d%d",&w,&v,&tp,&tq);    int i;    for(i=0;i<w;++i){        window tmp;        tmp.curTime=0;        tmp.curV=0;        wins.push_back(tmp);    }    for(i=0;i<tp;++i){        visitor tmp;        scanf("%d",&tmp.need);        tmp.end=tmp.win=-1;        vis.push_back(tmp);    }    process();    int query;    for(i=0;i<tq;++i)    {        scanf("%d",&query);        --query;        if(vis[query].end!=-1)            printf("%02d:%02d\n",vis[query].end/60+8,vis[query].end%60);        else            printf("Sorry\n");    }    return 0;}
0 0