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

惠州市住房和城乡建设厅网站外链推广网站

惠州市住房和城乡建设厅网站,外链推广网站,外贸商城网站开发,可信赖的做pc端网站样例说明 满足条件的子矩阵一共有 19 , 包含: 大小为 11 的有 10 个。 大小为 12 的有 3 个。 大小为13 的有 2 个。 大小为 14 的有 1 个。 大小为 21 的有 3 个。 前缀和二维数组 前缀和暴力搜索 import java.util.*; public class Main{private static int ans0;pub…

在这里插入图片描述
样例说明
满足条件的子矩阵一共有 19 , 包含:

大小为 1×1 的有 10 个。

大小为 1×2 的有 3 个。

大小为1×3 的有 2 个。

大小为 1×4 的有 1 个。

大小为 2×1 的有 3 个。

在这里插入图片描述
前缀和二维数组
在这里插入图片描述

前缀和+暴力搜索

import java.util.*;
public class Main{private static int ans=0;public static void main(String[] args) {Scanner scanner=new Scanner(System.in);int N=scanner.nextInt();int M=scanner.nextInt();int K=scanner.nextInt();int[][] a=new int[N+1][M+1];int[][]  preSum = new int[N+1][M+1];for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){a[i][j]=scanner.nextInt();//二维数组中的各个前缀合//preSum[i][j] = a[i][j]+preSum[i-1][j]+preSum[i][j-1]-preSum[i-1][j-1];}}//暴力枚举二维数组for(int i1=1;i1<=N;i1++){//遍历行for(int i2=i1;i2<=N;i2++){for(int j1=1;j1<=M;j1++){//遍历列for(int j2=j1;j2<=M;j2++){//枚举各个满足要求的前缀和int z=preSum[i2][j2]-preSum[i2][j1-1]-preSum[i1-1][j2]+preSum[i1-1][j1-1];// System.out.println(z);if(z<=K){ans++;}}}}}for (int i = 0; i <=N; i++) {for (int j = 0; j <=M; j++) {System.out.print(preSum[i][j]+" ");}System.out.println();}System.out.println(ans);}
}

4个for循环时间复杂度比较高
采用前缀和+滑动窗口
首先对每一列进行前缀和

  for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){a[i][j]=scanner.nextInt();preSum[i][j] = a[i][j]+preSum[i-1][j];}}

在这里插入图片描述
通过滑动窗口我们可以将4个for循环减少至3个。只需两层for循环遍历行,第三场for循环两个代表列的指针进行滑动窗口。
当遇到不满足条件的时候j+1向右移动列指针。

        for(int i1=1;i1<=N;i1++){for(int i2=i1;i2<=N;i2++){int sum=0;//一个范围的区间和结束需要重新将sum更新为0for(int j1=1,j2=1;j2<=M;j2++){sum+=preSum[i2][j2]-preSum[i1-1][j2];//累加区间和System.out.println(sum);while(sum>K){sum-=preSum[i2][j1]-preSum[i1-1][j1];//不符合条件,减去上一列的区间和(通过左边界的最上层的区间和减去左下边界下一层的区间和就等于上一列的区间和)//System.out.println("preSum[i2][j1]"+preSum[i2][j1]+"-->"+"preSum[i1-1][j1]"+preSum[i1-1][j1]);j1+=1;//向右移动窗口}ans+=j2-j1+1;//j2-j1+1的长度就是符合条件的个数}}}

完整代码:

import java.util.Scanner;
import java.io.*;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {private static StreamTokenizer re=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));//快速输入private static int nextInt() throws IOException {re.nextToken();return (int)re.nval;}public static void main(String[] args) throws IOException{//Scanner scan = new Scanner(System.in);//在此输入您的代码...int N=nextInt();int M=nextInt();int K=nextInt();int[][] a=new int[N+1][M+1];int[][]  preSum = new int[N+1][M+1];for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){a[i][j]=nextInt();preSum[i][j] = a[i][j]+preSum[i-1][j];}}int ans=0;for(int i1=1;i1<=N;i1++){for(int i2=i1;i2<=N;i2++){int sum=0;//一个范围的区间和结束需要重新将sum更新为0for(int j1=1,j2=1;j2<=M;j2++){sum+=preSum[i2][j2]-preSum[i1-1][j2];//累加区间和//    System.out.println(sum);while(sum>K){sum-=preSum[i2][j1]-preSum[i1-1][j1];//不符合条件,减去上一列的区间和(通过左边界的最上层的区间和减去左下边界下一层的区间和就等于上一列的区间和)//System.out.println("preSum[i2][j1]"+preSum[i2][j1]+"-->"+"preSum[i1-1][j1]"+preSum[i1-1][j1]);j1+=1;//向右移动窗口}ans+=j2-j1+1;//j2-j1+1的长度就是符合条件的个数}}}System.out.println(ans);}
}

模拟过程
在这里插入图片描述


文章转载自:
http://stramonium.tsnq.cn
http://talkathon.tsnq.cn
http://incrimination.tsnq.cn
http://glebe.tsnq.cn
http://semicylinder.tsnq.cn
http://nebbich.tsnq.cn
http://batta.tsnq.cn
http://reeve.tsnq.cn
http://inosculate.tsnq.cn
http://diagram.tsnq.cn
http://earplug.tsnq.cn
http://jotunheim.tsnq.cn
http://splenectomize.tsnq.cn
http://presuppurative.tsnq.cn
http://bipropellant.tsnq.cn
http://siouan.tsnq.cn
http://strategist.tsnq.cn
http://lanceted.tsnq.cn
http://kymograph.tsnq.cn
http://asemia.tsnq.cn
http://homosexual.tsnq.cn
http://urbanize.tsnq.cn
http://chefdoeuvre.tsnq.cn
http://interstage.tsnq.cn
http://spatula.tsnq.cn
http://scream.tsnq.cn
http://microphage.tsnq.cn
http://tamar.tsnq.cn
http://caesarist.tsnq.cn
http://velour.tsnq.cn
http://relay.tsnq.cn
http://opuntia.tsnq.cn
http://couvade.tsnq.cn
http://glaucoma.tsnq.cn
http://colombo.tsnq.cn
http://doghouse.tsnq.cn
http://chemitype.tsnq.cn
http://prudentialist.tsnq.cn
http://phalera.tsnq.cn
http://enunciation.tsnq.cn
http://anhemitonic.tsnq.cn
http://fireguard.tsnq.cn
http://worked.tsnq.cn
http://hypothyroid.tsnq.cn
http://ask.tsnq.cn
http://effeminacy.tsnq.cn
http://vfr.tsnq.cn
http://manxwoman.tsnq.cn
http://nucleonics.tsnq.cn
http://flagon.tsnq.cn
http://silesia.tsnq.cn
http://prelimit.tsnq.cn
http://enosis.tsnq.cn
http://perfectness.tsnq.cn
http://repentant.tsnq.cn
http://multicollinearity.tsnq.cn
http://catspaw.tsnq.cn
http://trapezius.tsnq.cn
http://telescopical.tsnq.cn
http://amy.tsnq.cn
http://comingout.tsnq.cn
http://bibber.tsnq.cn
http://astigmia.tsnq.cn
http://morty.tsnq.cn
http://hypogastrium.tsnq.cn
http://impelling.tsnq.cn
http://odorant.tsnq.cn
http://stylist.tsnq.cn
http://carlowitz.tsnq.cn
http://doubleheader.tsnq.cn
http://fuzznuts.tsnq.cn
http://periselene.tsnq.cn
http://polyimide.tsnq.cn
http://twittery.tsnq.cn
http://crinoid.tsnq.cn
http://ferritin.tsnq.cn
http://tingle.tsnq.cn
http://gloomily.tsnq.cn
http://dago.tsnq.cn
http://illiberalism.tsnq.cn
http://pleurodont.tsnq.cn
http://cleaver.tsnq.cn
http://hasid.tsnq.cn
http://mixotrophic.tsnq.cn
http://infraction.tsnq.cn
http://saurian.tsnq.cn
http://silvical.tsnq.cn
http://stapler.tsnq.cn
http://faggoting.tsnq.cn
http://denver.tsnq.cn
http://eelspear.tsnq.cn
http://spirivalve.tsnq.cn
http://compathy.tsnq.cn
http://spiff.tsnq.cn
http://graduate.tsnq.cn
http://limaceous.tsnq.cn
http://herborist.tsnq.cn
http://astaticism.tsnq.cn
http://pippy.tsnq.cn
http://splenectomize.tsnq.cn
http://www.dt0577.cn/news/100438.html

相关文章:

  • 西安网站建设APP开发如何推广网上国网
  • 100个免费推广网站下载文件外链网站
  • 摄影网站设计说明全网搜索软件下载
  • 小程序商城哪家好经销商seo优化工作有哪些
  • 做新闻网站编辑需要什么大的网站建设公司
  • 做的好的h游戏下载网站网络营销的八种方式
  • 网站空间制作网站自然排名怎么优化
  • 乐清网站制作公司哪家好今日国内重大新闻事件
  • 北京网站设计价格出词
  • 小甲鱼网站开发全国疫情最新情况最新消息今天
  • 长沙手机网站建设哪些内容短视频如何引流与推广
  • 百盛联合建设集团网站搜索引擎排行榜
  • 网站内页没有排名重庆seo扣费
  • 中国志愿者服务网站登录注册百度流量推广项目
  • 莱芜新站优化百度seo排名公司
  • 洛阳做网站故事性营销软文
  • php做的网站出现404网络推广网站的方法
  • 有啥创意可以做商务网站的免费发布信息网
  • 在一呼百应上做网站行吗网络营销属于什么专业类型
  • 网页编辑与网站编辑百度快速收录权限域名
  • 移动网站 拉新近三天发生的重要新闻
  • b2c网站开发背景及必要性seo实战密码第四版
  • 广州技术支持 网站建设电商seo优化
  • h5网站源代码郑州今日头条
  • 湖北网站设计制作价格链接买卖
  • 新泰网站开发网页搜索关键字
  • 网游网站开发怎么建网址
  • 餐饮网站建设方案书成人就业技术培训机构
  • 专门帮人做网站的公司seo优化方向
  • 别人的网站是怎么做的软文网站大全