描述

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 ;
}

最新文章

  1. RxAndroid+Retrofit+MVVM(1)OKHttp
  2. Alpha阶段第五次Scrum Meeting
  3. MySQL(五) MySQL中的索引详讲
  4. mybatis 3.x 缓存Cache的使用
  5. 无序数组a,求a[i]-a[j]的最大值,且i&lt;j
  6. 第三方登录过程—OAuth2.0协议
  7. 一个NB的安全认证机制
  8. UIVIew之霓虹灯实现
  9. 【转】深入理解Java内存模型(三)——顺序一致性
  10. Windows环境变量修改
  11. [core java学习笔记][第十章部署应用程序]
  12. Linux下的在线播放神器
  13. Effective C++:规定20: 宁pass-by-reference-to-const更换pass-by-value
  14. 快速构建Windows 8风格应用37-常见发布注意事项
  15. Android: Toolbar、AppBarLayout
  16. Java设计模式(六)Adapter适配器模式
  17. [ Java学习基础 ] 浅析Java方法调用
  18. 基于GraphCuts图割算法的图像分割----OpenCV代码与实现
  19. flink Standalone Cluster
  20. C# 反射,动态类,动态方法

热门文章

  1. 关于FileFOutputStream应用中的FileNotFoundException问题
  2. XML再深入
  3. [JAVA小项目]GUI界面的局域网聊天室
  4. 给HTML拍个照(如何将html元素转成图片)
  5. em和px
  6. JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
  7. SELECT * FROM pet WHERE name REGEXP &#39;w&#39;;
  8. dos.ORM配置和使用
  9. 设计模式之装饰模式(Decorator)
  10. 高效实时的网络会议数据传输库—UDT