zoukankan      html  css  js  c++  java
  • CodeForces 173C Spiral Maximum (想法、模拟)

    Spiral Maximum
    Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    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值递增)

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<vector>
     7 #define LL long long
     8 #define maxn 505
     9 #define inf 0x3f3f3f3f
    10 #define IN freopen("in.txt","r",stdin);
    11 using namespace std;
    12 
    13 int n,m;
    14 int val[maxn][maxn];
    15 int row[maxn][maxn];
    16 int col[maxn][maxn];
    17 
    18 void input(){
    19     for(int i=1; i<=n; i++)
    20         for(int j=1; j<=m; j++)
    21             scanf("%d",&val[i][j]);
    22     for(int i=1; i<=n; i++){
    23         row[i][0] = 0;
    24         for(int j=1; j<=m; j++){
    25             row[i][j] = row[i][j-1]+val[i][j];
    26         }
    27     }
    28     for(int i=1; i<=m; i++){
    29         col[i][0] = 0;
    30         for(int j=1; j<=n; j++){
    31             col[i][j] = col[i][j-1]+val[j][i];
    32         }
    33     }
    34 }
    35 
    36 bool is_ok(int x, int y){
    37     return x>=1 && y>=1 && x<=n &&y<=m;
    38 }
    39 
    40 int main(int argc, char const *argv[])
    41 {
    42     //IN;
    43 
    44     while(scanf("%d %d",&n,&m)!=EOF)
    45     {
    46         input();
    47 
    48         int ans = -inf;
    49         for(int i=1; i<=n; i++){
    50             for(int j=1; j<=m; j++){ //center
    51                 int cur = val[i][j];
    52                 int tol = val[i][j];
    53                 int ex = i, ey = j-1;
    54                 int x1 = i-1, y1 = j-1;
    55                 int x2 = i+1, y2 = j+1;
    56                 while(is_ok(x1,y1) && is_ok(x2,y2) && is_ok(ex,ey)){
    57                     tol += row[x1][y2] - row[x1][y1-1];
    58                     tol += row[x2][y2] - row[x2][y1-1];
    59                     tol += col[y1][x2-1] - col[y1][x1];
    60                     tol += col[y2][x2-1] - col[y2][x1];
    61 
    62                     cur = tol - cur - val[ex][ey];
    63                     ans = max(ans, cur);
    64 
    65                     ex = x1; ey = y1-1;
    66                     x1 = x1-1, y1 = y1-1;
    67                     x2 = x2+1, y2 = y2+1;
    68                 }
    69 
    70             }
    71         }
    72 
    73         printf("%d
    ", ans);
    74     }
    75 
    76     return 0;
    77 }
  • 相关阅读:
    Reusable action with query database
    Oracle实现分组统计记录
    Oracle行列转换的几种实现方法
    junit私有方法测试
    Junit实现抽象类测试(二)
    C++的性能C#的产能?! .Net Native 系列《二》:.NET Native开发流程详解
    C++的性能C#的产能?! .Net Native 系列向导
    c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥
    辞职敬礼
    WPF 心路历程
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5327438.html
Copyright © 2011-2022 走看看