【PAT】1017. Queueing at Bank

考查点:模拟题

思路:用优先队列可提高效率,关键在于维护每个窗口的结束时间数组,每次遍历排队用户时更新结束时间,这里更新时多了个+号结果调了半天

#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAXN 10010
#define oo 0x3f3f3f3f
using namespace std;
struct customer{
    int start,process;
}person[10010];
bool cmp(customer a,customer b){
    return a.start<b.start;
}
int wait[110];

int main()
{
     #ifdef LOCAL
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif // LOCAL
    int n,k;
     int hh,mm,ss,pp;
    scanf("%d%d",&n,&k);
    int num=0;
    FOR(i,0,n-1)
    {
       scanf("%d:%d:%d %d",&hh,&mm,&ss,&pp);
       int tmp=hh*3600+mm*60+ss;

       if(tmp<=17*3600){
            person[num].process=(pp<60?pp*60:3600);
            person[num].start=tmp;
            num++;

       }
    }
    double sum=0;
    sort(person,person+num,cmp);
   
    FOR(i,0,k-1)
    wait[i]=8*3600;
    FOR(i,0,num-1)
    {
        int wmin=oo;int id;
        FOR(j,0,k-1)
        {
            if(wait[j]<wmin){
                wmin=wait[j];
                id=j;
            }


        }
        if(person[i].start<wait[id]){
                sum+=(wait[id]-person[i].start);
                wait[id]+=person[i].process;

        }else{
            wait[id]=(person[i].start+person[i].process);

        }
        

    }
    if(num==0) printf("0.0");
    else{

    printf("%.1f",sum/60.0/num);
    }

    return 0;
}

优先队列:由于默认最大值。所以要重载运算符

#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAXN 10010
#define oo 0x3f3f3f3f
using namespace std;
struct customer{
    int start,process;
  bool operator<(const customer& a)const{
        if(a.start<start)return true;
        else return false;
  }
}person;
struct window{
    int time;
    bool operator<(const window& a)const{
        if(time>a.time)return true;
        else return false;
    }
}w;
bool cmp(customer a,customer b){
    return a.start<b.start;
}
priority_queue<customer> cu;
priority_queue<window> ww;

int main()
{
     #ifdef LOCAL
        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif // LOCAL
    int n,k;
     int hh,mm,ss,pp;
    scanf("%d%d",&n,&k);
    int num=0;
    FOR(i,0,n-1)
    {
       scanf("%d:%d:%d %d",&hh,&mm,&ss,&pp);
       int tmp=hh*3600+mm*60+ss;

       if(tmp<=17*3600){
            person.process=(pp<60?pp*60:3600);
            person.start=tmp;

            cu.push(person);

       }
    }
    double sum=0;
    num=cu.size();

    FOR(i,0,k-1)
    {
        w.time=8*3600;
        ww.push(w);
    }


    while(!cu.empty())
    {
        w=ww.top();
        int endt=w.time;
        person=cu.top();
        cu.pop();
       if(endt<=person.start){
            endt=person.start+person.process;
            ww.pop();
            w.time=endt;
            ww.push(w);
        }else{
            sum+=(endt-person.start);
            endt+=person.process;
            ww.pop();
            w.time=endt;
            ww.push(w);
        }


    }
    if(num==0) printf("0.0");
    else{

    printf("%.1f",sum/60.0/num);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Queueing theory is a mathematical study of waiting lines or queues that arise in various real-life scenarios, such as customer service, traffic congestion, hospital emergency rooms, and telecommunications networks. Basic queueing theory involves the following concepts: 1. Arrival Process: This is the process of customers arriving at the queue. The arrival process can be modeled using different distributions, such as Poisson or exponential. 2. Service Process: This is the process of serving customers in the queue. The service process can also be modeled using different distributions, such as Poisson or exponential. 3. Queue Length: This is the number of customers waiting in the queue at any given time. 4. Queue Occupancy: This is the proportion of time that the server is busy serving customers. 5. System Capacity: This is the maximum number of customers that the system can handle at any given time. 6. Utilization: This is the proportion of time that the server is busy serving customers compared to the total time. 7. Waiting Time: This is the time that a customer spends waiting in the queue before being served. 8. Service Time: This is the time that a customer spends being served by the server. 9. Queueing Models: There are different queueing models that can be used to analyze queueing systems, such as the M/M/1 model, M/M/c model, M/G/1 model, and M/D/1 model. 10. Performance Measures: Different performance measures can be used to evaluate queueing systems, such as average waiting time, average queue length, and system throughput.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值