离散事件模拟-银行管理

~题目链接~

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2087&cid=1182

输入

1
1
60 10

结果

10.00

就是用 对列模拟
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>

using namespace std;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        //模拟窗口
        queue<int >Q;
        queue<int >P;
        int n,x,y,q=0,p=0,time=0;//q、p分别记录队列Q、P中最后一位的离开时间,time总时间统计
        scanf("%d",&n);
        for(int i=0; i<n; i++){
            scanf("%d%d",&x,&y);
            //判断离开
            while(!Q.empty()){
                if(Q.front()>x)
                    break;
                Q.pop();
            }
            while(!P.empty()){
                if(P.front()>x)
                    break;
                P.pop();
            }
            //入队
            if(Q.size()<=P.size()){
                //离开时间更新
                if(q>=x)
                    q+=y;
                else
                    q=x+y;
                //入站
                Q.push(q);
                time+=q-x;
            }
            else{
                if(p>=x)
                    p+=y;
                else
                    p=x+y;
                P.push(p);
                time+=p-x;
            }
        }
        double ave=1.0*time/n;
        printf("%.2lf
",ave);
    }
    
    return 0;
}

  

 

  



原文地址:https://www.cnblogs.com/guoyongzhi/p/3233044.html