CSU 1424 Qz’s Maximum All One Square

  原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1424

  逐渐找到做这种题的感觉了。

  二分法。g[i][j]存储坐标(i, j)的值,s[i][j]存储的值为左上角为起始点(1,1),右下角为(i,
j)的矩形区域内所有值的和,那么:

    s[i][j] = g[i][j] + s[i-1][j] + s[i][j-1] - s[i-1][j-1]

  扫描整个矩形,遇到为“1”的点就将其作为起点,开始二分边长,利用数组s在O(1)的时间复杂度内判断是否满足为由1组成的正方形,不管更新最大值即可。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define N 1005

int g[N][N], s[N][N];

int n, m;

bool ok(int i, int j, int mid)
{
int t1 = mid * mid;
int t2 = s[i+mid-1][j+mid-1] - s[i+mid-1][j-1] - s[i-1][j+mid-1] + s[i-1][j-1];
return t1 == t2;
}

int bs(int i, int j)
{
int l = 1, r = N;
while(l < r)
{
int mid = (l + r) >> 1;
if(i + mid - 1 > m || j + mid - 1 > n)
r = mid;
else if(ok(i, j, mid))
l = mid+1;
else
r = mid;
}
return (l-1) * (l-1);
}

int main()
{
int ans;
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &g[i][j]);

for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
s[i][j] = g[i][j] + s[i-1][j] + s[i][j-1] - s[i-1][j-1];
ans = 0;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
if(g[i][j])
ans = max(ans, bs(i, j));
printf("%d\n", ans);
}
return 0;
}


View
Code

CSU 1424 Qz’s Maximum All One Square,布布扣,bubuko.com

时间: 2024-10-10 07:51:34

CSU 1424 Qz’s Maximum All One Square的相关文章

新概念英语第四册16-30课(转)

Lesson 16  The modern city 现代城市 1.  在工业生活的组织里In the organization of industrial life 这种影响the influence是工厂of the factory 在生理和心理状态上的uponthe physiological and mental state是工人们的 of the workers 已被完全地忽视.has been completely neglected. 在工业生活的组织里In the organiz

Codeforces Round #599 (Div. 2) A. Maximum Square

Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai). Now, Ujan wants to make a square roof. He will first choos

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]

题目链接 Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square. Example 1: Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,

Baozi Leetcode solution 1292.&#160;Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Problem Statement Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square. Example 1: Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2

221 Maximum Square

这道题有两个思路, 一是沿用085的maximum rectangle的思路, 稍作改进即可, 代码如下, 这个方法运行192ms class Solution: # @param {character[][]} matrix # @return {integer} def maximalSquare(self, matrix): ans = 0 for i in range(0,len(matrix)): for j in range(0, len(matrix[0])): if i == 0

【codeforces 19/11/06 div2】A. Maximum Square

1 #include<iostream> 2 #include<algorithm> 3 #include<map> 4 using namespace std; 5 6 map<int, int>cnt; 7 8 int main() 9 { 10 int T; 11 cin >> T; 12 while (T--) 13 { 14 cnt.clear(); 15 int n; 16 cin >> n; 17 for (int i

UVa 108 - Maximum Sum(最大连续子序列)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=44  Maximum Sum  Background A problem that is simple to solve in one dimension is often much more difficult to solve in more th

URAL 1146. Maximum Sum(求最大子矩阵和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1146 1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is

[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长: