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思想来做,同时有个小技巧处理:就是把二维数组看做 ...
随机推荐
- 转化率最高的16个WordPress 电子商务主题
想自己开一个WordPress的电子商务商店?下面我们分享转化率最高的16个WordPress 电子商务主题,它们拥有最棒的用户体验,集成最新的用户体验,慢慢欣赏吧! 原文地址:http://thet ...
- ALSA音频工具amixer,aplay,arecord
ALSA音频工具编译安装 ========================================================================1.官网http://www. ...
- 用Robocod游戏来学习JAVA
Robocode(用游戏来学习Java技术还是用Java来玩游戏?)用你的JAVA编程技术来玩游戏吧!不会JAVA?那就用游戏来学习JAVA吧!什么是Robocode? 其实我对机器人一直很感兴趣.我 ...
- linux内存操作----kernel 3.5.X copy_from_user()和copy_to_user()
前面的一篇文章中简单的描写叙述了一下内存映射的内容,http://blog.csdn.net/codectq/article/details/25658813,这篇文章作为用户把内存规划好之后,在用户 ...
- java的wait和notifyAll使用方法
class Num { private int num; public int getNum() { return num; } public void setNum(int num) { this. ...
- java学习笔记day02
1.如何定义一个功能,并通过函数(方法)体现出来呢? 1)明确改功能运算后的结果.确定函数的返回值类型. 2)明确在实现该功能的过程中是否有未知内容参与运算.确定参数列表. 2.函数重载:在同一个 ...
- 自学HTML的几个例子
此处不赘述HTML中不同标签的用法仅仅给出自己学习时写的一些自娱自乐的例子,具体标签用法请参考W3C(http://www.w3school.com.cn/),毕竟这个才是最靠谱的,请不要相信任何二道 ...
- 数据库监控[Z]
--查看表锁 select * from sys.v_$sqlarea where disk_reads>100 --监控事例的等待 select event,sum(decode(wai ...
- 解决升级Xcode后插件不能使用的问题
从Xcode 5开始,苹果要求加入UUID证书从而保证插件的稳定性.因此Xcode版本更新之后需要在插件的Info.plist文件中添加当前Xcode的UUID. 具体步骤如下: 1.获取Xcode的 ...
- JSON 日期格式带 T 问题
var iso = new IsoDateTimeConverter(); iso.DateTimeFormat = "yyyy-MM-dd"; object obj = new ...