您的位置:首页 > 其它

CodeForces 173C Spiral Maximum (想法、模拟)

2016-03-27 23:43 369 查看
Spiral Maximum
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 173C

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

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


Output
17


Input
3 3
1 1 1
1 0 0
1 1 1


Output
6


Input
6 6
-3 2 0 1 5 -1
4 -1 2 -3 0 1
-5 1 2 4 1 -2
0 -2 1 3 -1 2
3 1 4 -3 -2 0
-1 2 -1 3 1 2


Output
13


Hint

In the first sample the spiral with maximum sum will cover all 1's of the table.

In the second sample the spiral may cover only six 1's.

【题意】:

在m*n的表中找出权值和最小的螺旋线。

【解题思路】:

螺旋线的个数上限为500*500*250。

观察可以看到 相邻两个螺旋线再加上一个小方格就可以组成一个方阵。

只要预处理出方阵的权值和,就可以在O(1)内从一个螺旋线到另一个螺旋线。

这里的枚举方式是:枚举中心点,以中心点为基准向外依次拓展螺旋线(对应K值递增)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define LL long long
#define maxn 505
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n,m;
int val[maxn][maxn];
int row[maxn][maxn];
int col[maxn][maxn];

void input(){
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d",&val[i][j]);
for(int i=1; i<=n; i++){
row[i][0] = 0;
for(int j=1; j<=m; j++){
row[i][j] = row[i][j-1]+val[i][j];
}
}
for(int i=1; i<=m; i++){
col[i][0] = 0;
for(int j=1; j<=n; j++){
col[i][j] = col[i][j-1]+val[j][i];
}
}
}

bool is_ok(int x, int y){
return x>=1 && y>=1 && x<=n &&y<=m;
}

int main(int argc, char const *argv[])
{
//IN;

while(scanf("%d %d",&n,&m)!=EOF)
{
input();

int ans = -inf;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){ //center
int cur = val[i][j];
int tol = val[i][j];
int ex = i, ey = j-1;
int x1 = i-1, y1 = j-1;
int x2 = i+1, y2 = j+1;
while(is_ok(x1,y1) && is_ok(x2,y2) && is_ok(ex,ey)){
tol += row[x1][y2] - row[x1][y1-1];
tol += row[x2][y2] - row[x2][y1-1];
tol += col[y1][x2-1] - col[y1][x1];
tol += col[y2][x2-1] - col[y2][x1];

cur = tol - cur - val[ex][ey];
ans = max(ans, cur);

ex = x1; ey = y1-1;
x1 = x1-1, y1 = y1-1;
x2 = x2+1, y2 = y2+1;
}

}
}

printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: