当前位置: 首页 > news >正文

双指针、bfs与图论

1238. 日志统计 - AcWing题库 

import java.util.*;class PII implements Comparable<PII>{int x, y;public PII(int x, int y){this.x = x;this.y = y;}public int compareTo(PII o){return Integer.compare(x, o.x);}
}public class Main{static int N = 100010, D, K;static int[] cnt = new int[N];static PII[] a = new PII[N];static boolean[] st = new boolean[N];public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();D = sc.nextInt();K = sc.nextInt();for(int i = 0; i < n; i ++){int ts = sc.nextInt();int id = sc.nextInt();a[i] = new PII(ts, id);}Arrays.sort(a, 0, n);for(int i = 0, j = 0; i < n; i ++){//i比j快int id = a[i].y;cnt[id] ++;while(a[i].x - a[j].x >= D){cnt[a[j].y] --;j ++;}if(cnt[id] >= K) st[id] = true;}for(int i = 0; i <= 100000; i ++){if(st[i]) System.out.println(i);}}
}

1101. 献给阿尔吉侬的花束 - AcWing题库 

import java.util.*;class PII{int x, y;public PII(int x, int y){this.x = x;this.y = y;}
}public class Main{static int N = 210;static int r, c;static char[][] g = new char[N][N];static int[][] dist = new int[N][N];static int[] dx = {-1, 0, 1, 0};static int[] dy = {0, 1, 0, -1};static PII start, end;public static int bfs(PII start, PII end){Queue<PII> q = new LinkedList<>();for(int i = 0; i < r; i ++){Arrays.fill(dist[i], -1);}q.offer(start);dist[start.x][start.y] = 0;while(!q.isEmpty()){PII t = q.poll();for(int i = 0; i < 4; i ++){int x = t.x + dx[i];int y = t.y + dy[i];if(x < 0 || x >= r || y < 0 || y >= c) continue;if(g[x][y] == '#') continue;if(dist[x][y] != -1) continue;dist[x][y] = dist[t.x][t.y] + 1;if(end.x == x && end.y == y) return dist[x][y];q.offer(new PII(x, y));}}return -1;}public static void main(String[] args){Scanner sc = new Scanner(System.in);int T = sc.nextInt();while(T -- > 0){r = sc.nextInt();c = sc.nextInt();for(int i = 0; i < r; i ++){char[] s = sc.next().toCharArray();for(int j = 0; j < c; j ++){g[i][j] = s[j];if(g[i][j] == 'S') start = new PII(i, j);if(g[i][j] == 'E') end = new PII(i, j);}}int res = bfs(start, end);if(res == -1) System.out.println("oop!");else System.out.println(res);}}
}

1113. 红与黑 - AcWing题库 

import java.util.*;public class Main{static int N = 25;static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};static int n, m, res;static char[][] g = new char[N][N];static boolean[][] st = new boolean[N][N];public static void dfs(int x, int y){res ++;st[x][y] = true;for(int i = 0; i < 4; i ++){int a = x + dx[i];int b = y + dy[i];if(a < 0 || b < 0 || a >= n || b >= m) continue;if(st[a][b]) continue;if(g[a][b] == '#') continue;dfs(a, b);}}public static void main(String[] args){Scanner sc = new Scanner(System.in);while(true){m = sc.nextInt();n = sc.nextInt();//这里是先行后列if(n == 0 && m == 0) break;res = 0;int x = -1, y = -1;for(int i = 0; i < n; i ++){String s = sc.next();for(int j = 0; j < m; j ++){g[i][j] = s.charAt(j);st[i][j] = false;if(g[i][j] == '@'){x = i;y = j;}}}dfs(x, y);System.out.println(res);}}
}

1224. 交换瓶子 - AcWing题库

import java.util.*;public class Main{static int N = 10010;static int[] a = new int[N];static boolean[] st = new boolean[N];public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();for(int i = 1; i <= n; i ++){a[i] = sc.nextInt();}int res = 0;for(int i = 1; i <= n; i ++){if(!st[i]){res ++;for(int j = i; !st[j]; j = a[j]){st[j] = true;}}}System.out.print(n - res);}
}

 

1240. 完全二叉树的权值 - AcWing题库

import java.util.*;public class Main{static int N = 100010;static int[] w = new int[N];public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();for(int i = 1; i <= n; i ++) w[i] = sc.nextInt();int depth = 0;long max = -0x3f3f3f3f;for(int i = 1, k = 1; i <= n; i *= 2, k ++){long sum = 0;for(int j = i; j < i + (1 << (k - 1)) && j <= n; j ++){sum += w[j];}if(sum > max){max = sum;depth = k;}}System.out.print(depth);}
}

 

1096. 地牢大师 - AcWing题库

import java.util.*;class PII{int x, y, z;public PII(int x, int y, int z){this.x = x;this.y = y;this.z = z;}
}public class Main{static int N = 110;static int L, R, C;static char[][][] g = new char[N][N][N];static int[][][] dist = new int[N][N][N];static boolean[][][] st = new boolean[N][N][N];static PII start, end;static int[] dx = {0, 0, -1, 0, 1, 0}, dy = {0, 0, 0, 1, 0, -1}, dz = {-1, 1, 0, 0, 0, 0};public static int bfs(){for(int i = 0; i < L; i ++){for(int j = 0; j < R; j ++){Arrays.fill(dist[i][j], -1);}}Queue<PII> q = new LinkedList<>();q.offer(start);dist[start.x][start.y][start.z] = 0;while(!q.isEmpty()){PII t = q.poll();for(int i = 0; i < 6; i ++){int a = t.x + dx[i];int b = t.y + dy[i];int c = t.z + dz[i];if(a < 0 || b < 0 || c < 0 || a >= L || b >= R || c >= C) continue;if(dist[a][b][c] != -1) continue;if(g[a][b][c] == '#') continue;dist[a][b][c] = dist[t.x][t.y][t.z] + 1;if(a == end.x && b == end.y && c == end.z) return dist[a][b][c];q.offer(new PII(a, b, c));}}return -1;}public static void main(String[] args){Scanner sc = new Scanner(System.in);while(true){L = sc.nextInt();R = sc.nextInt();C = sc.nextInt();if(L == 0 && R == 0 && C == 0) break;for(int i = 0; i < L; i ++){for(int j = 0; j < R; j ++){String s = sc.next();for(int k = 0; k < C; k ++){g[i][j][k] = s.charAt(k);if(g[i][j][k] == 'S') start = new PII(i, j, k);if(g[i][j][k] == 'E') end = new PII(i, j, k);}}}int res = bfs();if(res == -1) System.out.println("Trapped!");else System.out.printf("Escaped in %d minute(s).\n", res);}}
}

1233. 全球变暖 - AcWing题库

import java.util.*;class PII{int x, y;public PII(int x, int y){this.x = x;this.y = y;}
}public class Main{static int N = 1010;static int n;static char[][] g = new char[N][N];static boolean[][] st = new boolean[N][N];static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};public static boolean bfs(int x, int y){Queue<PII> q = new LinkedList<>();q.offer(new PII(x, y));st[x][y] = true;int max = 0;while(!q.isEmpty()){PII t = q.poll();int cnt = 0;//记录周围#的个数for(int i = 0; i < 4; i ++){int a = t.x + dx[i];int b = t.y + dy[i];if(a < 0 || b < 0 || a >= n || b >= n) continue;if(g[a][b] == '.') continue;cnt ++;if(!st[a][b]){st[a][b] = true;q.offer(new PII(a, b));}}max = Math.max(max, cnt);}if(max == 4) return true;else return false;}public static void main(String[] args){Scanner sc = new Scanner(System.in);n = sc.nextInt();for(int i = 0; i < n; i ++){String s = sc.next();for(int j = 0; j < n; j ++){g[i][j] = s.charAt(j);}}int res = 0;for(int i = 0; i < n; i ++){for(int j = 0; j < n; j ++){if(!st[i][j] && g[i][j] == '#'){if(!bfs(i, j)) res ++;}}}System.out.print(res);}
}

 1207. 大臣的旅费 - AcWing题库

import java.util.*;public class Main{static int N = 100010, M = 2 * N;static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];static int[] dist = new int[N];static boolean[] st = new boolean[N];static int n, idx;public static void add(int a, int b, int c){e[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx ++;}public static void bfs(int u){Arrays.fill(st, false);Queue<Integer> q = new LinkedList<>();dist[u] = 0;q.offer(u);st[u] = true;while(!q.isEmpty()){int t = q.poll();for(int i = h[t]; i != -1; i = ne[i]){int j = e[i];if(st[j]) continue;dist[j] = dist[t] + w[i];st[j] = true;q.offer(j);}}}public static void main(String[] args){Scanner sc = new Scanner(System.in);n = sc.nextInt();Arrays.fill(h, -1);for(int i = 1; i < n; i ++){int a = sc.nextInt();int b = sc.nextInt();int c = sc.nextInt();add(a, b, c);add(b, a, c);}bfs(1);int u = 1;for(int i = 2; i <= n; i ++){if(dist[i] > dist[u]) u = i;}bfs(u);int maxv = dist[1];for(int i = 2; i <= n; i ++){if(dist[i] > maxv) maxv = dist[i];}System.out.println(maxv * 10 + ((long)(maxv + 1) * maxv) / 2);}
}

相关文章:

双指针、bfs与图论

1238. 日志统计 - AcWing题库 import java.util.*;class PII implements Comparable<PII>{int x, y;public PII(int x, int y){this.x x;this.y y;}public int compareTo(PII o){return Integer.compare(x, o.x);} }public class Main{static int N 100010, D, K;st…...

RabbitMQ高级-高级特性

1.消息可靠性传递 在使用RabbitMQ的时候&#xff0c;作为消息发送方希望杜绝任何消息丢失或者投递失败场景。RabbitMQ为我们提供了两种方式来控制消息的投递可靠性模式 1.confirm 确认模式 确认模式是由exchange决定的 2.return 退回模式 回退模式是由routing…...

Word粘贴时出现“运行时错误53,文件未找到:MathPage.WLL“的解决方案

在安装完MathType后&#xff0c;打开word复制粘贴时报错“运行时错误53,文件未找到&#xff1a;MathPage.WLL” 首先确定自己电脑的位数&#xff08;这里默认32位&#xff09; 右击MathType桌面图标&#xff0c;点击“打开文件所在位置”&#xff0c; 然后分别找到MathPage.W…...

html元素基本使用

前言 大家好&#xff0c;我是jiantaoyab&#xff0c;第一次学习前端的html&#xff0c;写一篇笔记总结常用的元素 语义化 例如只要是 不管字体的大小是怎么样&#xff0c;有没有加粗都是标题&#xff0c;元素显示到页面中的效果应该由css决定&#xff0c;这就是语义化。 文…...

PHP+golang开源办公系统CRM管理系统

基于ThinkPHP6 Layui MySQL的企业办公系统。集成系统设置、人事管理、消息管理、审批管理、日常办公、客户管理、合同管理、项目管理、财务管理、电销接口集成、在线签章等模块。系统简约&#xff0c;易于功能扩展&#xff0c;方便二次开发。 服务器运行环境要求 PHP > 7.…...

smartmontools-5.43交叉编译Smartctl

嵌入式系统的sata盘经常故障&#xff0c;需要使用smatctl工具监控和诊断sata故障。 1. 从网上下载开源smartmontools-5.43包。 2. 修改makefile进行交叉编译。 由于软件包中已经包含Makefile.am&#xff0c;Makefile.in。直接运行 automake --add-missing 生成Makefile。 3.…...

idea找不到或无法加载主类

前言 今天在运行项目的时候突然出了这样一个错误&#xff1a;IDEA 错误 找不到或无法加载主类,相信只要是用过IDEA的朋友都 遇到过它吧&#xff0c;但是每次遇到都是一顿焦头烂额、抓耳挠腮、急赤白咧&#xff01;咋整呢&#xff1f;听我给你吹~ 瞧我这张嘴~ 问题报错 找不…...

2.二进制的方式读写文件

文章目录 写入文件代码运行结果 读出文件代码运行结果 文件打开模式标记&#xff08;查表&#xff09; 写入文件 ------写文件一共五步&#xff1a;------ 第一步&#xff1a;包含头文件 第二步&#xff1a;创建流对象 第三步&#xff1a;指定方式打开文件 第四步&#xff1a;…...

Seata的详细解释

什么是seata Seata&#xff08;Simple Extensible Autonomous Transaction Architecture&#xff09;是一个开源的分布式事务解决方案。它是由阿里巴巴集团开发的&#xff0c;旨在解决分布式系统中的事务一致性问题。 Seata提供了一种简单易用的方式来实现跨多个数据库和服务的…...

JS手写实现洋葱圈模型

解释洋葱圈模型&#xff1a; 当我们执行第一个中间件时&#xff0c;首先输出1&#xff0c;然后调用next()&#xff0c;那么此时它会等第二个中间件执行完毕才会继续执行第一个中间件。然后执行第二个中间件&#xff0c;输出3&#xff0c;调用next()&#xff0c;执行第三中间件…...

3.Windows下安装MongoDB和Compass教程

Windows下安装MongoDB 总体体验下来&#xff0c;&#xff0c;要比MySQL的安装简单了许多&#xff0c;没有过多的配置&#xff0c;直接就上手了&#xff01; 1、下载 进入官方的下载页面https://www.mongodb.com/try/download/community&#xff0c;如下选择&#xff0c;我选…...

go反射实战

文章目录 demo1 数据类型判断demo2 打印任意类型数据 demo1 数据类型判断 使用reflect.TypeOf()方法打印go中数据类型&#xff0c;可参考go官方API文档;使用格式化参数%T也能打印数据类型。 package mainimport "fmt" import "reflect" import "io&…...

Docker 中 MySQL 的部署与管理

目录 一、Docker 中部署 MySQL1.1 部署 MySQL1.2 进入容器并创建数据库1.3 Navicat 可视化工具连接 二、可能存在的问题2.1 1130 - Host ‘172.17.0.1‘ is not allowed to connect to this MySQL server 参考资料 一、Docker 中部署 MySQL 1.1 部署 MySQL 首先&#xff0c;从…...

基础练习题之函数

前言 这些题目来自与一些刷题网站,以及c primer plus,继续练习 第一题 给你一个数&#xff0c;让他进行巴啦啦能量&#xff0c;沙鲁沙鲁&#xff0c;小魔仙大变身&#xff0c;如果进行变身的数不满足条件的话&#xff0c;就继续让他变身。。。直到满足条件为止。 巴啦啦能量…...

Java NIO浅析

NIO&#xff08;Non-blocking I/O&#xff0c;在Java领域&#xff0c;也称为New I/O&#xff09;&#xff0c;是一种同步非阻塞的I/O模型&#xff0c;也是I/O多路复用的基础&#xff0c;已经被越来越多地应用到大型应用服务器&#xff0c;成为解决高并发与大量连接、I/O处理问题…...

数据挖掘与大数据的结合

随着大数据技术的不断发展和普及&#xff0c;数据挖掘在大数据环境下的应用也变得更加广泛和深入。以下将探讨大数据技术对数据挖掘的影响&#xff0c;以及如何利用大数据技术处理海量数据并进行有效的数据挖掘&#xff0c;同时分析大数据环境下的数据挖掘挑战和解决方案。 1.…...

分布式链路追踪(一)SkyWalking(2)使用

一、使用方法 1、简介 agent探针可以让我们不修改代码的情况下&#xff0c;对Java应用上使用到的组件进行动态监控&#xff0c;获取运行数据发送到OAP上进行统计和存储。agent探针在Java使用中是使用Java agent技术实现。不需要更改任何代码&#xff0c;Java agent会通过虚拟…...

【QT入门】VS2019+QT的开发环境配置

声明&#xff1a;该专栏为本人学习Qt知识点时候的笔记汇总&#xff0c;希望能给初学的朋友们一点帮助(加油&#xff01;) 往期回顾&#xff1a; 【QT入门】什么是qt&#xff0c;发展历史&#xff0c;特征&#xff0c;应用&#xff0c;QtCreator-CSDN博客【QT入门】Windows平台下…...

RTP 控制协议 (RTCP) 反馈用于拥塞控制

摘要 有效的 RTP 拥塞控制算法&#xff0c;需要比标准 RTP 控制协议(RTCP)发送方报告(SR)和接收方报告(RR)数据包提供的关于数据包丢失、定时和显式拥塞通知 (ECN) 标记的更细粒度的反馈。 本文档描述了 RTCP 反馈消息&#xff0c;旨在使用 RTP 对交互式实时流量启用拥塞控制…...

基于SpringBoot SSM vue办公自动化系统

基于SpringBoot SSM vue办公自动化系统 系统功能 登录 个人中心 请假信息管理 考勤信息管理 出差信息管理 行政领导管理 代办事项管理 文档管理 公告信息管理 企业信息管理 会议室信息管理 资产设备管理 员工信息管理 开发环境和技术 开发语言&#xff1a;Java 使用框架: S…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

【OSG学习笔记】Day 18: 碰撞检测与物理交互

物理引擎&#xff08;Physics Engine&#xff09; 物理引擎 是一种通过计算机模拟物理规律&#xff08;如力学、碰撞、重力、流体动力学等&#xff09;的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互&#xff0c;广泛应用于 游戏开发、动画制作、虚…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

Neo4j 集群管理:原理、技术与最佳实践深度解析

Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...

OPenCV CUDA模块图像处理-----对图像执行 均值漂移滤波(Mean Shift Filtering)函数meanShiftFiltering()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 在 GPU 上对图像执行 均值漂移滤波&#xff08;Mean Shift Filtering&#xff09;&#xff0c;用于图像分割或平滑处理。 该函数将输入图像中的…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”

2025年#高考 将在近日拉开帷幕&#xff0c;#AI 监考一度冲上热搜。当AI深度融入高考&#xff0c;#时间同步 不再是辅助功能&#xff0c;而是决定AI监考系统成败的“生命线”。 AI亮相2025高考&#xff0c;40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕&#xff0c;江西、…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

GruntJS-前端自动化任务运行器从入门到实战

Grunt 完全指南&#xff1a;从入门到实战 一、Grunt 是什么&#xff1f; Grunt是一个基于 Node.js 的前端自动化任务运行器&#xff0c;主要用于自动化执行项目开发中重复性高的任务&#xff0c;例如文件压缩、代码编译、语法检查、单元测试、文件合并等。通过配置简洁的任务…...

无人机侦测与反制技术的进展与应用

国家电网无人机侦测与反制技术的进展与应用 引言 随着无人机&#xff08;无人驾驶飞行器&#xff0c;UAV&#xff09;技术的快速发展&#xff0c;其在商业、娱乐和军事领域的广泛应用带来了新的安全挑战。特别是对于关键基础设施如电力系统&#xff0c;无人机的“黑飞”&…...