做網(wǎng)站下載別人的圖算不算侵權(quán)北京網(wǎng)站sem、seo
統(tǒng)計子矩陣
-
核心思想:矩陣前綴和 + 雙指針
- 用i和j雙指針 遍歷所有子矩陣的列
- 用s和t雙指針 遍歷所有子矩陣的行
- 求其子矩陣的和 若>k 將s向下移動 矩陣和必定減小(元素個數(shù)減少)
- 直到滿足<=k 因為列一定 行數(shù)即為方案數(shù)(從t行往上數(shù)到s行 共t-s+1個區(qū)間[t,t][t-1,t]….[s,t])
-
#include<iostream>using namespace std;const int N = 510;typedef long long LL;int a[N][N];int n,m,k;int main(){cin>>n>>m>>k;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){cin >> a[i][j];a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1]; //求前綴和數(shù)組}}LL res = 0;for(int i=1;i<=m;i++) //遍歷列{for(int j=i;j<=m;j++){for(int s=1,t=1;t<=n;t++) //遍歷行{while(s<=t && a[t][j] - a[s-1][j] - a[t][i-1] + a[s-1][i-1] > k)s++;if(s<=t) res += t-s+1;}}}cout<<res;}