OpenJudge 2766 最大子矩阵
1.链接:
http://bailian.openjudge.cn/practice/2766
2.题目:
- 总Time Limit:
- 1000ms
- Memory Limit:
- 65536kB
- Description
- 已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。
比如,如下4 * 4的矩阵
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2的最大子矩阵是
9 2
-4 1
-1 8这个子矩阵的大小是15。
- Input
- 输入是一个N * N的矩阵。输入的第一行给出N (0 < N <= 100)。再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给出第二行的N个整数……)给出矩阵中的N2个整数,整数之间由空白字符分隔(空格或者空行)。已知矩阵中整数的范围都在[-127, 127]。
- Output
- 输出最大子矩阵的大小。
- Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2- Sample Output
15- Source
- 翻译自 Greater New York 2001 的试题
3.思路:
拓展的最大字段和。先遍历行的所有可能情况k=1-n。然后计算k行的矩阵每列的和,转为一维,在用最大字段和的方法求最大。
4.代码:
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main()
{
//freopen("C://input.txt","r",stdin); int i,j,k; int n;
cin >> n; int **arr_matrix = new int*[n];
for(i = ; i < n; ++i) arr_matrix[i] = new int[n]; for(i = ;i < n; ++i)
{
for(j = ;j < n; ++j)
{
cin >> arr_matrix[i][j];
}
} int *arr_temp = new int[n]; int *dp = new int[n]; int max_sum = arr_matrix[][];
for(k = ; k < n; ++k)
{
memset(arr_temp,,sizeof(int) * n);
for(j = ; j < n; ++j)
{
for(i = ; i < k; ++i) arr_temp[j] += arr_matrix[i][j];
} for(i = k; i < n; ++i)
{
for(j = ; j < n; ++j) arr_temp[j] += arr_matrix[i][j]; memset(dp,,sizeof(int) * n);
dp[] = arr_temp[];
for(j = ; j < n; ++j)
{
dp[j] = ((dp[j - ] + arr_temp[j]) > arr_temp[j]) ? (dp[j - ] + arr_temp[j]) : arr_temp[j];
if(max_sum < dp[j]) max_sum = dp[j];
} for(j = ; j < n; ++j) arr_temp[j] -= arr_matrix[i - k][j];
}
} cout << max_sum << endl; delete [] dp; delete [] arr_temp; for(i = ; i < n; ++i) delete [] arr_matrix[i];
delete [] arr_matrix; return ;
}
OpenJudge 2766 最大子矩阵的更多相关文章
- #DP# ----- OpenJudge最大子矩阵
OpenJudge 1768:最大子矩阵 总时间限制: 1000ms 内存限制: 65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 ...
- dp专练
dp练习. codevs 1048 石子归并 区间dp #include<cstdio> #include<algorithm> #include<cstring> ...
- noi openjudge 1768:最大子矩阵
链接:http://noi.openjudge.cn/ch0406/1768/ 描述已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如 ...
- ACM 中 矩阵数据的预处理 && 求子矩阵元素和问题
我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各 ...
- [BZOJ1127][POI2008] KUP子矩阵
Description 给一个n*n的地图,每个格子有一个价格,找一个矩形区域,使其价格总和位于[k,2k] Input 输入k n(n<2000)和一个n*n的地图 Output 输出矩形的左 ...
- 【OpenJudge 8463】Stupid cat & Doge
http://noi.openjudge.cn/ch0204/8463/ 挺恶心的一道简单分治. 一开始准备非递归. 大if判断,后来发现代码量过长,决定大打表判断后继情况,后来发现序号不对称. 最后 ...
- 【OpenJudge 191】【POJ 1189】钉子和小球
http://noi.openjudge.cn/ch0405/191/ http://poj.org/problem?id=1189 一开始忘了\(2^{50}\)没超long long差点写高精度Q ...
- 【OpenJudge 1665】完美覆盖
http://noi.openjudge.cn/ch0405/1665/?lang=zh_CN 状压水题,手动转移 #include<cstdio> #include<cstring ...
- 【OpenJudge 1793】矩形覆盖
http://noi.openjudge.cn/ch0405/1793/ 好虐的一道题啊. 看数据范围,一眼状压,然后调了好长时间QwQ 很容易想到覆盖的点数作为状态,我用状态i表示至少覆盖状态i表示 ...
随机推荐
- linux搜索jar内容
linux搜索 spring-beans-2.5.6.jar 内容 1.jar tvf spring-beans-2.5.6.jar -c 创建新的归档文件 -t 列出归档目录 -x 解压缩 ...
- Oracle用户及角色的权限管理[Oracle基础]
1.查看全部用户: select * from dba_users; select * from all_users; select * from user_users; 2.查看用户或角 ...
- Android 如何添加一种锁屏方式
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- HT518V311
HT518V311.rar
- File transfer in android with asmack and Openfire
http://harryjoy.com/2012/08/18/file-transfer-in-android-with-asmack-and-openfire/ http://www.javacod ...
- JQuery Mobile navbar动态刷新创建
今天突然用到须要动态改变tab页, 布局代码例如以下: <div data-role="navbar" id='divtab'> <ul id='divtabul ...
- 【转】MySQL数据库主从同步管理
MYSQL主从同步架构是目前使用最多的数据库架构之一,尤其是负载比较大的网站,因此对于主从同步的管理也就显得非常重要,新手往往在出现主从同步错误的时候不知道如何入手,这篇文章就是根据自己的经验来详细叙 ...
- Nodejs v4.x.0API文档学习(2)Assert断言测试模块
文档参考地址:https://nodejs.org/dist/latest-v4.x/docs/api/ Assert(断言) assert模块提供了一组简单的断言测试方法,可以拥有测试不变量.该模块 ...
- Guzzle Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON
项目更新到正式平台时,出现Guzzle(5.3) client get请求出现:Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, ...
- Spring引用测试
上下文 using System; using Spring.Core; using Spring.Aop; using System; using Spring.Core; using Spring ...