牛客_小白月赛_60

A

在这里插入图片描述
签到题

/*
 * @author :Changersh
 * @date : 2022/11/15
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    public static void main(String[] args) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int x = sc.nextInt();
        out.println((x - b) / a);
        out.close();
    }
    static class FastScanner{
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }
    
        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }
    
        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }
    
            return true;
        }
    
        public String next() {
            hasNext();
            return st.nextToken();
        }
    
        public int nextInt() {
            return Integer.parseInt(next());
        }
    
        public long nextLong() {
            return Long.parseLong(next());
        }
    
        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }
    
    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

B

在这里插入图片描述
分别统计每一个房间联通的出口个数,劫匪在某个房间的时候,该房间联通的出口都不能用了。
用出口的总数,减去这些出口即是可以逃跑的出口

/*
 * @author :Changersh
 * @date : 2022/11/15
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int N = 100005;
    public static void main(String[] args) {
        int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt();
        int[] st = new int[N];
        for (int i = 1; i <= n; i++) {
            int x = sc.nextInt();
            st[x]++;
        }
        while (q-- > 0) {
            int x = sc.nextInt();
            out.println(n - st[x]);
        }
        out.close();
    }11
    static class FastScanner{
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

C

在这里插入图片描述
经典dp

/*
 * @author :Changersh
 * @date : 2022/11/15
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int N = 4005, n, m;
    private static int[] st = new int[N];
    public static void main(String[] args) {
        n = sc.nextInt(); m = sc.nextInt();

        for (int i = 0; i < n; i++) {
            st[i] = sc.nextInt();
        }
        int[] dp = new int[N];
        dp[0] = st[0];
        for (int i = 1; i < n; i++) {
            if (i <= m) dp[i] = Math.max(dp[i - 1], st[i]);
            else dp[i] = Math.max(dp[i - 1], dp[i - m - 1] + st[i]);

        }
        out.println(dp[n - 1]);
        out.close();
    }
    static class FastScanner{
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}

D

在这里插入图片描述
在这里插入图片描述
因为相邻距离是1,求最短路就可以使用 bfs,就是很典型的bfs,只不过在走的时候,必须要经过一个符合条件的商店(游戏刺激度 > x)。
分别对起点和终点做一次bfs,遍历求 (刺激度 > x),的商店到二者的距离和,取最小值即可。

/*
 * @author :Changersh
 * @date : 2022/11/15
 */

import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
    private static int N = 4005, n, m, x, sx, ex, sy, ey, ans = 0;
    private static int[][] g = new int[N][N];
    private static int[][] s1 = new int[N][N], s2 = new int[N][N];
    private static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
    public static void main(String[] args) {
        n = sc.nextInt(); m = sc.nextInt(); x = sc.nextInt();
        sx = sc.nextInt(); sy = sc.nextInt(); ex = sc.nextInt(); ey = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                g[i][j] = sc.nextInt();
                ans = Math.max(g[i][j], ans);
            }
        }
        if (ans <= x) {
            out.println("-1");
        } else {
            ans = Integer.MAX_VALUE;
            for (int i = 0; i <= n; i++) {
                Arrays.fill(s1[i], -1);
                Arrays.fill(s2[i], -1);
            }
            bfs(s1, sx, sy);
            bfs(s2, ex, ey);
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= m; j++)  {
                    if (g[i][j] > x && s1[i][j] != -1 && s2[i][j] != -1) {
                        ans = Math.min(ans, s1[i][j] + s2[i][j]);
                    }
                }
            }
            out.println(ans == Integer.MAX_VALUE ? -1 : ans);
        }
        out.close();
    }

    private static void bfs(int[][] st, int a, int b) {
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{a, b});
        st[a][b] = 0;
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int x = t[0], y = t[1];
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx <= n && nx > 0 && ny <= m && ny > 0 && g[nx][ny] != -1 && st[nx][ny] == -1) {
                    q.add(new int[]{nx, ny});
                    st[nx][ny] = st[x][y] + 1;
                }
            }
        }
    }

    static class FastScanner{
        // sc.xxx;
        // out.print();
        // out.flush();
        // out.close();
        BufferedReader br;
        StringTokenizer st;
        public FastScanner(InputStream in) {
            br=new BufferedReader( new InputStreamReader(System.in));
            eat("");
        }
        public void eat(String s) {
            st=new StringTokenizer(s);
        }

        public String nextLine() {
            try {
                return br.readLine();
            }catch(IOException e) {
                return null;
            }
        }

        public boolean hasNext() {
            while(!st.hasMoreTokens()) {
                String s=nextLine();
                if(s==null)return false;
                eat(s);
            }

            return true;
        }

        public String next() {
            hasNext();
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }
    }

    static FastScanner sc=new FastScanner(System.in);
    static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值