PKU 1050-To The Max(找矩形内元素最大和)
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
is in the lower left corner:
9 2
and has a sum of 15.
Input
Output
Sample Input
0 -2 -7 0 9 2 -6 2
Sample Output
- #include<cstdio>
- #include<cstring>
- #include<string>
- #include<algorithm>
- #include<set>
- #include<map>
- #include<queue>
- #include<vector>
- #include<iterator>
- #include<utility>
- #include<sstream>
- #include<iostream>
- #include<cmath>
- #include<stack>
- using namespace std;
- const int INF=1000000007;
- const double eps=0.00000001;
- int N,elem[101][101];
- int RowSum[101][101],RecSum[101][101];
- inline void Get_RowSum() //处理每一行的前m个数的元素和
- {
- memset(RowSum,0,sizeof(RowSum));
- for(int x=1;x<=N;x++)
- for(int y=1;y<=N;y++)
- {
- if(y==1) RowSum[x][y]=elem[x][y];
- else RowSum[x][y]=RowSum[x][y-1]+elem[x][y];
- }
- }
- inline void Get_RecSum() //得到RecSum[][]
- {
- memset(RecSum,0,sizeof(RecSum));
- for(int x=1;x<=N;x++)
- for(int y=1;y<=N;y++)
- {
- if(x==1) RecSum[x][y]=RowSum[x][y];
- else RecSum[x][y]=RecSum[x-1][y]+RowSum[x][y];
- }
- }
- inline int Get(int upx,int upy,int lowx,int lowy)
- {
- return RecSum[lowx][lowy]-RecSum[upx][lowy]-(RecSum[lowx][upy]-RecSum[upx][upy]);
- }
- int Cal(int row,int col)
- {
- int ret=-INF;
- for(int i=0;i+row<=N;i++) //枚举每个矩形
- {
- for(int j=0;j+col<=N;j++)
- {
- int upx=i,upy=j,lowx=i+row,lowy=j+col;
- ret=max(ret,Get(upx,upy,lowx,lowy));
- }
- }
- return ret;
- }
- int main()
- {
- while(cin>>N)
- {
- for(int i=1;i<=N;i++)
- for(int j=1;j<=N;j++) scanf("%d",&elem[i][j]);
- Get_RowSum();
- Get_RecSum();
- int ans=-INF;
- for(int row=1;row<=N;row++) // 枚举矩形大小
- for(int col=1;col<=N;col++)
- ans=max(ans,Cal(row,col));
- cout<<ans<<endl;
- }
- return 0;
- }
PKU 1050-To The Max(找矩形内元素最大和)的更多相关文章
- lightOJ 1366 Pair of Touching Circles(统计矩形内相切圆对)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:给出一个矩形,在内部画两个圆A和B使得AB都完全在矩形内且AB相切且AB的 ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- 以log(n)的时间求矩形内的点
设想这么一个简单的问题,在一个平面上有n个点,给定一个矩形,问位于矩形内的点有哪些. 这个问题的简单思路非常简单,每次遍历所有点,看其是否在给定的矩形中.时间复杂度呢?单次查询的时间就是一次遍历的时间 ...
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- POJ 1050 To the Max 最详细的解题报告
题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...
- 洛谷P2241-统计方形-矩形内计算长方形和正方形的数量
洛谷P2241-统计方形 题目描述: 有一个 \(n \times m\) 方格的棋盘,求其方格包含多少正方形.长方形(不包含正方形). 思路: 所有方形的个数=正方形的个数+长方形的个数.对于任意一 ...
- CSS里常见的块级元素和行内元素
根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...
- CSS块级元素和行内元素
根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display属性值为“block”,成为“块级 ...
- css盒模型和块级、行内元素深入理解
盒模型是CSS的核心知识点之一,它指定元素如何显示以及如何相互交互.页面上的每个元素都被看成一个矩形框,这个框由元素的内容.内边距.边框和外边距组成,需要了解的朋友可以深入参考下 一.CSS盒模型 盒 ...
随机推荐
- Dungeon Game 解答
Question The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a ...
- SQL server 中 COUNT DISTINCT 函数
目的:统计去重后表中所有项总和. 直观想法: SELECT COUNT(DISTINCT *) FROM [tablename] 结果是:语法错误. 事实上,我们可以一同使用 DISTINCT 和 C ...
- Unity 屏幕适配小脚本
屏幕适配是可以通过代码实现的,相信给你时间就一定能写出来. 我们公司貌似没有分辨率适配框架通常对应小屏幕的苹果4要额外设置下等等就完了! 屏幕适配框架实现思路: 通过代码获取当前的分辨率 –> ...
- 精通CSS+DIV基础总结(二)
上一篇我们已经总结了部分CSS+DIV相关知识,这篇我们接着总结,从下边几个方面学习一下: 一,我们看如何设置网页的背景,顾名思义背景可以通过颜色和图片来设置,下边我们看一下如何设置: 颜色的设置非常 ...
- JConsole 连接配置
远程监控配置 JDK配置 在%JAVA_HOME%/jre/lib/management目录下,jmxremote.password.template.jmxremote.password需要修改配置 ...
- SQL:deferrable initially deferred
SQL> create table cust(id number,name varchar2(10));Table created SQL> alter table cust add co ...
- stagefright omx小结
由于stagefright和openmax运行在两个不同的进程上,所以他们之间的通讯要经过Binder进行处理,本小结不考虑音频这一块,假设视频为MP4封装的AVC编码文件. 先简单的看一下stage ...
- echarts演示笔记
http://echarts.baidu.com/doc/start.html 1.新建一个echarts.html文件,为ECharts准备一个具备大小(宽高)的Dom. <!DOCTYPE ...
- C#基础学习心得(一)
类的成员 数据成员:字段,常量(const) 函数成员:方法,属性,索引器,构造函数,析构函数,事件 类的声明 实例成员:对象相关性,不同于同一类的其他实例 静态成员:常量,static修饰的字段,方 ...
- js对象克隆, 深复制.
亲测有效: //对象克隆 function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == o ...