蓝桥杯算法记录

算法记录

因为最近快到蓝桥杯了 所以跟练了几道题 今天就来记录一下

信号覆盖

信号覆盖

解题思路:这题将输入的x,y的进行储存 然后将每个点用check方法进行检测

java 复制代码
int t1=x-X[i];
 int t2=y-Y[i];
 //用这个进行判断
 t1*t1+t2*t2<=r*r

答案

java 复制代码
import java.util.Scanner;

// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {

  static int[] X = new int[101];
  static int[] Y = new int[101];
  static int n = 0;
  static int r = 0;

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int w = scan.nextInt();
    int h = scan.nextInt();
    n = scan.nextInt();
    r = scan.nextInt();
    for (int i = 0; i < n; i++) {
      X[i] = scan.nextInt();
      Y[i] = scan.nextInt();
    }
    scan.close();
    int res = 0;
    for (int i = 0; i <= w; i++) {
      for (int j = 0; j <= h; j++) {
        if (check(i, j)) {
          res++;
        }
      }
    }
    System.out.println(res);
  }

  public static boolean check(int x, int y) {
    for (int i = 0; i < n; i++) {
      int t1 = x - X[i];
      int t2 = y - Y[i];
      if (t1 * t1 + t2 * t2 <= r * r) {
        return true;
      }
    }
    return false;
  }
}

并查集

幼儿园

第一个是初始化

将数组初始化下标为本身

java 复制代码
  for(int i=1;i<arr.length;i++){
              arr[i]=i;
            }

合并 如果他俩不是一个父节点再将

java 复制代码
public static void merge(int x,int y){
          int prex=findRoot(x);
          int prey=findRoot(y);
          if(prex!=prey){
            arr[prey]=prex;
          }

查找

传入查找的数字 不然继续往下查找

java 复制代码
 public static int findRoot(int key){
          if(key==arr[key]){
            return key;
          }
          return arr[key]=findRoot(arr[key]);
        }
java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    private static int[] arr = null;
    public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int n=scan.nextInt();
            int m=scan.nextInt();
            arr=new int[n+1];
            for(int i=1;i<arr.length;i++){
              arr[i]=i;
            }
            while(m-->0){
              int op=scan.nextInt();
              int x=scan.nextInt();
              int y=scan.nextInt();
              if(op==1){
                merge(x,y);
              }else{
                int x1=findRoot(x);
                int y1=findRoot(y);
                if(x1!=y1){
                  System.out.println("NO");
                }else{
                  System.out.println("YES");
                }
              }
            }
            scan.close();
        }
        public static int findRoot(int key){
          if(key==arr[key]){
            return key;
          }
          return arr[key]=findRoot(arr[key]);
        }
        public static void merge(int x,int y){
          int prex=findRoot(x);
          int prey=findRoot(y);
          if(prex!=prey){
            arr[prey]=prex;
          }
        }
}

简单的动态规划

台阶问题

答案:

动态规划是这次的状态由前几次状态或者上次状态推导出来的

阶 走法

1 1

2 2

3 4

4 7

5 13

看出 f(n) = f(n-1) + f(n-2) + f(n-3)

然后用这个列方程就行

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt();
        int []dp=new int[n+1];
        if(n<=0){
         System.out.println(0);
         return;
        }
        if(n<=2){
         System.out.println(n);
         return;
        };
        if(n==3){
         System.out.println(4);
         return;
        }
        dp[0]=0;
        dp[1]=1;
        dp[2]=2;
        dp[3]=4;
        for(int i=4;i<=n;i++){
          dp[i]=dp[i-1]+dp[i-2]+dp[i-3];
        }
        System.out.println(dp[n]);
        scan.close();
    }
}

二分求区间最大值的最小值

跳石头

这题思路是在起点和终点之间不断地寻找缩小范围 如果当前每次记录下来的的值进入 check方法进行判断

如果当前的距离小于最小值搬去不做影响 对搬去的数字++ 否则就从此处开始计算后续的有没有距离更小的

java 复制代码
 static boolean check(int d){
      int cnt=0,pos=0;
      for(int i=0;i<n;i++){
        if(arr[i]-pos<d){
          cnt++;
        }else{
          pos=arr[i];
        }
      }
      if(cnt<=m){
        return true;
      }
      return false;
    }

答案

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
   static int l,n,m;
    static int[] arr;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        l = scan.nextInt();
        n = scan.nextInt();
        m = scan.nextInt();
        arr = new int[n];
        for(int i=0;i<n;i++) {
            arr[i] = scan.nextInt();
        }
        int left=0,right=l;
        int mid,result=0;
        while(left<=right){
          mid=(left+right)>>1;
          if(check(mid)){
            result=mid;
            left=mid+1;
          }else{
            right=mid-1;
          }
        }
         System.out.println(result);
        scan.close();
    }
    static boolean check(int d){
      int cnt=0,pos=0;
      for(int i=0;i<n;i++){
        if(arr[i]-pos<d){
          cnt++;
        }else{
          pos=arr[i];
        }
      }
      if(cnt<=m){
        return true;
      }
      return false;
    }
}

总结

直播挺难写的 算法也挺难写的 感觉算法基础还是不行 但是面试也要问 还是每天多写点吧

相关推荐
重生之我要进大厂6 分钟前
LeetCode 876
java·开发语言·数据结构·算法·leetcode
_祝你今天愉快10 分钟前
技术成神之路:设计模式(十四)享元模式
java·设计模式
KBDYD101041 分钟前
C语言--结构体变量和数组的定义、初始化、赋值
c语言·开发语言·数据结构·算法
小筱在线1 小时前
SpringCloud微服务实现服务熔断的实践指南
java·spring cloud·微服务
luoluoal1 小时前
java项目之基于Spring Boot智能无人仓库管理源码(springboot+vue)
java·vue.js·spring boot
ChinaRainbowSea1 小时前
十三,Spring Boot 中注入 Servlet,Filter,Listener
java·spring boot·spring·servlet·web
Crossoads1 小时前
【数据结构】排序算法---桶排序
c语言·开发语言·数据结构·算法·排序算法
小游鱼KF1 小时前
Spring学习前置知识
java·学习·spring
扎克begod1 小时前
JAVA并发编程系列(9)CyclicBarrier循环屏障原理分析
java·开发语言·python
青灯文案11 小时前
SpringBoot 项目统一 API 响应结果封装示例
java·spring boot·后端