zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    }
  • 相关阅读:
    .netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析
    .netcore3.1 设置可跨域
    基于MMSE的预测
    概率模型
    [离散时间信号处理学习笔记] 15. 模拟信号的数字处理
    [离散时间信号处理学习笔记] 14. 多采样率信号处理
    [离散时间信号处理学习笔记] 13. 重采样
    [离散时间信号处理学习笔记] 12. 连续时间信号的离散时间处理以及离散时间信号的连续时间处理
    [离散时间信号处理学习笔记] 11. 连续时间信号的采样与重构
    [离散时间信号处理学习笔记] 10. z变换与LTI系统
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5146302.html
Copyright © 2011-2022 走看看