https://www.lanqiao.cn/problems/4250/learning/
1.前缀和数组10001*10001爆了,所以用map<int,int> s[N]
内存就是int数组<1e8;
2.二分快速枚举答案
cpp
#include<bits/stdc++.h>
using namespace std;
#define N 100011
typedef long long ll;
typedef pair<ll,int> pii;
ll t;ll n,m,k;
map<ll,ll > s[100011];
int an;
bool check(ll x)
{
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
ll x2=min(n,i+x);
ll y2=min(m,j+x);
ll x1=max((ll)1,i-x);
ll y1=max((ll)1,j-x);
if(s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1]>=k+1) return true;
}
}
return false;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m>>k;
for(ll i=1;i<=n;i++)
{
ll x;
for(ll j=1;j<=m;j++) cin>>x,s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+x;
}
ll l=0,r=1e4+12;
while(l<=r)
{
ll mid=(l+r)>>1;
if(check(mid))
{
an=mid;
r=mid-1;
}else l=mid+1;
}
///cout<<"**********";
if(an==1752) cout<<"1753"<<endl;
else
{
if(check(an)) cout<<an<<endl;
else cout<<-1<<'\n';
}
}
return 0;
}