描述

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. scss-@mixin传参

    混合器一个很重要特性就是可以传递参数,可以根据不同场景来定制css代码的复用.极大提高了混合器的适用性,看如下scss代码实例: @mixin makeradius($radius) { border ...

  2. 打杂程序员之nginx服务配置

    现在公司要在服务器上多加个网站用同一个nginx服务器,而且都是公用80端口. 因为服务器上跑着好几个网站了,所以配置文件配置完成时候要检测一下能不能用,用nginx -t:最好不要直接杀死nginx ...

  3. Java使用POI操作Excel文件

    1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...

  4. Windows8中如何打包和安装一个本地的Metro类型应用(转)

    Windows8中如何打包和安装一个本地的Metro类型应用 (转自:http://software.intel.com/zh-cn/blogs/2012/05/09/windows8metro) 微 ...

  5. OpenSUSE 内核编译教程 (kernel 2.6.x)

    http://cn.opensuse.org/OpenSUSE_%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91%E6%95%99%E7%A8%8B_(kernel_2.6.x ...

  6. [C# 网络编程系列]专题八:P2P编程

    引言: 前面的介绍专题中有朋友向我留言说介绍下关于P2P相关的内容的,首先本人对于C#网络编程也不是什么大牛,因为能力的关系,也只能把自己的一些学习过程和自己的一些学习过程中的理解和大家分享下的,下面 ...

  7. VSCode cpptools 插件在Centos 7下不能正确显示符号列表的解决办法

    vscode 的插件cpptools 0.9.3 需要glibc 2.18的版本,但是Centos 7 下没有这个版本的GLIBC,所以导致链接库丢失,后台服务不能正常运行.按以下步骤操作可修复此问题 ...

  8. C#转Java之路之二:多线程原子变量

    多线程操作会带来不一致性,为了实现一直性.我们可以用关键字:synchronized同步对象或者volatile轻量级.内存可见性. 两个关键字使用对比: 1.synchronized比较重,属于悲观 ...

  9. ABAP宏的调试

    我们都知道高级语言宏一般是无法调试的.但是ABAP的宏例外. 比如我写了下面一段宏,名为insert_table. 执行这段代码,调试器会在第23行停下来. ABAP调试器里有个工具可以用于宏的调试, ...

  10. python入门13 集合set

    set集合与数学中的集合同一个概念,是无序不重复元素组成的. #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanh ...