【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area

时间限制: 1 Sec  内存限制: 128 MB
提交: 146  解决: 54
[提交] [状态] [命题人:admin]

题目描述

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. 

 

输入

The first line is a positive integer T <= 20 representing the number of test cases. For each case, the input is formatted as follows. 


Note: there is at least one non-forest patch in each test case. 

 

输出

There are T lines in the output. Each line is the number of rows in the largest allowed square region for each case. 

 

样例输入

复制样例数据

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

样例输出

9
7

题目大意:

先输入一个整数t,代表有t组测试样例,对于每组测试样例,先输入两个整数n,m,然后输入n行m列的0,1矩阵,问在这个0,1矩阵中选取一个正方形区域,使得这个正方形区域内有且仅有一个1,问这样的正方形边长最大是多少。

解题思路:

可以通过二分正方形的边长,对于每次二分的mid,可以先预处理一下0,1矩阵,用sum[i][j]代表第i行第j列的元素的左上角的全部元素和,因此根据容斥思想可得出推导公式sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[i][j],若一个正方形区域内仅包含一个1,则证明这个正方形区域的面积为1,因此对于每次二分出的正方形的边长,可以通过遍历一下这个矩阵,判断此边长是否会是的某个正方形区域的面积为1.

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[1200][1200];
int sum[1200][1200];
int n,m;
bool judge(int x) {rep(i,1,n-x) {rep(j,1,m-x) {int t=sum[i+x][j+x]-sum[i+x][j-1]-sum[i-1][j+x]+sum[i-1][j-1];if(t==1) return true;}}return false;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {scanf("%d %d",&n,&m);rep(i,1,n) {rep(j,1,m) {arr[i][j]=sum[i][j]=0;scanf("%d",&arr[i][j]);sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+arr[i][j];}}int l=1,r=min(n,m);while(r-l>1) {int mid=(r+l)>>1;if(judge(mid)) l=mid;else r=mid;}printf("%d\n",l+1);}return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536292.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…

大凤号装甲空母【找规律+矩阵快速幂】

大凤号装甲空母 时间限制: 1 Sec 内存限制: 128 MB 提交: 108 解决: 15 [提交] [状态] [命题人:admin] 题目描述 大凤号航空母舰很喜欢算术。 它&#xff0c;是旧日本海军中最为先进的航空母舰。 它&#xff0c;是旧日本海军中最为短命的航空母舰。 同时&#xff0c;她还是最平…

Degree Sequence of Graph G【模拟】

Degree Sequence of Graph G 时间限制: 1 Sec 内存限制: 128 MB 提交: 362 解决: 92 [提交] [状态] [命题人:admin] 题目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

【动态规划】魔法石矿

【动态规划】魔法石矿 时间限制: 1 Sec 内存限制: 64 MB 提交: 116 解决: 27 [提交] [状态] [命题人:admin] 题目描述 为了找到回家的路&#xff0c;张琪曼施展魔法&#xff0c;从高维空间召唤出了一种叫作“读者”的生物&#xff0c;据说“读者”这种生物无所不能&#xff0c;…

简单类及成员实例【C#】

简单类及成员实例&#xff08;C#&#xff09; 题目描述 简单类及成员实例。定义了如下图所示类Student&#xff0c;根据下图和给出代码&#xff0c;补写缺失的代码。 using System; namespace sample{ class Student { public string studentid;//学号 p…

c#随机数的产生与输出【C#】

c#随机数的产生与输出 题目描述 编写一个实例方法Method01。该方法使用Random类随机产生n个3位数字&#xff08;如636&#xff09;的随机正整数&#xff0c;并把产生的随机数存入数组中并输出该数组int num Convert.ToInt32(Console.ReadLine()); using System; using System…

C# teacher类【C#】

C# teacher类 题目描述 定义一个教师类Teacher&#xff0c;具体要求如下&#xff1a; 1、私有字段工号no&#xff08;string&#xff09;、姓名name&#xff08;string&#xff09;、出生日期birthday&#xff08;DateTime&#xff09;、性别sex&#xff08;SexFlag&#xff0…

接口实例(C#,IShape)【C#】

接口实例&#xff08;C#,IShape&#xff09; 题目描述 接口实例。接口和类如下图所示&#xff0c;根据给出代码&#xff0c;补写缺失的代码&#xff0c;然后在Program类的静态Main方法中验证所实现的类。 using System; namespace Myinterface { public interface IShape…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…

Problem B: 求各位数字之和

#include <stdio.h> #include <stdlib.h> int main() { int n,sum0,m; while(~scanf("%d",&n)) { while(n>0) { mn%10; nn/10; summ; } printf("%d\n",sum); sum0; } return 0; }

Problem C: 判断字符串是否为回文

#include <stdio.h> #include <stdlib.h> int main() { int i,j,n; char str[10]; gets(str); nstrlen(str); for(i0,jn-1;i<j;i,j--) { if(str[i]!str[j]) { printf("No\n"); break; } } if(i>j)printf("Yes\n"); return 0; }

Problem A: 童年生活二三事

斐波那契数列:F(n)F(n-1)F(n-2) #include <stdio.h> #include <stdlib.h> int f(int n) {int b;if(n1)b1;if(n2)b2;if(n>2)bf(n-1)f(n-2);return b; }int main() {int a,n;while(~scanf("%d",&n)&&n!0){af(n);printf("%d\n",a…

Problem C: 01字串

#include <stdio.h> #include <stdlib.h>int main() {int i,j,n0,m0;char a[129][8];for(i0;i<128;i){for(j0;j<7;j){a[i][j]n%2;nn/2;}m;nm;}for(i0;i<128;i){for(j6;j>0;j--)printf("%d",a[i][j]);printf("\n");}return 0; }

汉诺塔III

#include <stdio.h> #include <stdlib.h> int main() {int fx(int );int n,m;while(~scanf("%d",&n)){mfx(n);printf("%d\n",m);}return 0; } int fx(int n) {int m;if(n1)m2;if(n>1)m3*fx(n-1)2;return m; }

骨牌铺方格

骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 46495 Accepted Submission(s): 22470 Problem Description在2n的一个长方形方格中,用一个1 2的骨牌铺满方格,输入n ,输出铺放方案的总数.例如n3时…

递归思想完成n皇后问题

已经很长时间不敲代码了&#xff0c;感觉自己越来与颓废&#xff0c;所以现在又想做回一名苦逼的程序员&#xff0c;开启自己的代码之路。 我是根据视频敲的&#xff0c;没有题目&#xff0c;先看个四皇后问题吧。 所谓4皇后问题就是求解如何在44的棋盘上无冲突的摆放4个皇后棋…

virtualbox的USB识别

VirtualBox识别USB教程 作者&#xff1a;Vincent June 13, 2017 在Virtualbox虚拟机配置面板中打开USB设备选项&#xff0c;分别勾选上“启动USB控制器”“启用usb2.0控制器”选项&#xff0c;如果有错误去https://www.virtualbox.org/wiki/Downloads 下载相应版本的插件包&a…

修改win10我的文档下载等移动别处

win10移动我的文档&#xff0c;下载等到其他盘符办法 解决办法 1.选择我的文档&#xff0c;鼠标右键选择属性&#xff0c;在工具栏选择位置&#xff0c;然后选择想移动到哪里的盘符即可&#xff0c;如图&#xff1a;2.操作完后选择应用->确定&#xff0c;就这么简单。