D. DS队列----银行单队列多窗口模拟

输入样例

 9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3

 关于这道题我觉得自己写得蛮有意思的

把排队的人变成一个队列,然后逐个去选择,然后pop();

选择时选择窗口空闲时间最早的那个,也就是nexttime 最小的那个

有两种情况,一种是顾客到的时候,有窗口空闲,那么窗口的下一个空闲时间变成顾客的到达时间加上完成时间

一种是顾客到的时候没空闲,那么选择时间短的,等待时间由窗口下一个空闲时间减去到达时间

先计算等待时间才能计算下一个时间,这是我踩的一个雷,代码的运行顺序会影响变量的值

为了减少判断时间,把第一个顾客安排在第一个窗口,然后弹出队头的顾客

#include <bits/stdc++.h>
using namespace std;
class guke{
public:
    int arrive_time;
    int work_time;
    guke(int arriveTime, int workTime) : arrive_time(arriveTime), work_time(workTime) {}
};
class window{
public:
   // windows should jilu nexttime , withtime ,longgestwithtime
    double nexttime;
    double withtime;
    double lwtime;

    window() {
        nexttime=0;
        withtime=0;
        lwtime=0;
    }

    void jisuan(guke guketemp)
    {
        // guke arrive free window, needn't with
        if(guketemp.arrive_time>=nexttime)
        {
            nexttime=guketemp.arrive_time+guketemp.work_time;
        }
        //guke shoule with
        else
        {
            //nexttime = when guke finish
            //guke withtime =nexttime-guke arrive time, winodw withtime +=
            double gukewithtime=nexttime-guketemp.arrive_time;
            nexttime+=guketemp.work_time;
            withtime+=gukewithtime;
            //bijiao withtime longer?
            if(gukewithtime>lwtime)
                lwtime=gukewithtime;
        }


    }
};

int lookforwhichnear(int arrivetime,window* windowlist,int windownum)
{
    // look for which nearest
    int nexttimenear=windowlist[0].nexttime;
    int choose=0;
    for (int i = 1; i <windownum ; ++i) {
        //if xiaoyu nexttime,nextime change,choose=i;
        if(windowlist[i].nexttime<nexttimenear) {
            nexttimenear=windowlist[i].nexttime;
            choose = i;
        }
    }
    return choose;
}

int main()
{
    //shuru guke chuangjian gukelist
    int gukenum;
    cin>>gukenum;
    queue<guke> gukelist;
    for (int i = 0; i < gukenum; ++i) {
        int arrivetime,worktime;
        cin>>arrivetime>>worktime;
        guke guke1(arrivetime,worktime);
        gukelist.push(guke1);
    }

    //shuru windownum,jianli window[]
    int windownum;
    cin>>windownum;
    window windowlist[windownum+1];
    //jiaru diyige guke;
    guke guketemp=gukelist.front();
    //first guke do not with,
    windowlist[0].nexttime=guketemp.arrive_time+guketemp.work_time;
   //jiaru wan,guke yao likai duiwu
   gukelist.pop();
    //one by one xuanze
    while(!gukelist.empty()){
       guketemp=gukelist.front();
        //jiancha which windows nexttime is nearer choose
       int chosewhich=lookforwhichnear(guketemp.arrive_time,windowlist,windownum);
       // which near,which should change nexttime and so on;
       windowlist[chosewhich].jisuan(guketemp);
       //paiwandui,next one;
       gukelist.pop();
    }
    //jiancha which windows nexttime is nearer choose
    //windows should jilu nexttime , withtime ,longgestwithtime

    // bijiao which windows nextime bigger

    //jisuan withtimesum
    double withsum=0;
    double lwtime=0;
    double lnexttime=0;
    for (int i = 0; i <windownum ; ++i) {
        withsum+=windowlist[i].withtime;
        //bijiao longgestwithtime;
        if(windowlist[i].lwtime>lwtime)
            lwtime=windowlist[i].lwtime;
        //bijiao nexttime
        if(windowlist[i].nexttime>lnexttime)
            lnexttime=windowlist[i].nexttime;
    }
    withsum/=gukenum;
    cout<<fixed<<setprecision(1)<<withsum<<" ";
    cout<<(int)lwtime<<" ";
    cout<<(int)lnexttime<<endl;
}

第一个博客,写写写写

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值