【TOJ 4475】The Coolest Sub-matrix(对角线前缀和)
描述
Given an N*N matrix, find the coolest square sub-matrix.
We define the cool value of the square matrix as X-Y where X indicating the sum of all integers of the main diagonal and Y indicating the sum of the other diagonal.
输入
The first line has a positive integer N (2 ≤ N ≤ 400), the size of the matrix.
The following N lines each contain N integers in the range [-1000, 1000], the elements of the matrix.
输出
Output the coolest value of a square sub-matrix.
样例输入
2
1 -2
4 5
样例输出
4
题意:
在n*n的矩阵中找到一个子矩阵,使得其 (正对角线整数之和-反对角线整数之和) 最大。
思路:
记录矩阵中各对角线的前缀和,将所取对角线两端的前缀和相减,所得即为该对角线整数之和。
#include<bits/stdc++.h>
#define MAX 405
using namespace std;
int A1[MAX][MAX],A2[MAX][MAX];//A1正对角线前缀和 与 A2反对角线前缀和
int main()
{
int i,j,k,n,x,maxx=-;
cin>>n;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&x);
A1[i][j]=A1[i-][j-]+x;//左上加到右下
A2[i][j]=A2[i-][j+]+x;//右上加到左下
}
}
for(i=;i<=n;i++) //起始行位置i
for(j=;j<=n;j++)//起始列位置j
for(k=;k<=min(n+-i,n+-j);k++)//小矩阵边长k
maxx=max(maxx,A1[i+k][j+k]-A1[i-][j-]-(A2[i+k][j]-A2[i-][j+k+]));
cout<<maxx<<endl;
return ;
}
【TOJ 4475】The Coolest Sub-matrix(对角线前缀和)的更多相关文章
- TOJ 4475: The Coolest Sub-matrix
4475: The Coolest Sub-matrix Time Limit(Common/Java):4000MS/12000MS Memory Limit:65536KByteTota ...
- [算法]Min_25筛
前言 本篇文章中使用的字母\(p\),指\(\text{任意的} p \in \text{素数集合}\) 应用场景 若函数\(f(x)\)满足, \(f(x)\)是积性函数 \(f(p)\)可以使用多 ...
- CodeForces 263E Rhombus
洛谷题目页面传送门 & CodeForces题目页面传送门 给定一个$n$行$m$列的矩阵,第$i$行$j$列为$a_{i,j}$,以及一个常数$s\in\left[1,\left\lceil ...
- leetcode-766-Toeplitz Matrix(每一条对角线元素的比较)
题目描述: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- hdu多校第4场E. Matrix from Arrays HDU 二维前缀和
Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...
- HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)
题意: for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i - j] = A[cursor]; cursor = (cu ...
随机推荐
- Linux进程优先级和nice值
在学习了linux的完全公平调度算法(CFS)后,记录下学习轨迹 这篇文章主要讲述,完全公平调度算法的工作方式,和一些调度知识 我们可以通过ps -l看到当前正在运行的进程的详细信息其中 F:表示进程 ...
- Linux的find命令实例详解和mtime ctime atime
这次解释一下三个Linux文件显示的三个时间,然后展示一下find命令的各个功能 在linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime mo ...
- JSON初试
本次在课堂上老师教我使用JSON. 本次的教学网站是:https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON?td ...
- scss-嵌套属性
使用scss不但可以实现选择器的嵌套,属性也可以进行嵌套. 首先看一段代码实例: nav { border-style: solid; border-width: 1px; border-color: ...
- sass文件处理
sass注释方式有两种: 1.标准的css注释/**/: 2.//双斜杠形式的单行注释(不会被转译): 标准的css注释 双斜杆单行注释 sass文件后缀名有两种: 1.后缀名为sass,不适用用大括 ...
- js CheckBox 全选、反选
<h3>你最喜欢的水果是?</h3> <label><input type="checkbox"/>苹果</label> ...
- 前端自动化构建工具Grunt
一.了解Gurnt(http://www.open-open.com/lib/view/open1433898272036.html) Grunt 是一个基于任务的JavaScript工程命令行构建工 ...
- Shader之性能优化
1.像素>>顶点数>>物体个数:shader中的计算应首先考虑放在script,其次vert,最后frag中 2.尽量用精度小的类型替换精度大的类型(特别是在frag中,要尽可 ...
- Oracle 数据库异机恢复(归档模式)
操作必须在服务器上进行(在192.168.12.71上执行) 0.按需要恢复的日期,把距恢复日期最近的全部备份和全部备份日期后的日志备份一同拷贝到需要恢复的机器上: 1.用RMAN登陆 C:\User ...
- C#设计模式之代理模式(一)
原文地址:http://blog.csdn.net/lovelion/article/details/8227953 代理模式是常用的结构型设计模式之一,当无法直接访问某个对象或访问某个对象存在困难时 ...