客户中心模拟(Queue and A, ACM/ICPC World Finals 2000, UVa822)rust解法

你的任务是模拟一个客户中心运作情况。客服请求一共有n(1≤n≤20)种主题,每种主题用5个整数描述:tid, num, t0, t, dt,其中tid为主题的唯一标识符,num为该主题的请求个数,t0为第一个请求的时刻,t为处理一个请求的时间,dt为相邻两个请求之间的间隔(为了简单情况,假定同一个主题的请求按照相同的间隔到达)。
客户中心有m(1≤m≤5)个客服,每个客服用至少3个整数描述:pid, k, tid1, tid2, …,
tidk ,表示一个标识符为pid的人可以处理k种主题的请求,按照优先级从大到小依次为tid1,tid2, …, tidk 。当一个人有空时,他会按照优先级顺序找到第一个可以处理的请求。如果有多个人同时选中了某个请求,上次开始处理请求的时间早的人优先;如果有并列,id小的优先。输出最后一个请求处理完毕的时刻。

分析:
每个请求项,都由其优先级最高的那个客服处理。
比如,
A客服优先级为[1 ,3, 2, 4]
B客服优先级为[2, 1, 3,]
那么请求项3,要经过两轮选择,之后由A处理。
请求项2,则只经过一轮选择,由B处理。
请求项4,要经过四轮选择,由A处理。

样例:
输入

3
128 20 0 5 10
134 25 5 6 7
153 30 10 4 5
4
10 2 128 134
11 1 134
12 2 128 153
13 1 15315
1 68 36 23 2
2 9 6 19 60
3 67 10 6 49
4 49 44 23 66
5 81 8 18 35
6 99 85 85 75
7 94 75 94 96
8 29 7 67 28
9 100 95 11 89
10 29 16 10 29
11 32 55 10 15
12 70 48 4 84
13 100 36 63 73
14 42 93 28 47
15 100 35 2 73
3
1 13 1 2 3 4 5 6 7 8 9 11 12 13 14
2 10 2 3 4 5 9 10 11 12 14 15
3 11 1 2 3 4 5 6 7 9 13 14 15

输出

finish time 195
finish time 13899

解法:

use std::{collections::{BTreeSet, BinaryHeap},io,
};
#[derive(Debug)]
struct Request {id: usize,num: usize,t0: usize,t: usize,dt: usize,
}#[derive(Debug, PartialEq, Eq, Clone, Copy)]
struct RequestItem {req_id: usize,arrive_time: usize,process_time: usize,
}
impl Ord for RequestItem {fn cmp(&self, other: &Self) -> std::cmp::Ordering {other.arrive_time.cmp(&self.arrive_time)}
}
impl PartialOrd for RequestItem {fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {Some(self.cmp(other))}
}#[derive(Debug, PartialEq, Eq, Clone)]
struct Server {id: usize,num: usize,req_ids: Vec<usize>,start_process_time: usize,finsh_process_time: usize,
}
impl Ord for Server {fn cmp(&self, other: &Self) -> std::cmp::Ordering {if self.finsh_process_time != other.finsh_process_time {other.finsh_process_time.cmp(&self.finsh_process_time)} else if self.start_process_time != other.start_process_time {other.start_process_time.cmp(&self.start_process_time)} else {other.id.cmp(&self.id)}}
}
impl PartialOrd for Server {fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {Some(self.cmp(other))}
}
fn main() {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let n: usize = buf.trim().parse().unwrap();let mut requests: Vec<Request> = vec![];for _i in 0..n {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let v: Vec<usize> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();requests.push(Request {id: v[0],num: v[1],t0: v[2],t: v[3],dt: v[4],});}//println!("{:?}", requests);let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let m: usize = buf.trim().parse().unwrap();let mut servers: BinaryHeap<Server> = BinaryHeap::new();for _i in 0..m {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let v: Vec<usize> = buf.split_whitespace().map(|x| x.parse().unwrap()).collect();servers.push(Server {id: v[0],num: v[1],req_ids: v[2..].to_vec(),start_process_time: 0,finsh_process_time: 0,});}//println!("{:?}", servers);let mut request_items: BinaryHeap<RequestItem> = BinaryHeap::new();let mut time_points: BTreeSet<usize> = BTreeSet::new();for r in requests.iter() {for i in 0..r.num {let item = RequestItem {req_id: r.id,arrive_time: r.t0 + r.dt * i,process_time: r.t,};time_points.insert(item.arrive_time);request_items.push(item);}}//println!("{:?}", request_items);let mut finish_time = 0;while request_items.len() > 0 {let t = time_points.pop_first().unwrap();//等待中的所有请求项let mut wait_items: Vec<RequestItem> = vec![];while let Some(i) = request_items.peek() {if i.arrive_time <= t {wait_items.push(*i);request_items.pop();} else {break;}}if wait_items.len() == 0 {continue;}//所有可用的客服let mut available_servers: Vec<Server> = vec![];while let Some(s) = servers.peek() {if s.finsh_process_time <= t {available_servers.push(s.clone());servers.pop();} else {break;}}//请求项和客服配对,按照优先级for i in 0..n {if available_servers.len() == 0 || wait_items.len() == 0 {break;}let mut j = 0;while j < wait_items.len() {let mut chosen_servers: Vec<&Server> = vec![];//同一个请求项可能有多个客服选中for server in available_servers.iter() {if server.num > i && server.req_ids[i] == wait_items[j].req_id {chosen_servers.push(server);}}if chosen_servers.len() > 0 {chosen_servers.sort_by(|a, b| {if a.start_process_time != b.start_process_time {a.start_process_time.cmp(&b.start_process_time)} else {a.id.cmp(&b.id)}});//分配第一个客服给请求项,之后把客服从可用列表中删除let mut server = chosen_servers[0].clone();for k in 0..available_servers.len() {if available_servers[k].id == server.id {available_servers.remove(k);break;}}server.start_process_time = t;server.finsh_process_time =server.start_process_time + wait_items[j].process_time;finish_time = finish_time.max(server.finsh_process_time);time_points.insert(server.finsh_process_time);servers.push(server);wait_items.remove(j); //把请求项从等待列表中删除} else {j += 1;}}}if wait_items.len() > 0 {request_items.append(&mut BinaryHeap::from(wait_items));}if available_servers.len() > 0 {servers.append(&mut BinaryHeap::from(available_servers));}}println!("finish time {}", finish_time);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/124072.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

centos遇到的问题

lsof -i :8091 > 查看这个端口的线程 lsof &#xff1a; list open files 列出打开文件 -i &#xff1a; internet linux检测系统进程和服务&#xff1a; top &#xff1a; 实时监视系统的进程和资源的利用情况htop &#xff1a; top的增强版 问题&#xff1a; -bash: …

气膜场馆里面噪声很大怎么解决?

随着气膜结构在各个领域的广泛应用&#xff0c;人们开始意识到在这些场馆内部&#xff0c;特别是在大型活动和展览中&#xff0c;噪声问题可能会变得相当严重。传统的气膜结构通常难以提供良好的声学环境&#xff0c;这对于参与者的舒适度和活动的质量构成了挑战。为了解决气膜…

vue3使用@vitejs/plugin-vue-jsx来满足jsx

vue3使用vitejs/plugin-vue-jsx来满足jsx jsx在vue3的使用语法 jsx在vue2种使用 1、安装插件vitejs/plugin-vue-jsx npm install --save-dev vitejs/plugin-vue-jsx2、在 vite.config.js 中添加插件 import { defineConfig } from vite import vue from vitejs/plugin-vue impo…

网站如何判断请求是来自手机-移动端还是PC-电脑端?如何让网站能适应不同的客户端?

如果网站需要实现手机和PC双界面适应&#xff0c;可以有两种方式&#xff1a; 第一种是响应式界面&#xff0c;根据屏幕宽度来判定显示的格式。这种需要前端来做&#xff0c;手机/PC共用一套代码&#xff0c;有一定的局限性。 第二种是后端通过request请求头中的内容来分析客户…

内网穿透实现在外远程访问NAS威联通(QNAP)

文章目录 前言1. 威联通安装cpolar内网穿透2. 内网穿透2.1 创建隧道2.2 测试公网远程访问 3. 配置固定二级子域名3.1 保留二级子域名3.2 配置二级子域名 4. 使用固定二级子域名远程访问 前言 购入威联通NAS后&#xff0c;很多用户对于如何在外在公网环境下的远程访问威联通NAS…

人工智能基础_机器学习011_梯度下降概念_梯度下降步骤_函数与导函数求解最优解---人工智能工作笔记0051

然后我们来看一下梯度下降,这里先看一个叫 无约束最优化问题,,值得是从一个问题的所有可能的备选方案中选最优的方案, 我们的知道,我们的正态分布这里,正规的一个正态分布,还有我们的正规方程,他的这个x,是正规的,比如上面画的这个曲线,他的这个x,就是大于0的对吧,而现实生活…

现代挖掘机vr在线互动展示厅是实现业务增长的加速度

VR数字博物馆全景展示充分应用5G、VR全景、web3d开发和三维动画等技术&#xff0c;将实体博物馆整体还原到3D数字空间&#xff0c;让游客360全景漫游式参观&#xff0c;无论大小、贵重、破损的典藏展品都能通过3D建模技术&#xff0c;逼真重现到三维虚拟场景中&#xff0c;让参…

从单模态到多模态,自主AI离我们还有多远?

一、从AI的诞生和发展说起 人工智能的发展&#xff0c;从思想诞生上&#xff0c;可以追逐到十七世纪的帕斯卡和莱布尼茨&#xff0c;1666年&#xff0c;德国博学家戈特弗里德威廉莱布尼茨发表了一篇题为《论组合的艺术》的神秘论文。当时的莱布尼茨只有20岁&#xff0c;他概述了…

python爬虫之正则表达式解析实战

文章目录 1. 图片爬取流程分析2. 实现代码—爬取家常菜图片 1. 图片爬取流程分析 先获取网址&#xff0c;URL&#xff1a;https://www.xiachufang.com/category/40076/ 定位想要爬取的内容使用正则表达式爬取导入模块指定URLUA伪装&#xff08;模拟浏览器&#xff09;发起请求…

SAP SPAD新建打印纸张

SAP SPAD新建打印纸张 1.事务代码SPAD 2.完全管理&#xff0d;设备类型&#xff0d;页格式-显示(创建格式页) 3.按标准A4纸张为模板参考创建。同一个纸张纵向/横向各创建1次(创建格式页) 4.完全管理&#xff0d;设备类型&#xff0d;格式类型-显示(创建格式类型&#xff0…

RabbitMQ如何保证消息不丢失呢?

RabbitMQ 是一个流行的消息队列系统&#xff0c;用于在分布式应用程序之间传递消息。要确保消息不会丢失&#xff0c;可以采取以下一些措施&#xff1a; 持久化消息&#xff1a; RabbitMQ 允许你将消息标记为持久化的。这意味着消息将被写入磁盘&#xff0c;即使 RabbitMQ 服务…

centos格式化硬盘/u盘的分区为NTFS格式

centos7好像不支持ntfs&#xff1f; 对报这个&#xff0c;ntfs not configured in kernel。 安装了ntfs-3g就可以访问了。 插上u盘查看u盘设备 #查看硬件设备及挂载目录 df -h #查看硬件设备&#xff08;包括未挂载的&#xff09; fdisk -l卸载外部设备 umount /dev/sdbxxx …

vite+vue3实现 tomcat 的本地部署

背景&#xff1a; 很多开发小伙伴在本地开发完前端项目后&#xff0c;碍于服务端环境配置麻烦&#xff0c;想先试试在本地部署&#xff0c;已开发好的前端项目&#xff0c;由于很多文章都是文字性描述&#xff0c;不太直观&#xff0c;为了给大多数新手提供一个教程&#xff0c…

企业文件防泄密方法

企业文件防泄密方法 安企神数据防泄密系统下载使用 企业文件是企业的核心资产&#xff0c;其中可能包含大量的敏感信息&#xff0c;如客户资料、产品配方、财务数据等。一旦这些文件泄露&#xff0c;可能会给企业带来不可估量的损失。 然而&#xff0c;企业文件防泄密是确保…

好用的API调试工具推荐:Apipost

随着数字化转型的加速&#xff0c;API&#xff08;应用程序接口&#xff09;已经成为企业间沟通和数据交换的关键。而在API开发和管理过程中&#xff0c;API文档、调试、Mock和测试的协作显得尤为重要。Apipost正是这样一款一体化协作平台&#xff0c;旨在解决这些问题&#xf…

mulesoft开发支撑

mulesoft开发支撑 开发支撑1. raml语法说明2. dataweave在线测试平台3. dataweave基础语法4. dataweave官方指南 感 开发支撑 1. raml语法说明 点击跳转 raml-10.md 重点看下面这部分内容&#xff0c;对raml语法做了详细说明和举例。 2. dataweave在线测试平台 点击跳转 d…

JavaEE-博客系统1(数据库和后端的交互)

本部分内容包括网站设计总述&#xff0c;数据库和后端的交互&#xff1b; 数据库操作代码如下&#xff1a; -- 编写SQL完成建库建表操作 create database if not exists java_blog_system charset utf8; use java_blog_system; -- 建立两张表&#xff0c;一个存储博客信息&am…

【Java】多线程案例(单例模式,阻塞队列)

> :heart: Author&#xff1a; 老九☕️ 个人博客&#xff1a;老九的CSDN博客 &#x1f64f; 个人名言&#xff1a;不可控之事 乐观面对 &#x1f60d; 系列专栏&#xff1a; 文章目录 实现安全版本的单例模式饿汉模式类和对象的概念类对象类的静态成员与实例成员 懒汉模…

vulnhub靶机Venus

下载地址&#xff1a;The Planets: Venus ~ VulnHub 主机发现 arp-scan -l 端口扫描 nmap --min-rate 1000 -p- 192.168.21.132 端口版本扫描 nmap -sV -sT -O -p22,8080 192.168.21.132 对于http-alt HTTP Alternative Services 介绍 | JerryQu 的小站 (imququ.com) 总结…