双指针、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://www.hqwc.cn/news/544639.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java高级互联网架构师之路:排查当前JVM错误的步骤

程序 这个程序是有问题的,我们通过一些命令来分析这个程序究竟是哪里出了问题。首先把当前的程序通过SSH工具传输到centos系统中,之后我们就可以在linux环境下编译和执行。 注意一点:上面类的名字是Z,但是在linux环境下,我们将其改为了AA,并且文件名改为了AA,所以文章下…

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

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

单片机FLASH深度解析和编程实践(下)

本篇文章将同大家分享单片机FLASH编程的相关寄存器和寄存器操作及库函数操作。本篇文章依然以STM32单片机为例进行解析。有关FLASH的基本原理和实现方法&#xff0c;大家可以参考上一篇文章&#xff1a;单片机FLASH深度解析和编程实践&#xff08;上&#xff09;-CSDN博客 目录…

基于Struts开发物流配送(快递)管理系统

开发工具&#xff1a;EclipseJdkTomcatMySQL数据库

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

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

huawei services HK华为云服务

huaweiserviceshk是一种云计算服务&#xff0c;为华为云服务用户提供了多种服务&#xff0c;包括云服务器、数据库、存储、网络等&#xff0c;用户可以根据自己的需求选择不同的服务并支付相应的费用 如何付费呢&#xff0c;这里可以使用441112&#xff0c;点击获取 卡片信息在…

Lua中文语言编程源码-第一节,更改llex.c词法分析器模块, 使Lua支持中文关键词。

源码已经更新在CSDN的码库里&#xff1a; git clone https://gitcode.com/funsion/CLua.git 在src文件夹下的llex.c&#xff0c;是Lua的词法分析器模块。 增加中文保留字标识符列表&#xff0c;保留英文保留字标识符列表。 搜索“ORDER RESERVED”&#xff0c;将原始代码 …

Redisson 分布式锁原理分析

Redisson 分布式锁原理分析 示例程序 示例程序&#xff1a; public class RedissonTest {public static void main(String[] args) {Config config new Config();config.useSingleServer().setPassword("123456").setAddress("redis://127.0.0.1:6379"…

RISC-V 编译环境搭建:riscv-gnu-toolchain 和 riscv-tools

RISC-V 编译环境搭建&#xff1a;riscv-gnu-toolchain 和 riscv-tools 编译环境搭建以及说明 操作系统&#xff1a;什么系统都可以 虚拟机&#xff1a;VMmare Workstation Pro 17.50.x (版本不限) 编译环境&#xff1a;Ubuntu 18.04.5 CPU&#xff1a;i7-8750h(虚拟机分配4核…

IntelliJ IDEA 面试题及答案整理,最新面试题

IntelliJ IDEA中的插件系统如何工作&#xff1f; IntelliJ IDEA的插件系统工作原理如下&#xff1a; 1、插件架构&#xff1a; IntelliJ IDEA通过插件架构扩展其功能&#xff0c;插件可以添加新的功能或修改现有功能。 2、安装和管理&#xff1a; 通过IDEA内置的插件市场下载…

游戏引擎中的动画基础

一、动画技术简介 视觉残留理论 - 影像在我们的视网膜上残留1/24s。 游戏中动画面临的挑战&#xff1a; 交互&#xff1a;游戏中的玩家动画需要和场景中的物体进行交互。实时&#xff1a;最慢需要在1/30秒内算完所有的场景渲染和动画数据。&#xff08;可以用动画压缩解决&am…

【IC设计】Verilog线性序列机点灯案例(三)(小梅哥课程)

声明&#xff1a;案例和代码来自小梅哥课程&#xff0c;本人仅对知识点做做笔记&#xff0c;如有学习需要请支持官方正版。 文章目录 该系列目录设计目标设计思路RTL及Testbench代码RTL代码Testbench代码 仿真结果上板视频 该系列目录 Verilog线性序列机点灯案例(一)&#xff…