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

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;
}
相关推荐
梦境虽美,却不长6 分钟前
数据结构 学习 队列 2025年6月14日 11点22分
数据结构·学习·队列
GalaxyPokemon44 分钟前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
leo__5201 小时前
matlab实现非线性Granger因果检验
人工智能·算法·matlab
GG不是gg1 小时前
位运算详解之异或运算的奇妙操作
算法
FF-Studio3 小时前
万物皆数:构建数字信号处理的数学基石
算法·数学建模·fpga开发·自动化·音视频·信号处理·dsp开发
hy.z_7774 小时前
【数据结构】 优先级队列 —— 堆
数据结构
你的牧游哥4 小时前
前端面试题之将自定义数据结构转化成DOM元素
数据结构
float_六七4 小时前
Redis:极速缓存与数据结构存储揭秘
数据结构·redis·缓存
徐新帅4 小时前
基于 C 语言的图书管理系统开发详解
c语言·开发语言·数据结构
叶子爱分享4 小时前
从事算法工作对算法刷题量的需求
算法