news 2026/6/10 16:18:46

事件驱动通用思路(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/6/9 14:19:27

Android16 EDLA 认证测试CTS问题分析解决

Android16 EDLA 认证测试CTS问题分析解决 文章目录Android16 EDLA 认证测试CTS问题分析解决一、前言二、EDLA认证项分析解决1、修改系统签名和系统应用签名比如 CtsBionicTestCases 模块报错&#xff1a;2、烧录keyCtsDevicePolicyManagerTestCases 模块报错&#xff1a;3、修改…

作者头像 李华
网站建设 2026/6/10 12:46:23

今日2个,明日3个,这么频繁到底为什么……

有些人一面就能通过&#xff0c;有些人却会屡屡求职失败&#xff0c;失败的主要原因是面试者根本就不知道面试官到底要什么。 所以面试时&#xff0c;大家要不断提醒自己&#xff1a;我是来找工作的&#xff0c;必须考虑清楚说什么样的话&#xff0c;做什么样的事&#xff0c;才…

作者头像 李华
网站建设 2026/6/10 13:29:37

终极音频剪辑神器:Audiogrep让声音处理变得如此简单

终极音频剪辑神器&#xff1a;Audiogrep让声音处理变得如此简单 【免费下载链接】audiogrep Creates audio supercuts. 项目地址: https://gitcode.com/gh_mirrors/au/audiogrep 在当今多媒体时代&#xff0c;音频内容的处理往往需要耗费大量时间和精力。Audiogrep作为一…

作者头像 李华
网站建设 2026/6/10 13:50:39

论文阅读:arxiv 2025 Red Teaming Large Reasoning Models

总目录 大模型相关研究&#xff1a;https://blog.csdn.net/WhiffeYF/article/details/142132328 https://arxiv.org/pdf/2512.00412 https://www.doubao.com/chat/33384155469624578 论文翻译&#xff1a; https://whiffe.github.io/Paper_Translation/LLM_Thinking/BenchMa…

作者头像 李华
网站建设 2026/6/10 7:25:19

6类网线最建议买的三个品牌

6类网线最建议买的三个品牌在网络连接日益重要的今天&#xff0c;6类网线凭借其高速稳定的传输性能&#xff0c;成为了众多用户构建网络环境的首选。然而&#xff0c;市场上6类网线品牌众多&#xff0c;质量参差不齐&#xff0c;如何选择成为了一大难题。下面为您推荐三个值得购…

作者头像 李华
网站建设 2026/6/10 15:30:26

gRPC-web轻松上手:零基础构建现代Web通信应用

gRPC-web轻松上手&#xff1a;零基础构建现代Web通信应用 【免费下载链接】grpc-web gRPC for Web Clients 项目地址: https://gitcode.com/gh_mirrors/gr/grpc-web 在当今Web开发领域&#xff0c;gRPC-web通信协议正在重新定义前端与后端的数据交互方式。如果您还在为R…

作者头像 李华