(DP)To The Max --HDU -- 1081
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1081
这道题使用到的算法是:预处理+最大连续子串和
如果会做最大连续子串和,那么理解这题就相对简单一些,若不知道最大连续子串和,建议先看一下这两题:
http://acm.hdu.edu.cn/showproblem.php?pid=1003
http://www.cnblogs.com/YY56/p/4855766.html
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10107 Accepted Submission(s): 4864
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.
8 0 -2
代码1:
#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 200
#define oo 0x3f3f3f3f int a[N][N], dp[N][N]; int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j, j1, j2; memset(dp, , sizeof(dp));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d", &a[i][j]);
dp[i][j] = dp[i][j-] + a[i][j]; /// dp[i][j] i 代表的是第 i 行,j 代表的是这行前 j 个数的和
} int S = ;
for(j1=; j1<=n; j1++)
for(j2=j1; j2<=n; j2++)
{ /** * i 很明显代表的是行数
* j1 从第几列开始
* j2 从第几列结束 **/ int mx=, my=; for(i=; i<=n; i++)
{
mx += dp[i][j2] - dp[i][j1-]; /// mx 代表的是前 i 行里,从第j1-1列到j2列的和(相当于矩阵了) if(mx>=)
{
if(mx>my) my = mx; /// my 记录的是前 i 行里,从第j1-1列到第j2列矩阵的最大和
}
else mx = ;
}
if(my>=S) S = my; /// S 里面存的肯定是在所有矩阵中取最大值
} printf("%d\n", S);
} return ;
}
代码2:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max2(a,b) (a>b?a:b) #define N 110
#define INF 0xfffffff int a[N][N], b[N][N][N]; int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
int i, j, k, x, max1; memset(a, , sizeof(a));
memset(b, , sizeof(b)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d", &a[i][j]); max1=-INF;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
for(x=, k=j; k>; k--)
{
x += a[i][k]; b[i][j][k] = max2(b[i][j][k], b[i-][j][k]) + x; if(b[i][j][k]>max1)
max1 = b[i][j][k];
} printf("%d\n", max1);
}
return ;
}
题目比较水暴力也可以过
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; #define met(a,b) (memset(a,b,sizeof(a)))
#define N 110
#define INF 0xffffff int a[N][N], sum[N][N]; int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j, i1, j1, Max=-INF; met(a, );
met(sum, ); for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d", &a[i][j]);
} for(i=; i<=n; i++)
for(j=; j<=n; j++)
sum[i][j] = sum[i-][j]+sum[i][j-]-sum[i-][j-] + a[i][j]; for(i=; i<=n; i++)
for(j=; j<=n; j++)
for(i1=i+; i1<=n; i1++)
for(j1=j+; j1<=n; j1++)
{
Max = max(Max, sum[i1][j1]-sum[i1][j]-sum[i][j1]+sum[i][j]);
} printf("%d\n", Max);
}
return ;
}
(DP)To The Max --HDU -- 1081的更多相关文章
- 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)
.navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...
- Tour(dp)
Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...
- 最长公共子序列长度(dp)
/// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...
- Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)
Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
随机推荐
- Phonegap项目创建 编译 安装 运行
一.创建 Phonegap项目 1. cd workspace 2.创建phonegap项目 cordova create 目录 识别符 显示名 例如:cordova create hello com ...
- redhat 连接mysql数据库Can't get hostname for your address
redhat 连接mysql数据库Can't get hostname for your address Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQ ...
- ORA-12560:TNS:协议器错误的解决方法
使用SQL Plus登录数据库时,系统报ORA-12560:TNS:协议器错误.之前建了三个数据库实例,删除了一个实例.现在登录其中一个数据库报此错误. 工具/原料 Oracle11g 方法/ ...
- java web jsp
一.WEB应用的目录结构 通常我们是在IDE中创建web应用程序,IDE自动为我们实现了WEB的目录结构,下面来看如何徒手创建一个WEB程序. 首先来看一下Tomcat自带的一个web应用的目录结构 ...
- "废物利用"也抄袭——“完全”DIY"绘图仪"<三、上位机程序设计>
上位机的程序主要是解析图片和生成较好的代码,现在实现的功能有灰度打印,二值打印,轮廓打印,骨骼打印.当然,必不可少的是打印大小的控制.测试了一些图片,总体来说,打印速度依次加快,因为打印的内容依次减少 ...
- 转转转---js正则表达exec与match的区别说明
正则表达式对象有两个定义方式:: 1.第一种定义: new RegExp(pattern, attributes);如var reg = new RegExp("abc",&quo ...
- urllib2的GET和POST请求(五)
urllib2默认只支持HTTP/HTTPS的GET和POST方法 urllib.urlencode() urllib 和 urllib2 都是接受URL请求的相关模块,但是提供了不同的功能.两个最显 ...
- scrapy与redis实战
从零搭建Redis-Scrapy分布式爬虫 Scrapy-Redis分布式策略: 假设有四台电脑:Windows 10.Mac OS X.Ubuntu 16.04.CentOS 7.2,任意一台电脑都 ...
- 关于TP5中的依赖注入和容器和facade
看了不少的文章,也看了官方的介绍,还是根据自己的理解,写写看法,理清下思路 只是单纯的说依赖注入Dependency Injection和容器 别的不白扯 比如有A,B,C三个类 A类的1方法依赖B类 ...
- UCI一位搞theory的教授列的一些数学方面的比较趣味性的网页
http://www.ics.uci.edu/~eppstein/recmath.html 据说是recreational的,没具体看,先收藏下来.