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

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;
}
相关推荐
心中有国也有家16 小时前
cann-recipes-infer:昇腾 NPU 推理的“菜谱集合”
经验分享·笔记·学习·算法
绝知此事16 小时前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院16 小时前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet17 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
LuminousCPP17 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
AI算法沐枫18 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
m0_6294947319 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户20 小时前
用队列实现栈
数据结构·算法
做人求其滴20 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣