PAT1014 Waiting in Line (30)

1014 Waiting in Line (30)(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 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, customer~1~ is served at window~1~ while customer~2~ is served at window~2~. Customer~3~ will wait in front of window~1~ and customer~4

纯模拟
代码1自己写的 写了2个h 模拟至死
代码2借鉴了别人的 对啊就是个队列啊!!!
17:00是个坑点 上班之前接的锅再迟也得干完

代码1

#include <bits/stdc++.h>
using namespace std;
#define MAXN 0x3f3f3f3f
int custom[10200];
int custom1[10200];
int query[10200];
int N,M,K,Q;
vector<int> window[30];
int finish[10200];
int allsize;
void add(int x,int index){
    if(x-custom1[index]>=540){
        printf("Sorry\n");
    }
    else{
        printf("%02d:%02d\n",8+x/60,x%60);
    }
}
int findMin(){
    int m=MAXN;
    for(int i=1;i<=N;i++){
        if(window[i].size()&&custom[window[i][0]]<m){
            m=custom[window[i][0]];
        }
    }
    return m;
}
int findWindow(){
    int m=MAXN;
    int emm=0;
    for(int i=1;i<=N;i++){
        int k=window[i].size();
        if(k<m){
            m=k;
            emm=i;
        }
    }
    return emm; 
}
int main(){
    cin>>N>>M>>K>>Q;
    for(int i=1;i<=K;i++){
        cin>>custom[i];
        custom1[i]=custom[i];//另外开了一个数组记录值
    }
    allsize=K;//记录剩余的顾客
    for(int i=1;i<=K;i++){//按照窗口分配顾客
        window[(i-1)%N+1].push_back(i);
    }
    int now=0;//当前时间
    while(1){
        int m=findMin();//找到第一个窗口前所有窗口处理时间的最小值
        now+=m;//时间移动
        for(int i=1;i<=N;i++){
            if(window[i].size()==0) continue;
            custom[window[i][0]]-=m;//有顾客 时间-m 
            if(!custom[window[i][0]]){//遇到0的顾客
                finish[window[i][0]]=now;//结束
                window[i].erase(window[i].begin());//滚吧
                allsize--;//个数--
            }
        }
        int insert=findWindow();//找到最小列
        if(allsize==0){//人都解决完了
            int s;
            for(int i=1;i<=Q;i++){
                cin>>s;
                add(finish[s],s);
            }
            return 0;
        }
        else{
            for(int j=1;j<=N;j++){//查找黄线后的顾客看能不能移动到合适的队列
                int q=window[j].size();
                if(q>M){
                    int ans=window[j][M];
                    if(j!=insert){//自己排到后面去的是傻逼 最后一个点加了这句就过了
                        window[j].erase(window[j].begin()+M);
                        window[insert].push_back(ans);  
                    }
                    break;
                }
            }
        }
    }   
    return 0;
}

代码2

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
struct Customer{
    int process;
    int leave;
};
int main(){
    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);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值