news 2026/3/23 8:50:17

事件驱动通用思路(java版)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
事件驱动通用思路(java版)

何为事件驱动?

1 时间范围很大

2 真正有用的信息只发生在少数时间

3 中间有一大段时间的规则是线性的

4 暴力容易超时

思路:第一步记录所有事件,第二步按照时间排序,第三步事件发生->先补中间时间->再处理事件,第四步最后补到终点时间。

具体模版:

定义内部类

static class Event { int time; // 事件发生时间 int id; // 对象编号(人 / 店 / 设备) Event(int t, int i) { time = t; id = i; } }

读入加排序

Event[] events = new Event[m]; for (int i = 0; i < m; i++) { events[i] = new Event(sc.nextInt(), sc.nextInt()); } Arrays.sort(events, (a, b) -> a.time - b.time);

状态数组三件套

int[] value = new int[n + 1]; // 当前数值(分数 / 能量 / 状态) int[] last = new int[n + 1]; // 上一次事件时间 boolean[] flag = new boolean[n + 1]; // 是否满足某条件

核心模版

for (Event e : events) { int t = e.time; int id = e.id; //补中间时间(跳过) value[id] = Math.max(0, value[id] - (t - last[id] - 1)); //处理当前事件 value[id] += 事件带来的变化; //状态判断 if (value[id] > 上限) flag[id] = true; if (value[id] <= 下限) flag[id] = false; //更新时间 last[id] = t; }

尾处理(容易忘)

for (int i = 1; i <= n; i++) { value[i] = Math.max(0, value[i] - (END - last[i])); if (value[i] <= 下限) flag[i] = false; }

输出答案:

int res = 0; for (int i = 1; i <= n; i++) { if (flag[i]) res++; } System.out.println(res);

例:外卖优先级问题

import java.util.*; public class Main { static class Order { int time, shop; Order(int t, int s) { time = t; shop = s; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // 店铺数 int m = sc.nextInt(); // 订单数 int T = sc.nextInt(); // 截止时间 Order[] orders = new Order[m]; for (int i = 0; i < m; i++) { orders[i] = new Order(sc.nextInt(), sc.nextInt()); } // 按时间排序 Arrays.sort(orders, (a, b) -> a.time - b.time); int[] score = new int[n + 1]; // 当前优先级 int[] last = new int[n + 1]; // 上一次接单时间 boolean[] in = new boolean[n + 1];// 是否在优先缓存 // 事件驱动处理订单 for (Order o : orders) { int t = o.time; int s = o.shop; // 中间空闲时间衰减 score[s] = Math.max(0, score[s] - (t - last[s] - 1)); // 当前订单 score[s] += 2; // 状态判断 if (score[s] > 5) in[s] = true; if (score[s] <= 3) in[s] = false; last[s] = t; } // 尾处理:最后一次订单到 T for (int i = 1; i <= n; i++) { score[i] = Math.max(0, score[i] - (T - last[i])); if (score[i] <= 3) in[i] = false; } // 统计答案 int res = 0; for (int i = 1; i <= n; i++) { if (in[i]) res++; } System.out.println(res); } }

主要还是事件的更新

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/20 5:40:07

每次提出一个bug都让测试重现,描述得那么清楚,自己操作下不会吗?

一说到测试和开发的关系&#xff0c;你一定会想到一个词“冤家”。 开发的工作就是按照PM的设计将产品最终造出来&#xff0c;而测试则是在开发已完成的工作里纠错。so&#xff0c;测试的工作会让开发很不爽&#xff0c;人之常情&#xff0c;谁都不喜欢自己的劳动成果被别人挑…

作者头像 李华
网站建设 2026/3/22 7:56:30

终极指南:Artillery负载测试3分钟快速入门 [特殊字符]

终极指南&#xff1a;Artillery负载测试3分钟快速入门 &#x1f680; 【免费下载链接】artillery Load testing at cloud-scale, as easy as 1-2-3. Serverless & distributed out-of-the-box. Never fail to scale! 项目地址: https://gitcode.com/gh_mirrors/ar/artill…

作者头像 李华
网站建设 2026/3/14 6:28:32

硬件工程师成长终极指南:159页深度技术解析

硬件工程师成长终极指南&#xff1a;159页深度技术解析 【免费下载链接】华为硬件工程师手册全159页PDF介绍 这份华为硬件工程师手册是硬件领域学习的宝贵资源&#xff0c;涵盖159页的深度内容&#xff0c;从职责技能到设计流程&#xff0c;全面解析华为硬件工程师的工作精髓。…

作者头像 李华
网站建设 2026/3/22 9:13:28

wangEditor实现excel数据动态绑定更新

《一个码农的CMS奇幻漂流》 需求评审会&#xff1a;当客户说"很简单"的时候… 各位父老乡亲好啊&#xff01;我是福建厦门一名"资深"前端码农&#xff08;资深加班多&#xff09;。刚接到个CMS官网需求&#xff0c;看完需求文档我直接表演了个闽南式震惊…

作者头像 李华
网站建设 2026/3/22 11:33:00

Data Formulator终极指南:零代码实现AI驱动数据可视化革命

还在为复杂的数据可视化代码而烦恼吗&#xff1f;面对海量数据却不知从何下手&#xff1f;Data Formulator的出现彻底改变了这一局面——通过直观的拖拽操作和AI智能辅助&#xff0c;任何人都能在几分钟内创建专业级数据可视化报告。 【免费下载链接】data-formulator &#x1…

作者头像 李华
网站建设 2026/3/22 6:25:36

LLM训练算力优化终极指南:多后端引擎完整教程

LLM训练算力优化终极指南&#xff1a;多后端引擎完整教程 【免费下载链接】verl verl: Volcano Engine Reinforcement Learning for LLMs 项目地址: https://gitcode.com/GitHub_Trending/ve/verl 你是否在为大规模语言模型训练时的算力瓶颈而苦恼&#xff1f;面对70B以…

作者头像 李华