poj1050(nyoj104 zoj1074)dp问题
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39913 | Accepted: 21099 |
Description
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
input consists of an N * N array of integers. The input begins with a
single positive integer N on a line by itself, indicating the size of
the square two-dimensional array. This is followed by N^2 integers
separated by whitespace (spaces and newlines). These are the N^2
integers of the array, presented in row-major order. That is, all
numbers in the first row, left to right, then all numbers in the second
row, left to right, etc. N may be as large as 100. The numbers in the
array will be in the range [-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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int temp[],n;
int cal() //
{
int Max = temp[];
int sum = temp[];
for(int i = ;i<n;i++)
{
if(sum+temp[i]<temp[i])
sum = temp[i];
else
sum+=temp[i];
if(Max<sum)
Max = sum;
}
return Max;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int a[][];
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
scanf("%d",&a[i][j]);
int Max = ;
for(int i = ;i< n;i++) //i是起始行
{
for(int j =i ;j<n;j++) //j是终止行
{
memset(temp,,sizeof(temp));
for(int m = ;m<n;m++) //固定列,注意是行在变
{
for(int k =i ;k<=j;k++) //累加i起始行,j终止行中间的同列的数据
temp[m]+=a[k][m];
}
int MaxTemp = cal();
if(MaxTemp>Max)
Max = MaxTemp;
} }
printf("%d\n",Max);
}
return ;
}
事实证明我蠢了,后来看到nyoj这题的最优程序解答,在处理第i行到j行同列相加上面处理的很好,利用输入时候进行累加,然后做减法,直接减掉了一层循环
nyoj 的版本
#include<iostream>
#include<cstring>
using namespace std;
#define N 110
int a[N][N];
int b[N];
int main()
{
int n,r,c;
cin>>n;
while(n--)
{
cin>>r>>c;
for(int i=;i<=r;++i)
for(int j=;j<=c;++j)
{
cin>>a[i][j];
a[i][j]+=a[i-][j];
}
int max=a[][];
for(int i=;i<=r-;++i)
for(int j=i+;j<=r;++j)
{
memset(b,,sizeof(b));
for(int k=;k<=c;++k)
{
if(b[k-]>=)
b[k]=b[k-]+a[j][k]-a[i][k];
else
b[k]=a[j][k]-a[i][k];
if(max<b[k])
max=b[k];
}
}
cout<<max<<endl;
}
}
还有一种没有用这种求和的方法,但是是先求第一行最大子段和,再求第一行跟第二行合起来的最大子段和,,再求第一到第三合起来的最大子段和,以此类推,直到求出整个矩阵的合起来的最大子段和,最后就是我们需要的解 。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int temp[],n;
int cal() //最大子段和
{
int Max = temp[];
int sum = temp[];
for(int i = ;i<n;i++)
{
if(sum+temp[i]<temp[i])
sum = temp[i];
else
sum+=temp[i];
if(Max<sum)
Max = sum;
}
return Max;
} int main()
{
while(scanf("%d",&n)!=EOF)
{
int a[][];
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
scanf("%d",&a[i][j]);
int Max = ;
for(int i = ;i< n;i++)
{
memset(temp,,sizeof(temp));
for(int j =i ;j<n;j++)
{
for(int k = ;k<n;k++)
temp[k]+=a[j][k];
int t = cal();
if(t>Max)
Max =t;
} }
printf("%d\n",Max);
}
return ;
}
poj1050(nyoj104 zoj1074)dp问题的更多相关文章
- [POJ1050]To the Max(最大子矩阵,DP)
题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...
- ZOJ1074 (最大和子矩阵 DP)
F - 最大子矩阵和 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Descri ...
- poj1050 dp动态规划
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- POJ1050【DP】
题意: 求一个最大子矩阵和. 思路: 枚举行区间,然后求一个最大子序列和. 贴一发挫code- #include <iostream> #include <cstdio> #i ...
- POJ1050(dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 46788 Accepted: 24774 Desc ...
- (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54338 Accepted: 28752 Desc ...
- poj1050 To the Max(降维dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49351 Accepted: 26142 Desc ...
- DP总结 ——QPH
常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...
- POJ1050:To the max
poj1050:http://poj.org/problem?id=1050 * maximum-subarray 问题的升级版本~ 本题同样是采用DP思想来做,同时有个小技巧处理:就是把二维数组看做 ...
随机推荐
- 【温故而知新-万花筒】C# 异步编程 逆变 协变 委托 事件 事件参数 迭代 线程、多线程、线程池、后台线程
额基本脱离了2.0 3.5的时代了.在.net 4.0+ 时代.一切都是辣么简单! 参考文档: http://www.cnblogs.com/linzheng/archive/2012/04/11/2 ...
- 在Linux CentOS 6.5 (Final)上安装git-1.9.0
CentOS 6.5 (Final)默认安装的git版本为1.7.1.3,而我们希望安装1.9.0版本.由于rpm安装库里没有1.9.0版本,因此我们需要找其它方法来安装. 网上有很多文章介绍了如何从 ...
- ObjectOutputStream 追加写入读取错误 - 自己的实现方案
本篇博客灵感来自http://blog.csdn.net/chenssy/article/details/13170015 问题描述.问题出现的原因.尝试解决办法,请参见鄙人上一编博客. 上一编文章解 ...
- html 表格中文字的背景色
- Funny Sheep(思维)
Problem 1606 - Funny Sheep Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 612 Accepted ...
- 编解码学习笔记(十):Ogg系列
Ogg是一个自由且开放标准的容器格式,由Xiph.Org 基金会所维护.Ogg格式并不受到软件专利的限制,并设计用于有效率地串流媒体和处理高质量的数字多媒体. Ogg意指一种文件格式,能够纳入各式各样 ...
- 点滴记录——Centos 6.5 yum安装Ganglia
转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701141 注:下面操作都仅仅是在一台机器上操作 1. 安装php支持 yum inst ...
- oracle中的rowid--伪列-删除表中的重复内容-实用
1.rowid是一个伪列,是用来确保表中行的唯一性,它并不能指示出行的物理位置,但可以用来定位行. 2.rowid是存储在索引中的一组既定的值(当行确定后).我们可以像表中普通的列一样将它选出来. 3 ...
- django学习笔记一
django作为一个python的开源项目发布,其web框架采用了mtv设计模式 在目前一些较为成熟的大型网站中有不少网站的应用基于django开发,django作为一个重量型的web框架提供了以下的 ...
- Jq合成事件绑定
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...