【统计子矩阵——部分前缀和+双指针】

题目

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 510;
int s[N][N];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int n, m, k;
    cin >> n >> m >> k;
    
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            cin >> s[i][j];
            s[i][j] += s[i-1][j];
        }
    
    ll ans =  0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = i; j <= n; j++)
        {
            for(int l = 1, r = 1, sum = 0; r <= m; r++)
            {
                sum += s[j][r] - s[i-1][r];
                while(sum > k)
                {
                    sum -= s[j][l] - s[i-1][l];
                    l++;
                }
                
                ans += r - l + 1;
            }
        }
    }
    
    cout << ans;
}
相关推荐
Hcoco_me3 小时前
大模型面试题17:PCA算法详解及入门实操
算法
跨境卫士苏苏3 小时前
亚马逊AI广告革命:告别“猜心”,迎接“共创”时代
大数据·人工智能·算法·亚马逊·防关联
云雾J视界4 小时前
当算法试图解决一切:技术解决方案主义的诱惑与陷阱
算法·google·bert·transformer·attention·算法治理
Xの哲學4 小时前
Linux Miscdevice深度剖析:从原理到实战的完整指南
linux·服务器·算法·架构·边缘计算
夏乌_Wx5 小时前
练题100天——DAY23:存在重复元素Ⅰ Ⅱ+两数之和
数据结构·算法·leetcode
立志成为大牛的小牛5 小时前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法
沿着路走到底6 小时前
将数组倒序,不能采用reverse,算法复杂度最低
算法
IDIOT___IDIOT6 小时前
KNN and K-means 监督与非监督学习
学习·算法·kmeans
Hcoco_me6 小时前
大模型面试题18:t-SNE算法详解及入门实操
算法