集训第五周动态规划 F题 最大子矩阵和
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
Output
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
Sample Output
15 这道题其实就是求最大子段和,需要把原题数据变化一下,例如
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
这个矩阵我选择的是
9 2
-4 1
-1 8
那么我还可以把这个选择过程看待为求数组 4 11 -10 1 的最大子段和,很显然是选择4 11,答案为15
那么4 11 -10 1是怎么来的呢,是我把2 3 4行数组组合成一个数组得来的
那么这道题的解法就出来了,不断枚举行区间,得到一个新数组,然后求最大子段和
#include"iostream"
#include"cstring"
using namespace std;
const int maxn=;
int b[maxn],a[maxn][maxn];
int n; void Init()
{
int t;
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>t;
a[i][j]=a[i-][j]+t;
}
}
} int main()
{
while(cin>>n)
{
Init();
int sum,ans,temp;
sum=;
ans=-;
int c=;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
{
sum=;
for(int k=;k<=n;k++)
{
temp=a[j][k]-a[i-][k];
sum+=temp;
if(sum>ans)
{
ans=sum;
}
if(sum<)
{
sum=;
}
}
}
cout<<ans<<endl;
}
return ;
}
集训第五周动态规划 F题 最大子矩阵和的更多相关文章
- 集训第五周 动态规划 B题LIS
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Des ...
- 集训第五周动态规划 H题 回文串统计
Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A ...
- 集训第五周动态规划 D题 LCS
Description In a few months the European Currency Union will become a reality. However, to join the ...
- 集训第五周动态规划 I题 记忆化搜索
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- 集训第五周动态规划 G题 回文串
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- 集训第五周动态规划 C题 编辑距离
Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...
- 集训第五周 动态规划 K题 背包
K - 背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- 集训第五周动态规划 J题 括号匹配
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- 集训第五周动态规划 E题 LIS
Description The world financial crisis is quite a subject. Some people are more relaxed while others ...
随机推荐
- NOI题库--盒子和小球系列 By cellur925
题目传送门 盒子和小球之二:N个有差别的盒子(1<=N<=20).你有A个红球和B个蓝球.0 <= A <= 15, 0 <= B <= 15.球除了颜色没有任何区 ...
- 组合数学练习题(一)——Chemist
题意: 从 n 个人中选出不超过 k 个人,再在选出的人中选出一些人成为队员,再在队员中选一名队长,求不同的方案数.答案 mod 8388608. 共有T组询问,每次给你n和k.T ≤ 10^4 k ...
- [转]MySQL游标的使用
转自:http://www.cnblogs.com/sk-net/archive/2011/09/07/2170224.html 以下的文章主要介绍的是MySQL游标的使用笔记,其可以用在存储过程的S ...
- 转】Spark SQL 之 DataFrame
原博文出自于: http://www.cnblogs.com/BYRans/p/5003029.html 感谢! Spark SQL 之 DataFrame 转载请注明出处:http://www.cn ...
- AJPFX关于Java Object类常用方法小总结
java.lang.Object java.lang包在使用的时候无需显示导入,编译时由编译器自动导入. Object类是类层次结构的根,Java中所有的类从根本上都继承自这个类. Object类 ...
- Spark学习之Spark调优与调试(7)
Spark学习之Spark调优与调试(7) 1. 对Spark进行调优与调试通常需要修改Spark应用运行时配置的选项. 当创建一个SparkContext时就会创建一个SparkConf实例. 2. ...
- sql server 中引號嵌套
在SQL字符串是以单引号作为分界符的,在字符串前面和后面各一个单引号.但是字符串中也能包含单引号,为了使语法分析器能够区分字符串中的单引号还是分界符.规定当字符串中出现单引号时,在其前面添加一个单引号 ...
- 从0开始搭建SQL Server 2012 AlwaysOn 第一篇(AD域与DNS)
随着业务发展,公司需要提高数据安全与性能需求,所以需要对新技术预研(先采坑),做技术积累: 了解相关AlwaysOn 故障转移集群(热备),数据路由(ICX),Moebius(莫比斯数据路由) 决定测 ...
- Farseer.net轻量级开源框架说明及链接索引
Farseer.net是什么? 基于.net framework 4 开发的一系列解决方案. 完全开源在GitHub中托管.并发布到NuGet中. Farseer.Net由最初的关系数据库ORM框架后 ...
- Jenkins安装和初始化配置
Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 所以安装Jenkins包含安装相应的jdk环境 ...