news 2026/4/15 21:49:44

事件驱动通用思路(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/4/16 12:31:53

计算机网络应用层理论知识全面解析

1. 应用层概述与理论基础 1.1 应用层在网络体系结构中的地位 应用层是计算机网络体系结构中的最高层&#xff0c;在 OSI 七层模型和 TCP/IP 协议栈中都占据着直接面向用户和应用程序的关键位置。作为整个网络体系中 "交付实际价值" 的核心层&#xff0c;应用层是计…

作者头像 李华
网站建设 2026/4/15 14:41:22

别再背八股文了:资深架构师眼里的 RunLoop、GCD 与线程保活真相

1. 撕开 RunLoop 的伪装&#xff1a;它不仅仅是一个死循环很多兄弟在面试时把 RunLoop 背得滚瓜烂熟&#xff1a;“它是管理事件循环的对象&#xff0c;让线程有事做事&#xff0c;没事休眠...” 听起来没毛病&#xff0c;但你在写代码时真的看见过它吗&#xff1f;在 main.m 那…

作者头像 李华
网站建设 2026/4/16 12:32:01

西门子变频器G120C:工业自动化控制的智能解决方案

西门子变频器G120C&#xff1a;工业自动化控制的智能解决方案 【免费下载链接】西门子变频器G120C使用手册分享 西门子变频器G120C使用手册欢迎来到西门子变频器G120C的官方使用手册页面 项目地址: https://gitcode.com/Open-source-documentation-tutorial/7ef48 想要快…

作者头像 李华
网站建设 2026/4/16 12:31:57

C/C++知识积累-Lambda表达式

目录 定义 语法结构 常见捕获列表 常见使用场景 Lambda的本质 Lambda的发展和问题 定义 C中的Lambda表达式是C11引入的新特性&#xff0c;允许在代码中定义匿名函数。简单而言&#xff0c;他就是一个匿名函数对象&#xff0c;通常用于简单、不需要复用、或者需要访问当前…

作者头像 李华
网站建设 2026/4/16 11:46:50

Vue2如何设计大文件上传的交互界面与用户体验?

大文件上传系统优化版&#xff08;JavaVue3SpringBoot&#xff09; 优化说明 经过实际测试和客户反馈&#xff0c;我对之前的方案进行了以下优化&#xff1a; 加密方案优化&#xff1a;改用CryptoJS实现AES加密&#xff0c;兼容IE9断点续传增强&#xff1a;增加MD5校验&…

作者头像 李华
网站建设 2026/4/16 1:29:55

BoringNotch安装配置教程:将MacBook凹口变为动态音乐控制中心

BoringNotch安装配置教程&#xff1a;将MacBook凹口变为动态音乐控制中心 【免费下载链接】boring.notch TheBoringNotch: Not so boring notch That Rocks &#x1f3b8;&#x1f3b6; 项目地址: https://gitcode.com/gh_mirrors/bor/boring.notch BoringNotch是一款创…

作者头像 李华