zoukankan      html  css  js  c++  java
  • 轰炸区最优选取

    这是一道水题

    题意是:给你一个n*n的矩阵,让我们找出其中k*k的最大值。

    题解:我们用一个前缀和数组来记录当前位置i,j的前面所有值,即sum[ i ][ j ]是前i*j 矩阵内的所有值的和;

    每次更新的时候我们sum【i】【j】的值为 sum[i][j] += sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];

    然后当我们i和j大于k之后我们来取最大值即可;

    接下来是代码:

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #define ll long long
    using namespace std;
    
    const int inf = 0x3f3f3f3f;
    const int maxn = 100010;
    int a[110][110];
    int sum[110][110];
    int main()
    {
        int n, k;
        cin >> n >> k;
        int maxx = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                cin >> sum[i][j];
                sum[i][j] += sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1];
                if (i >= k && j >= k)
                {
                    maxx = max(maxx, sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k]);
                }
            }
        }
        cout << maxx << endl;
    }
  • 相关阅读:
    LeetCode "Median of Two Sorted Arrays"
    LeetCode "Distinct Subsequences"
    LeetCode "Permutation Sequence"

    LeetCode "Linked List Cycle II"
    LeetCode "Best Time to Buy and Sell Stock III"
    LeetCode "4Sum"
    LeetCode "3Sum closest"
    LeetCode "3Sum"
    LeetCode "Container With Most Water"
  • 原文地址:https://www.cnblogs.com/csxaxx/p/13360372.html
Copyright © 2011-2022 走看看