描述

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(对角线前缀和)的更多相关文章

  1. TOJ 4475: The Coolest Sub-matrix

    4475: The Coolest Sub-matrix  Time Limit(Common/Java):4000MS/12000MS     Memory Limit:65536KByteTota ...

  2. [算法]Min_25筛

    前言 本篇文章中使用的字母\(p\),指\(\text{任意的} p \in \text{素数集合}\) 应用场景 若函数\(f(x)\)满足, \(f(x)\)是积性函数 \(f(p)\)可以使用多 ...

  3. CodeForces 263E Rhombus

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定一个$n$行$m$列的矩阵,第$i$行$j$列为$a_{i,j}$,以及一个常数$s\in\left[1,\left\lceil ...

  4. leetcode-766-Toeplitz Matrix(每一条对角线元素的比较)

    题目描述: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...

  5. 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 ...

  6. HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)

    6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...

  7. 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  8. hdu多校第4场E. Matrix from Arrays HDU 二维前缀和

    Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...

  9. 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 ...

随机推荐

  1. 在 Eclipse Juno 上安装 Marketplace

    Select Help/Install new software... from the menu, select the Juno update site (http://download.ecli ...

  2. html跨域获取数据

    a.com下的a.html,需要嵌入b.com下的b.html.这时建一个静态页面c.html将c.html放到a.com服务器中.b.html在嵌入c.html.这样,将参数值传输到c.html中, ...

  3. GridCellChoiceEditor

    choice_editor = wx.grid.GridCellChoiceEditor(choices_list, True) grid.SetCellEditor(row, col, choice ...

  4. 【Leetcode】【Easy】Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  5. laravel5.4学习笔记

    1.安装laravel可以直接用composer安装,然后用laravel new xxx来新建项目 服务器上安装了composer(php包管理工具)以后, composer global requ ...

  6. 织梦Dedecms主要文件夹目录及模板文件说明

    虽然织梦DedeCMS因为安全问题被人所诟病,但瑕不掩瑜,无论从用户群数量还是时间等各方面,织梦DedeCMS都是国内排名前几的CMS建站程序.如果你想学习CMS的二次开发,织梦DedeCMS是必须需 ...

  7. python入门19 异常及异常处理 异常捕获

    常见异常举例 """ 一些异常举例:""" '''模块不存在 ModuleNotFoundError: No module named 'd ...

  8. (转)写的非常好的一篇HTTP协议详解

    原文地址:http://www.cnblogs.com/TankXiao/archive/2012/02/13/2342672.html#3931004(推荐看原文,因为原文文章末有更详细链接) 当今 ...

  9. iOS UI(布局)约束是什么?view1.attr1 = view2.attr2 * multiplier + constant

    /* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * mul ...

  10. Scrapy研究探索(三)——Scrapy核心架构与代码执行分析

    学习曲线总是这样,简单样例"浅尝".在从理论+实践慢慢攻破.理论永远是基础,切记"勿在浮沙筑高台". 一. 核心架构 关于核心架构.在官方文档中阐述的非常清晰, ...