ccf 202104-2 邻域均值

暴力解法

import java.util.Scanner;

public class NeighborAvgOld {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int L = sc.nextInt();
        int r = sc.nextInt();
        int t = sc.nextInt();
        sc.nextLine();
        int[][] arr = new int[n][n];
        int[][] sum_arr = new int[n+1][n+1];
        for(int i = 0; i < n; i++){
            String line = sc.nextLine();
            String[] str_arr = line.split(" ");
            for(int j = 0; j < n; j++){
                arr[i][j] = Integer.parseInt(str_arr[j]);
                sum_arr[i+1][j+1] = sum_arr[i][j+1] + sum_arr[i+1][j]
                        - sum_arr[i][j] + arr[i][j];
            }
        }
        int total_count = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                int tmp_sum = 0;
                int count = 0;
                for(int a = i-r; a <= i+r; a++){
                    for(int b = j-r; b <= j+r; b++){
                        if(a >= 0 && a < n && b >= 0 && b < n){
                            tmp_sum+=arr[a][b];
                            count++;
                        }
                    }
                }
                if(count!=0&&tmp_sum<=t*count){
                    total_count++;
                }
            }
        }

        System.out.println(total_count);

    }
}

二维前缀和

原理参考:

https://blog.csdn.net/m0_65998513/article/details/132534198
import java.util.Scanner;

public class NeighborAvg {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int L = sc.nextInt();
        int r = sc.nextInt();
        int t = sc.nextInt();
        sc.nextLine();
        int[][] arr = new int[n][n];
        int[][] sum_arr = new int[n+1][n+1];
        for(int i = 0; i < n; i++){
            String line = sc.nextLine();
            String[] str_arr = line.split(" ");
            for(int j = 0; j < n; j++){
                arr[i][j] = Integer.parseInt(str_arr[j]);
                sum_arr[i+1][j+1] = sum_arr[i][j+1] + sum_arr[i+1][j]
                        - sum_arr[i][j] + arr[i][j];
            }
        }
        int total_count = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                int X1 = i-r >= 1 ? i-r : 1;
                int Y1 = j-r >= 1 ? j-r : 1;
                int X2 = i+r <= n ? i+r : n;
                int Y2 = j+r <= n ? j+r : n;
                int p = sum_arr[X2][Y2] - sum_arr[X2][Y1 -1] - sum_arr[X1-1][Y2] + sum_arr[X1-1][Y1 -1];
                if(p <= (X2- X1 + 1)*(Y2 - Y1 + 1)* t) total_count++;
            }
        }

        System.out.println(total_count);

    }
}

相关推荐

  1. ccf 202104-2 均值

    2024-04-21 09:48:06       17 阅读
  2. 【CSP试题回顾】202104-2-均值(优化)

    2024-04-21 09:48:06       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-21 09:48:06       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-21 09:48:06       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-21 09:48:06       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-21 09:48:06       20 阅读

热门阅读

  1. 设计模式:状态模式示例

    2024-04-21 09:48:06       13 阅读
  2. C语言makefile语法

    2024-04-21 09:48:06       12 阅读
  3. Spring Cloud 面试题(三)

    2024-04-21 09:48:06       15 阅读
  4. 5G网络架构;6G网络架构

    2024-04-21 09:48:06       12 阅读
  5. springboot整合mybatis-plus

    2024-04-21 09:48:06       14 阅读