描述

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. 2018-12-20 第二章Java 预习作业

    一.什么是变量?如何使用变量? 答:变量是在程序运行中其值可以改变的量,它是Java程序的一个基本存储单元. 变量的语法格式如下: [访问修饰符]变量类型 变量名[=初始值]: 二.java中基本数据 ...

  2. vue——组件

    一.组件概念 vue的核心基础就是组件的使用,玩好了组件才能将前面学的基础更好的运用起来.组件的使用更使我们的项目解耦合.更加符合vue的设计思想MVVM. // 定义一个名为 button-coun ...

  3. 采用C/C++语言如何实现复数抽象数据类型Complex

    记录一下! 采用C/C++语言如何实现复数抽象数据类型Complex #include <stdio.h> typedef struct Complex { double e1; // 实 ...

  4. .NET开源工作流RoadFlow-表单设计-HTML编辑器

    roadflow目前采用的html编辑器为ueditor编辑器(和表单设计器的编辑器相同). 绑定字段:与数据表的某个字段对应. 宽度:编辑器的宽度. 高度:编辑器的高度. 运行效果如下:

  5. Linux漏洞分析入门笔记-CVE-2015-0235

    Ubuntu 12.04 32位 ida 7.0 0x00:漏洞描述 1.glibc的__nss_hostname_digits_dots存在缓冲区溢出漏洞,导致使用gethostbyname系列函数 ...

  6. python 后台运行命令

    nohup python a.py  > a.log 2>&1 & 在窗口中单开虚拟session: tmux new -s "name" 推出虚拟窗口 ...

  7. 转:C函数调用理解

    1 空函数 int main() { 00411360 push ebp ;压入ebp 00411361 mov ebp,esp ;ebp = esp,保留esp,待函数调用完再恢复,因为函数调用中肯 ...

  8. python字符串反转 高阶函数 @property与sorted(八)

    (1)字符串反转 1倒序输出 s = 'abcde' print(s[::-1]) #输出: 'edcba' 2 列表reverse()操作 s = 'abcde' lt = list(s) lt.r ...

  9. 免费的SSL证书,你值得拥有!Let's Encrypt 试用体验记录

    早上收到 Let’s Encrypt 的邮件,说偶之前申请的已经通过了,于是马上开始试用.Let’s Encrypt 是一个新的数字证书认证机构,它通过自动化的过程消除创建和安装证书的复杂性,为网站提 ...

  10. GitLab 数据自动备份

    gitlab自动备份 周期性计划任务: crontab -e * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create service crond ...