poj 1050 To the Max
来源:互联网 发布:php laravel 入门教程 编辑:程序博客网 时间:2024/05/22 02:23
题目链接:http://poj.org/problem?id=1050
题目大意:在一个矩阵中找到一个子矩阵,使该子矩阵和最大。
题目分析:一维:求最大连续子序列,状态方程:sum[i] = max(sum[i-1] + num[i], 0)。
num[]: -5, 7, -2, -6, 5, -1, 4
sum[]: 0, 7, 5, 0, 5, 4, 8
二维:转化为一维。对于该子矩阵从i(i<=0<n)行开始加到j(i<=j<n)行, 把每列上的数都相加,对于这一行和,用求最大连续子序列的方法求出最大值,答案即为所有情况中最大子序列和的最大值。
0行; 0 -2 -7 0
1行: 9 2 -6 2
2行: -4 1 -4 1
3行: -1 8 0 -2
(0,0) 0 -2 -7 0 sum = 0
(0,1) 9 0 -13 2 sum = 9
(0,2) 5, 1 -17 3 sum = 6
(0,3) 4 9 -17 1 sum = 13
(1,1) …… sum = ……
(1,2) ……
(3,3) ……
代码参考:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 100+9;int a[maxn][maxn], cur[maxn];int main(){ int n, i, j, k; while(~scanf("%d", &n)) { for(i=0; i<n; ++i) { for(j=0; j<n; ++j) { scanf("%d", &a[i][j]); } } int ans =0, sum; for(i=0; i<n; ++i)//枚举起始行i行 { memset(cur, 0, sizeof(cur)); for(j=i; j<n; ++j)//枚举加到j行 { for(k=0, sum=0; k<n; ++k)//枚举加到k列 {//一维的最大子段和 cur[k] += a[j][k]; sum = max(0, sum + cur[k]); ans = max(ans, sum); } } } printf("%d\n", ans); } return 0;}
- POJ 1050 To the Max
- poj 1050 To the Max
- POJ 1050 To the Max
- poj 1050 To the Max
- Poj 1050 To the Max
- POJ 1050 To the Max
- POJ 1050 To the Max
- POJ 1050 To the Max
- poj 1050 To the Max
- poj 1050 To the Max
- Poj 1050 To the Max
- POJ 1050 To the Max
- poj 1050 to the max
- POJ 1050 To the Max
- poj 1050 to the max
- poj-1050- To the Max
- POJ-1050-To the Max
- POJ 1050 To the Max
- sd文件的操作和获取SD卡剩于流量和总容量
- 基址重定位
- 数据库视频总结
- 工程中常用的宏
- Siebel Clib.rename(oldFileName,newFileName) 方法
- poj 1050 To the Max
- maven资源库
- 【Lucene3.6.2入门系列】第07节_高级搜索之普通Filter和自定义Filter
- POWER DESINGER 15.1 LICENCE
- 也谈雅虎中国
- uva-490 旋转句子
- linux系统下查看tomcat服务器实时 log日志的命令
- Linux系统学习笔记:shell环境
- poj-3468-A Simple Problem with Integers(线段树)