Largest Allowed Area(二维前缀和+二分)

本文介绍了一个寻找最大正方形区域的算法,目标是在包含最多一个森林块的地图上找到最大的平方区域,适用于公司总部选址问题。算法使用二维前缀和与二分查找技术,高效解决了大规模地图搜索难题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Largest Allowed Area

Gym - 102091L 

A company is looking for land to build its headquarters. It has a lot of money and can buy as 
many land patches as it needs. Its goal, however, is finding the largest square region 
containing no forest. Unfortunately, there is no such region that is large enough for the 
headquarters they want to build.
After negotiation with the government and the evaluation of environmental impacts, the 
government allows the company to purchase land with at most one forest patch. In other 
words, the company’s goal is now finding the largest square region containing at most one 
forest patch.
To facilitate the search process, the company creates a map in the form of a 2D table 
consisting R rows and C columns. In this 2D table, each entry represents a land of patch 
where 0 corresponds to a non-forest patch and 1 to a forest patch. Unfortunately, the map 
may have up to 1,000 x 1,000 entries and it is not a good idea to manually look for the largest 
allowed square region. This is where your skill comes into play. Write an efficient algorithm 
to find such a square region.


Input:
The first line is a positive integer T <= 20 representing the number of test cases. For each 
case, the input is formatted as follows.
First line R C
where R and C represents the number of rows and columns in the 
map. Also, 5 <= R, C <= 1,000
Next R lines Each line represents a row in the map from the first to last. It has C 
numbers which are 0 or 1, separated by one space.
Note: there is at least one non-forest patch in each test case.
Output:
There are T lines in the output. Each line is the number of rows in the largest allowed square 
region for each case.
2018 ICPC
Asia Nakhon Pathom
Regional Contest
Problem L
Largest Allowed Area
Time Limit 1 sec
2018 ICPC Asia Nakhon Pathom Regional Contest
Sample Input/Output
Input Output
2
10 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
20 10
1 0 0 0 0 1 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 1 0 
0 0 1 0 0 0 0 1 1 0 
0 0 0 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 0 0 1 
0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 1 0 0 0 1 0 0 0 
0 0 0 0 0 0 0 0 0 0

https://vjudge.net/contest/326230#problem/L

题意:求一个最大正方形矩阵,矩阵和不超过1

 

二维前缀和递推公式:dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+map[i][j]

求x1,y1到x2,y2的区间和:ans = dp[x2][y2]-dp[x2][y1-1]-dp[x1-1][y2]+dp[x1-1][x2-1]

 

先求出二维前缀和,再对正方形的边长二分找到答案

#include<bits/stdc++.h>
using namespace std;

int a[1200][1200],dp[1200][1200],n,m;

bool Find(int num)
{

    for(int i = 1; i <=n; i ++)
    {
        if(i+num-1> n)
            break;
        for(int j = 1; j <= m; j ++)
        {
            if(j+num-1> m)
                break;
            int sum = dp[i+num-1][j+num-1]+dp[i-1][j-1]-dp[i+num-1][j-1]-dp[i-1][j+num-1];
            if(sum <= 1)
                return 1;
        }
    }
    return 0;
}

int main()
{
    int i,j,t;
    char ch;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d %d",&n, &m);
        for(i = 1; i <= max(n,m); i ++)
        {
            dp[i][0] = dp[0][i] = 0;
        }
        for(i = 1; i <= n; i ++)
        {
            for(j = 1; j <= m; j ++)
            {
            ch = getchar();
            while(ch!='0'&&ch!='1')
            ch = getchar();
            a[i][j] = ch-'0';

            }
        }

        for(i = 1; i <= n; i ++)
        {
            for(j = 1; j <= m; j ++)
            {
                dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+a[i][j];
            }
        }

        int l = 1;
        int r = min(n,m);
        int ans = -1;
        while(l <= r)
        {
            int mid = (l+r)>>1;
            if(Find(mid))
            {
                ans = mid;
                l = mid+1;
            }
            else
                r = mid-1;
        }
        printf("%d\n",ans);


    }
    return 0;
}

 

### MATLAB 中实现二维速度追踪算法的方法 Meanshift 是一种基于核密度估计的无监督学习方法,广泛应用于图像处理和计算机视觉领域中的目标跟踪任务。它通过迭代寻找数据分布的最大值来定位目标位置[^1]。 为了在 MATLAB 中实现二维速度追踪算法,可以采用 Meanshift 的核心思想并扩展到速度域上。以下是具体实现方式: #### 1. 数据准备 首先需要定义输入数据集,通常是一个包含目标物体特征(如颜色直方图或灰度梯度)的空间坐标集合。对于二维速度追踪,假设我们已经获取了一组连续帧上的目标运动轨迹点 $(x_i, y_i)$ 和对应的时间戳 $t_i$。 #### 2. 构建核函数 Meanshift 使用加权平均计算新的中心位置。常用的高斯核函数形式如下: $$ w(\mathbf{x}) = \exp\left(-\frac{\|\mathbf{x}\|^2}{2h^2}\right) $$ 其中 $\mathbf{x}$ 表示样本向量,$h$ 是带宽参数控制平滑程度。 #### 3. 迭代更新规则 设当前候选模式为中心 $\mathbf{m}_k=(x_k,y_k,v_x,kv_y,k)$ ,则下一时刻的位置可通过下述公式得到: $$ \mathbf{m}_{k+1}=\sum_{i=1}^{N}w(\mathbf{x}_i-\mathbf{m}_k)\cdot\mathbf{x}_i / \sum_{i=1}^{N}w(\mathbf{x}_i-\mathbf{m}_k). $$ 此过程重复执行直到收敛或者达到最大迭代次数为止。 下面给出一段简单的 MATLAB 示例代码用于演示上述流程: ```matlab function [centerHistory] = meanshift_2D_velocity(dataPoints,h,maxIter,tolerance) % MEANSHIFT_2D_VELOCITY Performs Mean Shift algorithm on given data points. % % Inputs: % dataPoints - Nx2 matrix where each row represents a point (xi,yi). % h - Bandwidth parameter controlling smoothing level. % maxIter - Maximum number of iterations allowed. % tolerance - Convergence criterion based on distance change between centers. numData = size(dataPoints,1); weights = zeros(numData,1); % Preallocate weights array currentCenter = mean(dataPoints); % Initialize center as global mean initially centerHistory(1,:) = currentCenter; for iter = 1:maxIter distancesSquared = sum((bsxfun(@minus,dataPoints,currentCenter)).^2,2); weights(:) = exp(-distancesSquared/(2*h^2)); numerator = weights' * dataPoints; denominator = sum(weights); newCenter = numerator/denominator; if norm(newCenter-currentCenter)<tolerance break; end currentCenter = newCenter; centerHistory(iter+1,:)=newCenter'; end ``` 调用该函数时传入适当参数即可完成一次完整的运行测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值