当前位置: 首页 > news >正文

【CSP:202104-2】邻域均值(Java)

题目链接

  • 202104-2 邻域均值

题目描述

描述
1
格式
1
2

求解思路

  • 二维前缀和:用s数组记录下二维数组a的前缀和。在进行查询时直接利用前缀和求平均值,可以降低时间复杂度。
    示意

  • xl表示邻域的左边界,xr表示邻域的右边界,yt表示邻域的上边界,yb表示邻域的下边界。
    2

  • 为什么要用double类型进行比较?因为涉及除法,如果用整形的话可能会出现误差。

实现代码

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int L = in.nextInt();int r = in.nextInt();double t = in.nextDouble();int[][] a = new int[n + 1][n + 1];int[][] s = new int[n + 1][n + 1];for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {a[i][j] = in.nextInt();s[i][j] = s[i - 1][j] + s[i][j - 1] + a[i][j] - s[i - 1][j - 1];}}int cnt = 0;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {int xl = Math.max(j - r, 1);int xr = Math.min(j + r, n);int yt = Math.max(i - r, 1);int yb = Math.min(i + r, n);double avg = (double) (s[yb][xr] - s[yb][xl - 1] - s[yt - 1][xr] + s[yt - 1][xl - 1]) /((yb - yt + 1) * (xr - xl + 1));if (avg <= t) {cnt ++;}}}System.out.println(cnt);}
}

http://www.mrgr.cn/news/16428.html

相关文章:

  • 【Python系列】SQLAlchemy 基本介绍
  • SPR系列单点激光雷达测距传感器|模组之CAN-OPEN软件调试说明
  • ​字​节​一​面​
  • Vue.js入门系列(十九):深入理解和应用组件自定义事件
  • [ABC133A] T or T
  • 23. 如何使用Collections.synchronizedList()方法来创建线程安全的集合?有哪些注意事项?
  • 设计模式之外观模式
  • 今日(2024年8月30日)科技新闻(本周)
  • 【Rust】——高级类型
  • win10多个wifi快速切换脚本
  • 15年让爱轮回
  • 黑神话悟空-提高画质、防卡顿、修复等各种功能、各种CT表、各种存档、武器包、人物、装备替换等185+MOD合集
  • “添加”业务功能开发
  • 通过python 操作mysql 脚本
  • “品牌VS套路:华为、格行、中兴随身WiFi谁才是真良心?“
  • VMware中CentOS虚拟机配置网络(Net模式)
  • git commit添加emoji表情
  • 从零开始自学Python-之-常用库篇(十四)python的异步编程库asyncio
  • Tampermonkey 安装
  • Call openai-node in the backend or call https in the frontend?