L - Largest Allowed Area Gym - 102091L 题解

L - 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.

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
output:
9
7
题意:给定一个由0 1 组成的n×m的数组,求所有数字之和小于等于一的最大的正方形的边长
思路:用二维前缀和+二分
代码:#include <bits/stdc++.h>
using namespace std;
注意:该题目输入需要用读入挂//也就是read函数,否则会超时
template
inline void read(T&x){
x=0;int f=1;char ch = getchar();
while(ch<‘0’ ||ch>‘9’){ if(ch==’-’)f=-1;ch=getchar(); }
while(ch>=‘0’ && ch<=‘9’){ x=x10+ch-‘0’; ch=getchar(); }
x
=f;
}
#define rep(i,s,t) for(int i=s;i<=t;i++)
const int N = 1e3+5;
int n,m,arr[N][N],sum[N][N];

bool check(int x){
rep(i,1,n) rep(j,1,m) {
if(i<x || j<x)continue;
int num=sum[i][j]-sum[i][j-x]-sum[i-x][j]+sum[i-x][j-x]; //得到任意子矩阵的值
if(num<=1)return true;
}
return false;
}
int main(){
int T;read(T);while(T–){
memset(arr,0,sizeof(arr));
memset(sum,0,sizeof(sum));
read(n);read(m);
rep(i,1,n) rep(j,1,m) {
read(arr[i][j]);
sum[i][j] = arr[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]; //维护二维前缀和
}
int l=1,r=1e3+10,ans=0;
while(l<=r){ //二分枚举正方形边长
int mid=l+r>>1;
if(check(mid))ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
}
上述代码摘自:https://www.cnblogs.com/00isok/p/10503851.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值