CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum
题目连接:
http://codeforces.com/problemset/problem/173/C
Description
Let's consider a k × k square, divided into unit squares. Please note that k ≥ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of two cases: either we've reached the square's border or the square following after the next square is already painted. We finish painting at the moment when we cannot move in any direction and paint a square. The figure that consists of the painted squares is a spiral.
The figure shows examples of spirals for k = 3, 5, 7, 9.
You have an n × m table, each of its cells contains a number. Let's consider all possible spirals, formed by the table cells. It means that we consider all spirals of any size that don't go beyond the borders of the table. Let's find the sum of the numbers of the cells that form the spiral. You have to find the maximum of those values among all spirals.
Input
The first line contains two integers n and m (3 ≤ n, m ≤ 500) — the sizes of the table.
Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≤ aij ≤ 1000) is the number recorded in the j-th cell of the i-th row of the table.
Output
Print a single number — the maximum sum of numbers among all spirals.
Sample Input
6 5
0 0 0 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 0 1
1 0 0 0 1
1 1 1 1 1
Sample Output
17
Hint
题意
给你一个n*m的矩阵,然后问你螺旋线能够覆盖的最大和是多少
每一个正方形都会存在一个螺旋线
螺旋线就是从左上角开始绕成的形状……
题解:
暴力记忆化搜索就好了,n^3可过
但是空间只能n^2,所以滚动数组优化一下就好了
dp[i][j][len]表示以i,j为起点,正方形边长为len的覆盖的值是多少
dp[i][j][len]显然等于len所在的正方形覆盖的和 - mp[i+1][j] - dp[i+1][j+1][len-2]
然后扑通扑通跑dp就好了
因为dp转移只和一层状态有关,所以直接滚动数组优化
代码
#include<bits/stdc++.h>
using namespace std;
int n,m;
int mp[520][520];
int sum[520][520];
int vis[520][520];
int dp[520][520][2];
int cal(int x1,int y1,int x2,int y2)
{
return sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
}
int solve(int x,int y,int len)
{
if(len==0)
{
dp[x][y][0]=mp[x][y];
return dp[x][y][0];
}
else
{
dp[x][y][0]=cal(x,y,x+len,y+len);
dp[x][y][0]-=mp[x+1][y];
dp[x][y][0]-=dp[x+1][y+1][1];
return dp[x][y][0];
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&mp[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
sum[i][j]=mp[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
int ans = -1e9;
for(int k=0;k<=min(n,m);k+=2)
{
memset(vis,0,sizeof(vis));
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--)
dp[i][j][1]=dp[i][j][0];
for(int i=n;i>=1;i--)
{
if(i+k>n)continue;
for(int j=m;j>=1;j--)
{
if(j+k>m)continue;
int p = solve(i,j,k);
if(k>0)
ans = max(ans,p);
}
}
}
printf("%d\n",ans);
}
CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化的更多相关文章
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 918D MADMAX(博弈+记忆化搜索)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CodeForces 173C Spiral Maximum (想法、模拟)
Spiral Maximum Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...
- Codeforces 667C Reberland Linguistics 记忆化搜索
链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...
- Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...
- 洛谷P1434滑雪题解及记忆化搜索的基本步骤
题目 滑雪是一道dp及记忆化搜索的经典题目. 所谓记忆化搜索便是在搜索的过程中边记录边搜索的一个算法. 当下次搜到这里时,便直接使用. 而且记忆化搜索一定要满足无后效性,为什么呢,因为如果不满足无后效 ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
随机推荐
- Spring3.0.6定时任务
项目使用的Spring版本比较旧是3.0.6版本,由于需要进行定时任务,就决定使用Spring自带的scheduled task. 在网上找了很多文章,也查看了Spring3.0.6的官方文档,按照网 ...
- vbScript常用运算符与函数
基本运算 + 数字加法及字符串连接 - 数字减法 * 数字乘法 / 数字除法 Mod 求余数 \ 求商数 & 字符串连接 ^ 次方 = 相等 <> 不相等 >= 大于或等于 ...
- JAVA & Android 系统环境变量配置
Java JAVA_HOME:C:\Program Files\Java\jdk1.7.0_40 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\ ...
- c/c++工程中外部头文件及库添加方法
在VS工程中,添加c/c++工程中外部头文件及库的基本步骤: 1.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录. 2.添加文件引用 ...
- ps做gif 登陆下拉菜单效果
作者这里仅介绍登录动画的制作思路和简单过程.一些细节的制作,如登录框,每一帧的图像等都需要自己根据参考图慢慢完成.最终效果 1.新建大小适当的文件,背景填充暗蓝色.首先设计一个底座,主要用图层样式来完 ...
- 在C语言环境下使用google protobuf
本文写给经常使用C编程且不喜欢C++而又要经常使用google protobuf的人. 经常写通讯程序的人对数据进行序列化或者反序列化时,可能经常使用google的protobuf(PB ...
- Linux下的GitHub安装与简单配置教程
1.GitHub简介 Git是一个分布式版本控制系统,与其相对的是CVS.SVN等集中式的版本控制系统. 2.Git的安装 1)安装Git a.查看与使用 在ubuntu下可以使用如下命令进行查看系统 ...
- 实现JQuery EasyUI右键菜单变灰不可用效果
使用过EasyUI的朋友想必都知道疯狂秀才写的后台界面吧,作为一个初学者我不敢妄自评论它的好坏,不过它确实给我们提供了一个很好框架,只要在它的基础上进行修改,基本上都可以满足我们开发的需要. 知道“疯 ...
- [转]https方式使用git保存密码的方式
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...
- phonegap,Cordova 使用html5标签
某些安卓手机的webview使用location.href="tel:123456"不能调到打电话的界面,可以用下面的解决办法: config.xml文件最后加上一行: <a ...