题解
1.当存在某个矩阵符合题意时,所有小于该矩阵的矩阵都符合题意
这是我们就可以想到用双指针
code
#include<bits/stdc++.h>
using namespace std;
int a[505][505]={0},sum[505][505]={0};
int n,m,k;
int check(int down,int right,int up,int left)
{
return sum[down][right]-sum[up-1][right]-sum[down][left-1]+sum[up-1][left-1]<=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];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+a[i][j];
int ans=0;
for(int up=1;up<=n;up++)
{
for(int down=up;down<=n;down++)
{
int left=1;
for(int right=1;right<=m;right++)
{
while(!check(down,right,up,left))left++;
ans+=right-left+1;
}
}
}
cout<<ans<<endl;
return 0;
}
标签:down,right,int,P8783,sum,矩阵,蓝桥,2022,505
From: https://www.cnblogs.com/pure4knowledge/p/18016131