首页 > 其他分享 >P1387 最大正方形 题解

P1387 最大正方形 题解

时间:2023-07-23 22:55:27浏览次数:37  
标签:int 题解 P1387 mid 正方形 mp check

注意细节

通过二维前缀和判定矩形内是否全为1,计算和等于长度的平方就判断为是

复杂度\(\Theta (n^2\log{n})\)

#include <bits/stdc++.h>

#define N (int)(105)

using namespace std;

int mp[N][N];
int s[N][N];
int n,m;

bool check(int lenth)
{
	for(int i = 1;i + lenth - 1 <= n;i++)
	{
		for(int j = 1;j + lenth - 1 <= m;j++)
		{
			if(s[i + lenth - 1][j + lenth - 1] - s[i-1][j + lenth - 1] - s[i + lenth - 1][j-1] + s[i-1][j-1] == lenth * lenth)//
			{
				return true;
			}
		}
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i++)
	{
		for(int j =1;j <= m;j++)
		{
			cin >> mp[i][j];
			s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + mp[i][j];
		}
	}
	
	int l = 1, r = min(n,m);
	while(l < r)
	{
		int mid = (l + r) / 2 + 1;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}
	cout << l;
	return 0;
}

标签:int,题解,P1387,mid,正方形,mp,check
From: https://www.cnblogs.com/l-cacherr/p/17576127.html

相关文章

  • Codeforces Round 886 (Div. 4) 题解 A - H
    A.ToMyCritics题目大意给定三个数,你可以挑俩数加起来,问这仨数有没有可能加起来大于等于\(10\).解题思路我们找其中最大的两个数相加与\(10\)比较即可。ACCode#include<iostream>#include<algorithm>#include<cstring>#defineendl'\n'#defineiosios::sync......
  • Codeforces Round 886 (Div. 4) 全题题解
    我关注的人中正式参与比赛排名公示:#Who=Penalty*ABCDEFGH1(980)Chen_Jinhui7381\(\color{#00CC00}{\textbf{+}}\)00:05\(\color{#00CC00}{\textbf{+1}}\)00:17\(\color{#00CC00}{\textbf{+}}\)00:19\(\color{#00CC00}{\textbf{+}}\)01:08......
  • 无角正方形的绘制用python
    无角正方形的绘制用Python引言无角正方形是一种特殊的几何形状,它没有任何角度,所有的边长都相等。在计算机图形学中,我们可以使用Python来绘制无角正方形。本文将介绍如何使用Python的turtle模块来绘制无角正方形,以及一些有关无角正方形的背景知识。背景知识在绘制无角正方形之前......
  • 题解 P9474 [yLOI2022] 长安幻世绘
    看到极差,不难想到双指针。显然,如果\([l,r]\)的位置都被覆盖,那么其中最多可以选\(\lceil\frac{r-l+1}{2}\rceil\)个数。我们先将所有数离散化,排序,双指针卡取值范围。set里面存pair类型变量,表示覆盖的区间。每次将值为\(r\)的数的位置加入,在set中二分到与它相邻的区......
  • 题解链接
    积跬步,至千里tag:二分洛谷P1314聪明的质检员洛谷P1024一元三次方程求解tag:分块CF785EAntonandPermutationtag:贪心UVA1335/洛谷P4409皇帝的烦恼题解tag:倍增+贪心P4155、P6902——一类区间覆盖问题tag:二进制洛谷P7617KOMPIĆI的一个小tricktag:排列组合+......
  • 题解:【ICPC WF 2021 H】 Prehistoric Programs
    题目链接#include<bits/stdc++.h>#defineldlongdouble#defineuiunsignedint#defineullunsignedlonglong#defineintlonglong#defineebemplace_back#definepbpop_back#defineinsinsert#definempmake_pair#definepiipair<int,int>#def......
  • cf 题解
    MihaiandSlavicwerelookingatagroupof$n$frogs,numberedfrom$1$to$n$,allinitiallylocatedatpoint$0$.Frog$i$hasahoplengthof$a_i$.Eachsecond,frog$i$hops$a_i$unitsforward.Beforeanyfrogsstarthopping,SlavicandMihaicanp......
  • P7074 [CSP-J2020] 方格取数 题解
    题目:题目描述设有n*m 的方格图,每个方格中都有一个整数。现有一只小熊,想从图的左上角走到右下角,每一步只能向上、向下或向右走一格,并且不能重复经过已经走过的方格,也不能走出边界。小熊会取走所有经过的方格中的整数,求它能取到的整数之和的最大值。输入格式第一行有两个整......
  • AtCoder Beginner Contest 311 A-E题解
    A-FirstABC题意给一个长度为N的仅由ABC三个字符组成的字符串S,问S中ABC三个字符第一次出现的位置的最大值。题解使用map<char,bool>判重,记录当前不同的字符串的个数cnt,当cnt等于3时,输出此时的下标+1作为答案。Code#include<bits/stdc++.h>usingnamespacestd;usingll......
  • 第二次比赛部分题解
    P7060[NWRRC2014]AlarmClock  #include<bits/stdc++.h>usingnamespacestd;intmain(){intn;cin>>n;intarr[10]={6,2,5,5,4,5,6,3,7,6};boolcheck=false;//对于时间ab:cdfor(inta=0;a<=2;a++){//a最多可以到2(因为最大为23......