双指针、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);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/2869608.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

solr/ES 分词插件Jcseg设置自定义词库

步骤&#xff1a; 1、找到配置文件jcseg-core/target/classes/jcseg.properties修改配置&#xff1a; 下载地址: https://gitee.com/lionsoul/jcseg#5-如何自定义使用词库 lexicon.path {jar.dir}/../custom-word 设置lexicon路径&#xff0c;我们这个配置可以自定义&#xf…

Qt教程 — 3.3 深入了解Qt 控件:Input Widgets部件(2)

目录 1 Input Widgets简介 2 如何使用Input Widgets部件 2.1 QSpinBox组件-窗口背景不透明调节器 2.2 DoubleSpinBox 组件-来调节程序窗口的整体大小 2.3 QTimeEdit、QDateEdit、QDateTimeEdit组件-编辑日期和时间的小部件 Input Widgets部件部件较多&#xff0c;将分为三…

很好的一本书,推荐给你们《Hello 算法》

算法犹如美妙的交响乐&#xff0c;每一行代码都像韵律般流淌。 愿这本书在你的脑海中轻轻响起&#xff0c;留下独特而深刻的旋律。 本项目旨在打造一本开源免费、新手友好的数据结构与算法入门教程。 全书采用动画图解&#xff0c;内容清晰易懂、学习曲线平滑&#xff0c;引导…

Linux使用git命令行教程

. 个人主页&#xff1a;晓风飞 专栏&#xff1a;数据结构|Linux|C语言 路漫漫其修远兮&#xff0c;吾将上下而求索 文章目录 git安装git仓库的创建.git 文件添加文件git 三板斧(add,commit,push)解释拓展git log.gitignore git安装 首先输入git --version看看有没有安装git 如…

白话模电:3.三极管(考研面试与笔试常考问题)

一、三极管的简单判断 1.判断三极 1)给了图 左边是b,有箭头是e,剩下是c 2)给了电位 b:中间值&#xff0c;e:较近值(离中间值)&#xff0c;c:较远值(离中间值) 2.判断流向 bc同向(共同流向“|”或共同流离“|”)&#xff0c;e与bc反向 3.判断材料 4.判断类型 5.判断能否构…

2024 年值得关注的三大 DevOps 趋势

在过去几年中&#xff0c;DevOps 世界以前所未有的速度发展&#xff0c;但它仍然是许多组织效率、创新和数字化转型的主要驱动力。 Google 的 2023 年 加速 DevOps 状态报告显示&#xff0c;公司的软件交付性能质量可以预测组织绩效、团队绩效和员工福祉。 2024年&#xff0c…

智慧交通:构建智慧城市的重要一环

随着信息技术的飞速发展&#xff0c;智慧城市已成为现代城市发展的重要方向。作为智慧城市的重要组成部分&#xff0c;智慧交通以其高效、便捷、环保的特性&#xff0c;成为推动城市现代化进程的关键力量。本文将从智慧交通的概念、发展现状、面临挑战以及未来趋势等方面&#…

如何构建Docker自定义镜像

说明&#xff1a;平常我们使用Docker运行各种容器&#xff0c;极大地方便了我们对开发应用的使用&#xff0c;如MySQL、Redis&#xff0c;以及各种中间件&#xff0c;使用时只要拉镜像&#xff0c;运行容器即可。本文介绍如何创建一个Demo&#xff0c;自定义构建一个镜像。 开…

JVM学习-底层字节码的执行过程

目录 1.一个简单的程序分析 2. a&#xff0c;a&#xff0c;a--在JVM中的执行过程 3. 一个好玩的xx 4.方法调用的字节码分析、多态的实现、对象头 5. try-catch-finally的字节码分析 5.1 try-catch 5.2 try-catch-finally 5.3特殊情况 5.3.1 try和finally块中都出现了re…

【AI】Ubuntu系统深度学习框架的神经网络图绘制

一、Graphviz 在Ubuntu上安装Graphviz&#xff0c;可以使用命令行工具apt进行安装。 安装Graphviz的步骤相对简单。打开终端&#xff0c;输入以下命令更新软件包列表&#xff1a;sudo apt update。之后&#xff0c;使用命令sudo apt install graphviz来安装Graphviz软件包。为…

oops-framework框架 之 启动流程(三)

引擎&#xff1a; CocosCreator 3.8.0 环境&#xff1a; Mac Gitee: oops-game-kit 回顾 上篇博客中我们通过 oops-game-kit 模版构建了基础的项目&#xff0c;另外讲解了下assets目录结构和游戏配置文件的基本使用相关&#xff0c;详情内容可参考&#xff1a; oops-framewo…

【Python使用】python高级进阶知识md总结第4篇:静态Web服务器-命令行启动动态绑定端口号,html 的介绍【附代码文档】

python高级进阶全知识知识笔记总结完整教程&#xff08;附代码资料&#xff09;主要内容讲述&#xff1a;操作系统&#xff0c;虚拟机软件&#xff0c;Ubuntu操作系统&#xff0c;Linux内核及发行版&#xff0c;查看目录命令&#xff0c;切换目录命令&#xff0c;绝对路径和相对…

复习 --- windows 上安装 git,使用相关命令

文章目录 很少使用windows的git工具&#xff0c;这次借助这个任务&#xff0c;记录下使用过程&#xff0c;其他的等有空在整理。 其中&#xff0c;还使用了浏览器的AI小助手&#xff0c;复习了git相关的命令&#xff1a;图片放最后

实现兼容性良好的前端页面开发

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

SpringAI——Java生态接入LLM

最近&#xff0c;Spring官网发布了SpringAI&#xff0c;可点此查看https://spring.io/blog/2024/03/12/spring-ai-0-8-1-released&#xff0c;对于SpringAI的介绍&#xff0c;可看官方文档&#xff1a;https://spring.io/projects/spring-ai#overview。 本文将使用SpringAI配合…

Github Copilot 工具,无需账号,一键激活

① 无需账号&#xff0c;100%认证成功&#xff01;0风险&#xff0c;可联网可更新&#xff0c;&#xff0c;支持copilot版本升级&#xff0c;支持chat ② 支持windows、mac、linux系统等设备 ③一号通用&#xff0c;支持所有IDE(AppCode,CLion,DataGrip,GoLand,IntelliJ IDEA …

【计算机网络】https的工作原理以及和http的区别

目录 前言 1. HTTP协议存在的问题 2. 什么是HTTPS协议&#xff1f; 3. HTTP和HTTPS有哪些区别&#xff1f; 4. HTTPS的工作原理 加密方式 前言 在日常的Web项目练习中&#xff0c;我们会发现老师会让我们在打开服务器之后使用 http://localhost/...进行项目效果测试和预览…

zookeeper基础学习之六: zookeeper java客户端curator

简介 Curator是Netflix公司开源的一套zookeeper客户端框架&#xff0c;解决了很多Zookeeper客户端非常底层的细节开发工作&#xff0c;包括连接重连、反复注册Watcher和NodeExistsException异常等等。Patrixck Hunt&#xff08;Zookeeper&#xff09;以一句“Guava is to Java…

无人机助力智慧农田除草新模式,基于YOLOv5全系列【n/s/m/l/x】参数模型开发构建无人机航拍场景下的农田杂草检测识别系统

科技发展到今天&#xff0c;无人机喷洒药物已经不是一件新鲜事情了&#xff0c;在很多高危的工作领域中&#xff0c;比如高空电力设备除冰&#xff0c;电力设备部件传送更换等等&#xff0c;无人机都可以扮演非常出色的作用&#xff0c;前面回到老家一段时间&#xff0c;最近正…

压铸模实现3D打印关键在材料

3D打印技术通过逐层堆积粉末状材料&#xff0c;可以制造出具有复杂形状和内部结构的模具零件&#xff0c;突破了传统加工方法的限制。这种设计自由度的提升使得模具制造更加精准和高效&#xff0c;如3D打印随形水路、随形透气钢等的应用&#xff0c;满足了现代制造业对高精度、…