这是亚麻OA题

问题描述:

给一个 int[] arrival time, int[] Execution time, int q. 例子: 【0,1,4】 【5,2,3】 q=3. 输出的是average wait time 2.3333333

#include <iostream>     // std::cout
#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>       // std::vector
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <sys/time.h>
#include <list>
#include <map>//main point : ( refer to key data structure and the trick. )//introduce a timer (int ) to mark the current time.
//
//use map that can sort all the items to load the each job [Atime, Etime]
//
//compare the each current job ( map.begin()) with the timer and get the steps the timer will move forward and the remaing time if the running time is long.using namespace std;float RoundBin( vector<int>& Atime, vector<int>& Etime, int q){if  (q < 1 || Atime.size() == 0 || Etime.size()==0|| Atime.size() != Etime.size()) return 0;map<int , int > jobs;auto  num = Atime.size();//initialize the jobs.for (int i =0; i< num; i++)jobs[Atime[i]] = Etime[i];int timer = 0;int waiting  = 0;while (jobs.size() >0){auto curJob = * jobs.begin();// no job runing at this time.if (timer < curJob.first){timer ++;continue;}int steps = min(q, curJob.second);waiting += timer - curJob.first;timer += steps;cout << "the start time is " << curJob.first << "waitint time is " << waiting << " after running the timer is " << timer << endl;//if the cucrent jobs is not done completely, save the remaining work load.int remains = curJob.second - steps;if ( remains  > 0){jobs[timer] = remains;cout << "insert  a new job start  " << timer << " runing " << remains<< endl;}jobs.erase( curJob.first);}cout <<"waiting time is " << waiting << " num is "<< num << endl;return waiting /(float) num;
}int main () {//get the start time.struct timeval tv;gettimeofday(&tv,NULL);long ts = tv.tv_sec * 1000 + tv.tv_usec / 1000;//*** call the function .
vector<int > in1 = {0,1,4};vector<int > in2 = {5, 2 ,3};int q = 3;cout <<" the waiting time is : " << RoundBin(in1, in2, q) << endl;//*** end of the call//get the time of end.gettimeofday(&tv,NULL);long te = tv.tv_sec * 1000 + tv.tv_usec / 1000;//output the time of the running.cout<<endl<< endl<< "running tmie is : " << te - ts << endl;return 0;
}

转载于:https://www.cnblogs.com/HisonSanDiego/p/8283436.html

亚麻:Round Robin相关推荐

  1. Round Robin 算法

    什么是Round Robin? 先来看和他相近的名词,轮询调度算法(Round-Robin Scheduling) 轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N ...

  2. round robin权重轮循算法实现

    为什么80%的码农都做不了架构师?>>>    先上代码,采用php脚本语言 <?php/* * Copyright (C) FatHong*//* 数据初始化,weight: ...

  3. Nginx负载均衡的4种方式 :轮询-Round Robin 、Ip地址-ip_hash、最少连接-least_conn、加权-weight=n

    负载均衡的概念: Load Balance负载均衡是用于解决一台机器(一个进程)无法解决所有请求而产生的一种算法. 我们知道单台服务器的性能是有上限的,当流量很大时,就需要使用多台服务器来共同提供服务 ...

  4. 仲裁器设计(二)-- Round Robin Arbiter 轮询调度算法

    作者:李虹江 原文:https://mp.weixin.qq.com/s/r-nckE5nGz9mc5KqjPXKYg 本文授权转自IC加油站微信号,未经作者授权,严禁二次转载. 上一篇老李讲了固定优 ...

  5. 【数字IC/FPGA】仲裁器进阶--Round Robin Arbiter

    Round Robin Arbiter 固定优先级的缺点是:每个模块的优先级自始至终是固定不变的,这在某种程度上来说是不公平的,Round Robin就是考虑到公平性的一种仲裁算法.其基本思路是,当一 ...

  6. Round robin

    本篇文章先讲述轮询调度算法 (Round-Robin)及其在此基础上改进型的权重轮询算法 (Weighted Round-Robin). 轮询调度算法(Round-Robin Scheduling) ...

  7. Round Robin算法的简单C#实现

    整理笔记的时候,发现自己在项目中还用C#实现过一次Round Robin算法算法,当时的应用场景,是要根据权重给多个队列均匀的提供数据,对输入的数据进行拆分处理.算法很简单,最核心的部分,其实是取最大 ...

  8. DNS Round Robin

    DNS Round Robin 一个域名 string, 有好多 IP CNAME 搞定了 www.11 和 11

  9. Verilog权重轮询仲裁器设计——Weighted Round Robin Arbiter

    前两篇讲了固定优先级仲裁器的设计.轮询仲裁器的设计 Verilog固定优先级仲裁器--Fixed Priority Arbiter_weixin_42330305的博客-CSDN博客 Verilog轮 ...

  10. Verilog轮询仲裁器设计——Round Robin Arbiter

    上篇讲了固定优先级仲裁器的设计,并给出了指定最高优先级的实现方法 Verilog固定优先级仲裁器--Fixed Priority Arbiter_weixin_42330305的博客-CSDN博客 轮 ...

最新文章

  1. 【读书笔记】iOS-网络-解析响应负载
  2. 送你38个常用的Python库,数值计算、可视化、机器学习等8大领域都有了
  3. 文巾解题 627. 变更性别
  4. 移动App的发展趋势
  5. QT的QRegularExpressionValidator类的使用
  6. 基于JAVA+SpringMVC+Mybatis+MYSQL的图书租赁系统
  7. springboot 没有找到service_Spring Boot 应用程序五种部署方式
  8. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
  9. 认识Java虚拟机的基本结构
  10. 注意!SQLite被曝漏洞,Chrome 火狐等数千应用或受影响
  11. 数据库查询前十名和当前信息的排名信息
  12. 用python画散点图
  13. 怎么快速制作web原型_快速原型制作指南
  14. 计算机f2锁定用户,台式电脑键盘被锁住按什么键恢复 点锁定直接锁定计算机
  15. 高电平和低电平 到底是啥?
  16. 上岸快手,我选择一条不一样的路
  17. 【51nod】2589 快速讨伐
  18. 泰坦以太(以太流说) titan_ysl 2020.01.27
  19. java多线程 占用内存_java线程池常驻线程占内存吗
  20. [Java]JDK1.7中HashMap的并发死链

热门文章

  1. 异常详细信息: System.ArgumentException: 不支持关键字: “metadata”。
  2. Linux下基于官方源代码RPM包构建自定义MySQL RPM包
  3. ARC与非ARC混编
  4. SCCM 2012 Part 2 部署前AD准备
  5. 关于json的操作 jsonArray---jsonObject
  6. 刻骨铭心的startActivityForResult三级跳获得第三个Activity中返回的数据
  7. PIL图像处理:读取图像,显示图像,ROI,保存图像
  8. FPGA中实现对数运算
  9. 对分贝(dB)概念的理解
  10. Linux下软件安装方法汇总