• leetcode 4405.统计子矩阵


    这道题其实就是运用二维前缀和进行计算的。

    如果说要进行暴力,那就是四重循环来凑活:

    1. #include<iostream>
    2. #include<stdio.h>
    3. #include<cstring>
    4. #include<cstdlib>
    5. #include<cmath>
    6. #include<vector>
    7. #include<algorithm>
    8. #include<stack>
    9. #include<queue>
    10. #include<sstream>
    11. #include<map>
    12. #include<limits.h>
    13. #include<set>
    14. #define MAX 505
    15. #define _for(i,a,b) for(int i=a;i<(b);i++)
    16. #define ALL(x) x.begin(),x.end()
    17. using namespace std;
    18. typedef long long LL;
    19. typedef pair<int,int> PII;
    20. int arr[MAX][MAX];
    21. int b[MAX][MAX];
    22. LL n, m, counts, num;
    23. void qianzhuihe() {
    24. for (int i = 1; i <= n; i++) {
    25. for (int j = 1; j <= m; j++) {
    26. b[i][j] = b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1] + arr[i][j];
    27. }
    28. }
    29. }
    30. int main() {
    31. ios::sync_with_stdio(false);
    32. cin.tie(NULL); cout.tie(NULL);
    33. cin >> n >> m >> num;
    34. _for(i, 1, n+1) {
    35. _for (j, 1, m + 1) {
    36. cin >> arr[i][j];
    37. }
    38. }
    39. qianzhuihe();
    40. for (int x1 = 1; x1 <= n; x1++) {
    41. for (int y1 = 1; y1 <= m; y1++) {//定下一个固定点
    42. for (int x2 = x1; x2 <= n; x2++) {
    43. for (int y2 = y1; y2 <= m; y2++) {
    44. if (b[x2][y2] - b[x1 - 1][y2] - b[x2][y1 - 1] + b[x1-1][y1-1] <= num)
    45. counts++;
    46. }//依次遍历其他的子矩阵
    47. }
    48. }
    49. }
    50. cout << counts;
    51. return 0;
    52. }

    所以我们需要进行优化才行:

    这里我们的优化用到了双指针的思路。

    那么,到底为什么需要这样用呢?我接下来就讲一讲具体的思路:

    首先,我们知道,在对于矩阵进行遍历的时候,我们是有四个边界的,也就是上界下界,左界右界一共就这4个边界,要想让时间复杂度降低的话,我们思考怎么才能减少一层循环才行。

    假设,我们把上下两个边界进行了固定,那么剩下的左右边界移动,这个时候是不是就是在这一个小边界里进行了遍历呢?也就是说,我们把这个问题转变成了动态的一维前缀和问题了。

    也就是说,如果上下边界固定住了,假如上边界是1,下边界是2,这个时候这两个边界之间是不是就只剩下了一行数据了呢?这样是不是一维的前缀和考虑了呢?显然是的。但是,如果说上下边界分别是4,1的话,中间不就是有3行了吗?这样该怎么办呢?我们类比着,将着每一列的数加起来,这样不就变成了一列一个数了吗?这样不就变相的转变成了一维数组的问题了么?

    其实这样就已经可以了,我们这个时候定义两个指针,进行一前一后的遍历,显然要比我们一直遍历要高效,也就是左指针和右指针。双指针能够根据判断当前的权值是否能够达到题目中的条件做出判断,如果说此时的权值要大,说明我们需要缩小现在的区间,让左指针向着右指针的方向靠拢。

    有人问,为啥不是右指针开始往后找呢?这里我们的右指针其实也是从跟左指针一样的初始位置,你如果这样认为,那有可能是其他的双指针题目让你把右指针放在了最后面,其实这样也行,不妨尝试一下。我们就只按照现在这个思路说。

    上代码:

    1. #include<iostream>
    2. #include<stdio.h>
    3. #include<cstring>
    4. #include<cstdlib>
    5. #include<cmath>
    6. #include<vector>
    7. #include<algorithm>
    8. #include<stack>
    9. #include<queue>
    10. #include<sstream>
    11. #include<map>
    12. #include<limits.h>
    13. #include<set>
    14. #define MAX 505
    15. #define _for(i,a,b) for(int i=a;i<(b);i++)
    16. #define ALL(x) x.begin(),x.end()
    17. using namespace std;
    18. typedef long long LL;
    19. typedef pair<int,int> PII;
    20. int arr[MAX][MAX];
    21. int b[MAX][MAX];
    22. LL n, m, counts, num;
    23. int main() {
    24. ios::sync_with_stdio(false);
    25. cin.tie(NULL); cout.tie(NULL);
    26. cin >> n >> m >> num;
    27. _for(i, 1, n+1) {
    28. _for (j, 1, m + 1) {
    29. cin >> arr[i][j];
    30. arr[i][j] += arr[i - 1][j];
    31. }
    32. }
    33. for (int i = 1; i <= n; i++) {
    34. for (int j = i; j <= n; j++) {
    35. for (int l = 1, r = 1, sum = 0; r <= m; r++) {
    36. sum += arr[j][r] - arr[i - 1][r];
    37. while (sum > num) {
    38. sum -= arr[j][l] - arr[i - 1][l];
    39. l++;
    40. }
    41. counts += r - l + 1;
    42. }
    43. }
    44. }
    45. cout << counts << endl;
    46. return 0;
    47. }

  • 相关阅读:
    使用 PHP 和 MySQL 的投票和投票系统
    HarmonyOS(二)Ability应用模型概述
    PowerDesigner 设置
    Docker知识总结 (五) Dockerfile
    QML代码生成
    1.1 数据采集总览
    SRS Config 二 Stream Caster
    【动手学深度学习】RNN浅记
    使用Cubemap和Volume Textures之间传输数据的示例编程
    199道SpringCloud面试题,你能答上来吗
  • 原文地址:https://blog.csdn.net/m0_73917165/article/details/136662026