大衣的旅行--前缀和+二分

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;
}
相关推荐
小娄~~23 分钟前
C语言卷子错题集
c语言·开发语言·数据结构
小L~~~26 分钟前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
洛水水1 小时前
【力扣100题】53.最长回文子串
算法·leetcode·职场和发展
jieyucx1 小时前
Go 语言 sort 包详解:从基础排序到自定义排序(含底层原理+零基础看懂)
算法·golang·排序算法·sort
叁散2 小时前
ESP32 LCD1602显示实验报告
算法
过期动态2 小时前
【LeetCode 热题 100】盛最多水的容器
java·数据结构·spring boot·算法·leetcode·spring cloud·职场和发展
凌波粒2 小时前
LeetCode--700.二叉搜索树中的搜索(二叉树)
算法·leetcode·职场和发展
君为先-bey2 小时前
LeMiCa——基于扩散模型的高效视频生成的词典序最小化路径缓存
python·算法·机器学习·扩散模型
洛水水2 小时前
【力扣100题】58.轮转数组
算法·leetcode
资深流水灯工程师3 小时前
LMS 最小均方算法在 DSP 上的 C 语言实现
算法