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思想来做,同时有个小技巧处理:就是把二维数组看做 ...
随机推荐
- .net 反编译利器 dnspy
Binaries Latest release: https://github.com/0xd4d/dnSpy/releases Latest build (possibly unstable): h ...
- 绘图工具graphviz学习使用
画图工具: http://www.tuicool.com/articles/r2iAfa http://www.tuicool.com/articles/RjQfey 绘图工具graphviz学习使用 ...
- asp数组的使用
定义简单数组 有两种方法在asp中定义和初始化数组,让我们看看每种的例子: 方法一:MyArray = Array("Jan","Feb","Mar& ...
- Hibernate更新部分字段浅谈
update语句是在Hibernate的Configuration的时候生成的,不能动态改变.为什么update的时候所有的属性都一起update,而不是只更新改变字段,其实这是一个比较值得探讨的问题 ...
- SQL注入原理深度解析
本文转自:http://www.iii-soft.com/forum.php?mod=viewthread&tid=1613&extra=page%3D1 对于Web应用来说,注射式攻 ...
- C#.NET学习笔记11,12---布尔表达式2组合,if语句
C#.NET学习笔记11---布尔表达式2组合 2013/9/6 技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com int ...
- ubuntu下安装xlrd模块,Mysqldb模块
Python中安装xlrd模块 1.从 https://pypi.python.org/pypi/xlrd 下载压缩包 2.解压所下载的压缩包 3.CD到解压文件夹,运行 sudo python s ...
- [Python] 发送email的几种方式
python发送email还是比較简单的,能够通过登录邮件服务来发送,linux下也能够使用调用sendmail命令来发送,还能够使用本地或者是远程的smtp服务来发送邮件,无论是单个,群发,还是抄送 ...
- [AC自动机][HDU3065]
//====================== // HDU 2222 // 求目标串中出现了几个模式串 //输入 //1 //5 //she //he //say //shr //her //ya ...
- ffmpeg常用参数一览
基本选项: -formats 输出所有可用格式 -f fmt 指定格式(音频或视频格式) -i filename 指定输入文件名,在linux下当然也能指定:0.0(屏幕录制)或摄像头 -y 覆盖已有 ...