当前位置: 首页 > 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…...

java_网络服务相关_gateway_nacos_feign区别联系

1. spring-cloud-starter-gateway 作用&#xff1a;作为微服务架构的网关&#xff0c;统一入口&#xff0c;处理所有外部请求。 核心能力&#xff1a; 路由转发&#xff08;基于路径、服务名等&#xff09;过滤器&#xff08;鉴权、限流、日志、Header 处理&#xff09;支持负…...

STM32标准库-DMA直接存储器存取

文章目录 一、DMA1.1简介1.2存储器映像1.3DMA框图1.4DMA基本结构1.5DMA请求1.6数据宽度与对齐1.7数据转运DMA1.8ADC扫描模式DMA 二、数据转运DMA2.1接线图2.2代码2.3相关API 一、DMA 1.1简介 DMA&#xff08;Direct Memory Access&#xff09;直接存储器存取 DMA可以提供外设…...

2021-03-15 iview一些问题

1.iview 在使用tree组件时&#xff0c;发现没有set类的方法&#xff0c;只有get&#xff0c;那么要改变tree值&#xff0c;只能遍历treeData&#xff0c;递归修改treeData的checked&#xff0c;发现无法更改&#xff0c;原因在于check模式下&#xff0c;子元素的勾选状态跟父节…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

【数据分析】R版IntelliGenes用于生物标志物发现的可解释机器学习

禁止商业或二改转载&#xff0c;仅供自学使用&#xff0c;侵权必究&#xff0c;如需截取部分内容请后台联系作者! 文章目录 介绍流程步骤1. 输入数据2. 特征选择3. 模型训练4. I-Genes 评分计算5. 输出结果 IntelliGenesR 安装包1. 特征选择2. 模型训练和评估3. I-Genes 评分计…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

网站指纹识别

网站指纹识别 网站的最基本组成&#xff1a;服务器&#xff08;操作系统&#xff09;、中间件&#xff08;web容器&#xff09;、脚本语言、数据厍 为什么要了解这些&#xff1f;举个例子&#xff1a;发现了一个文件读取漏洞&#xff0c;我们需要读/etc/passwd&#xff0c;如…...

【MATLAB代码】基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),附源代码|订阅专栏后可直接查看

文章所述的代码实现了基于最大相关熵准则(MCC)的三维鲁棒卡尔曼滤波算法(MCC-KF),针对传感器观测数据中存在的脉冲型异常噪声问题,通过非线性加权机制提升滤波器的抗干扰能力。代码通过对比传统KF与MCC-KF在含异常值场景下的表现,验证了后者在状态估计鲁棒性方面的显著优…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

windows系统MySQL安装文档

概览&#xff1a;本文讨论了MySQL的安装、使用过程中涉及的解压、配置、初始化、注册服务、启动、修改密码、登录、退出以及卸载等相关内容&#xff0c;为学习者提供全面的操作指导。关键要点包括&#xff1a; 解压 &#xff1a;下载完成后解压压缩包&#xff0c;得到MySQL 8.…...