双指针、bfs与图论

news2024/9/20 8:11:40

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.coloradmin.cn/o/1522122.html

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

相关文章

XCode打包IOS应用发布App Store和Ad Hoc测试

文章目录 零、前置说明一、创建本地证书二、配置描述文件2.1 配置certificates2.1.1 配置证书2.1.2 安装cer证书2.1.2.1 打包机器和生成证书同机器2.1.2.2 打包机器和生成证书不同机器 2.2 创建Identifiers2.3 配置Devices2.4 配置Profiles2.4.1 配置生产Profile2.4.2 配置开发…

MATLAB:拟合与插值

一、关于多项式的基本操作 若要求非线性方程的根&#xff0c;则采用fzero, fminbnd函数 二、多项式拟合 clc, clear x0:0.2:10; y0.25*x20*sin(x); plot(x,y,k.,MarkerSize,15) grid on; hold on [p1,s1,mu1]polyfit(x,y,3); %3阶多项式拟合 y1polyval(p1,x,s1,mu1); [p2,s…

JAVA---学生管理系统

遍历字符串 ArrayList学习&#xff1a;

git:码云仓库提交以及Spring项目创建

git&#xff1a;码云仓库提交 1 前言 码云访问稳定性优于github&#xff0c;首先准备好码云的账户&#xff1a; 官网下载GIT&#xff0c;打开git bash&#xff1a; 查看当前用户的所有GIT仓库&#xff0c;需要查看全局的配置信息&#xff0c;使用如下命令&#xff1a; git …

关于数据通信知识的补充——第二篇

目录 四.二层交换机 5.实现不同vlan通信的原理 方法一&#xff1a;路由器网关 方法二&#xff1a;单臂路由 方法三&#xff1a;三层交换机 五.三层路由技术 &#xff08;1&#xff09;直连路由 &#xff08;2&#xff09;静态路由 &#xff08;3&#xff09;动态路由 …

220平现代风装修设计亮点分享,福州·名城银河湾。福州中宅装饰,福州装修

福州名城银河湾&#xff0c;220平现代风装修案例分享&#xff0c;以下是对这些设计亮点的详细分析&#xff1a; ①客厅木饰面背景墙&#xff0c;搭配灰橙撞色皮质沙发 客厅的木饰面背景墙与撞色皮质沙发的搭配&#xff0c;不仅提供了温馨舒适的氛围&#xff0c;还为空间增添了…

2核4G云服务器能支持多少人同时访问?性能测评来了

腾讯云轻量2核4G5M带宽服务器支持多少人在线访问&#xff1f;5M带宽下载速度峰值可达640KB/秒&#xff0c;阿腾云以搭建网站为例&#xff0c;假设优化后平均大小为60KB&#xff0c;则5M带宽可支撑10个用户同时在1秒内打开网站&#xff0c;并发数为10&#xff0c;经阿腾云测试&a…

零知识玩转AVH(7)—— 门槛任务(2)所遇错误及解决(1)

接前一篇文章&#xff1a;零知识玩转AVH&#xff08;6&#xff09;—— 门槛任务&#xff08;1&#xff09;源码下载、编译及运行 上一回说到完成门槛任务 https://github.com/ArmDeveloperEcosystem/Paddle-examples-for-AVH &#xff08;推荐&#xff0c;内含 ML 视觉用例&am…

如何在Tomcat中的webapp中手动发布

这里写目录标题 首先进入Tomcat文件夹进入webaaps中,编写Java代码最后进入浏览器打开就看可以进入这个界面了 首先进入Tomcat文件夹 如图: 进入webaaps中, 编写Java代码 最后进入浏览器打开 就看可以进入这个界面了

【Spring Boot】创建你的第一个 Spring Boot 应用

创建你的第一个 Spring Boot 应用 1.环境配置2.步骤详解3.项目结构分析3.1 入口类 DemoApplication3.2 控制器 PathVariableController3.3 控制器 BasicController3.4 模型 User 4.运行 Spring Boot 目前已经成为了 Java 开发领域的框架范式。本篇博客&#xff0c;我将带领大家…

DataGrip 面试题及答案整理,最新面试题

DataGrip的数据库兼容性和多数据库支持如何实现&#xff1f; DataGrip实现数据库兼容性和多数据库支持的方式包括&#xff1a; 1、广泛的数据库支持&#xff1a; DataGrip支持多种数据库&#xff0c;包括但不限于MySQL, PostgreSQL, SQL Server, Oracle, SQLite, 和MongoDB&a…

前端学习之css选择器--基本选择器、关系选择器、属性选择器、复合选择器、伪类选择器

目录 基本选择器 结果 关系选择器 结果 父子关系 祖先后代关系 相邻兄弟关系 兄弟关系 ​编辑 属性选择器 结果 复合选择器 结果 伪类选择器 结果 伪类选择器-操作标签 结果 未访问 访问后 悬停 基本选择器 <!DOCTYPE html> <html lang"en"…

Java八股文(Git)

Java八股文のGit Git Git Git 是什么&#xff1f;它有什么作用&#xff1f; Git 是一种分布式版本控制系统&#xff0c;用于管理源代码的变更和追踪。 它可以记录文件更改的历史&#xff0c;帮助多人协同开发&#xff0c;并提供了回滚、分支管理等功能。 Git 和 SVN&#xff0…

数字电子技术实验(四)

单选题 1.组合逻辑电路中产生竞争冒险的原因是&#xff1f; A. 电路没有最简化 。 B. 时延 。 C. 电路有多个输出。 D. 逻辑门的类型不同。 答案&#xff1a;B 评语&#xff1a;10分 单选题 2.下列表达式不存在竞争冒险的有&#xff1f; 答案&#xff1a;A 评语&#x…

【b站咸虾米】2 Vue基础(下) 2021最新Vue从基础到实例高级_vue2_vuecli脚手架博客案例

课程地址&#xff1a;【2021最新Vue从基础到实例高级_vue2_vuecli脚手架博客案例】 https://www.bilibili.com/video/BV1pz4y1S7bC/?share_sourcecopy_web&vd_sourceb1cb921b73fe3808550eaf2224d1c155 目录 2 Vue基础 下 2.8 计算属性 2.8.1 计算属性使用 2.8.2 计算…

MySQL:视图

1. 概述 在MySQL中&#xff0c;视图&#xff08;View&#xff09;是一个虚拟存在的表&#xff0c;其内容是由查询定义的。视图本身并不包含数据&#xff0c;它只包含一条SQL查询语句&#xff08;即定义视图的SELECT语句&#xff09;。当通过视图访问数据时&#xff0c;MySQL会执…

zed2i相机驱动的安装(2)

安装完sdk和wrapper&#xff0c;启动时显示缺少标定文件&#xff0c;第一反应是运行自带的标定程序 但是此时运行ZED tools里的标定程序也会出问题 打开 On Linux : /usr/local/zed/settings/On Windows : C:\ProgramData\Stereolabs\settings 查看里面是否是空的&#xff…

c++算法学习笔记 (8) 树与图部分

1.树与图的存储 &#xff08;1&#xff09;邻接矩阵 &#xff08;2&#xff09;邻接表 // 链式前向星模板&#xff08;数组模拟&#xff09; #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N 100010, M …

官网链接怎么转二维码?扫码直接跳转官网的方法

随着互联网的不断发展&#xff0c;现在一般信息内容都是通过手机来获取的&#xff0c;所以现在通过手机访问官方网站获取内容也是一种很常见的方式&#xff0c;那么如何将官网网址生成二维码图片&#xff0c;用户能够通过扫码访问官方网站或者其他页面内容呢&#xff1f;下面分…

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

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