原题链接: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-][j+mid-] - s[i+mid-][j-] - s[i-][j+mid-] + s[i-][j-];
return t1 == t2;
} int bs(int i, int j)
{
int l = , r = N;
while(l < r)
{
int mid = (l + r) >> ;
if(i + mid - > m || j + mid - > n)
r = mid;
else if(ok(i, j, mid))
l = mid+;
else
r = mid;
}
return (l-) * (l-);
} int main()
{
int ans;
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
scanf("%d", &g[i][j]); for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
s[i][j] = g[i][j] + s[i-][j] + s[i][j-] - s[i-][j-];
ans = ;
for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
if(g[i][j])
ans = max(ans, bs(i, j));
printf("%d\n", ans);
}
return ;
}

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

  1. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  2. 利用tensorflow训练简单的生成对抗网络GAN

    对抗网络是14年Goodfellow Ian在论文Generative Adversarial Nets中提出来的. 原理方面,对抗网络可以简单归纳为一个生成器(generator)和一个判断器(di ...

  3. Lesson 16 The modern city

    What is the author's main argument about the modern city? In the organization of industrial life the ...

  4. UVALive 4867 Maximum Square 贪心

    E - Maximum Square Time Limit:4500MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

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

    A. Maximum Square Ujan decided to make a new wooden roof for the house. He has

  6. 【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 ...

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

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

  9. LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

    题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...

随机推荐

  1. opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子

    http://blog.csdn.net/timidsmile/article/details/8519751 环境: vs2008 + opencv2.1 先看,这两个函数的用法(参考 opencv ...

  2. 000. 规范类的设计(ing)

    1.变量命名规范 变量命名有许多约定俗成的规范,下面的这些规范能有效提高程序的可读性: 标识符要能体现实际含义(顾名思义). 变量名一般用小写字母,如index,不要使用Index或INDEX. 用户 ...

  3. 手脱tElock 0.98b1 -> tE!

    声明: 只为纪录自己的脱壳历程,高手勿喷 第一种:两次内存法 注: ①这是在win7x32系统上运行的脱壳,所以可能地址不同 ②修复的时候用等级三修复,最后修复不了的剪切掉然后转存合一正常运行,已测试 ...

  4. django 配置xamdin遇到的坑

    是在 Django==1.11.7 这个版本下配置的,需要说明的是,不是通过pip install xadmin方式安装的 在github上下载的xadmin源码包,需要在项目的根目录下创建extra ...

  5. 前端PHP入门-029-文件操作-掌握级别

    人生只有三天,活在昨天的人迷惑:活在明天的人等待:活在今天的人最踏实.你永远无法预测意外和明天哪个来得更早,所以,我们能做的,就是尽最大的努力过好今天.请记住:今天永远是昨天死去的人所期待的明天. f ...

  6. CCD与CMOS的区别

    我们在购买相机或是摄像机时,都会看到使用CMOS镜头或是CCD镜头,那么CCD与CMOS是什么意思呢,CCD与CMOS的区别是什么?首先,让我们了解CCD与CMOS的意思. CCDCCD使用一种高感光 ...

  7. 分块+deque维护 Codeforces Round #260 (Div. 1) D. Serega and Fun

    D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  8. 20155117 王震宇 2006-2007-2 《Java程序设计》第四周学习总结

    教材学习内容总结 继承(inheritance) 继承是面向对象软件技术当中的一个概念.继承可以使得子类别具有父类别的各种属性和方法,避免了重复的行为定义.在子类别继承父类别的同时,可以重新定义某些属 ...

  9. JavaScript:自动生成博文目录导航

    感谢 孤傲苍狼 分享了 自动生成博文目录的方法,本文仅作存档使用. 图 1:效果预览 CSS 样式 #TOCbar{ font-size:12px; text-align:left; position ...

  10. oozie与sqoop的简单案例

    1:拷贝模板 2:拷贝hive用的jar包 方式一: 3:编辑job.properties # # Licensed to the Apache Software Foundation (ASF) u ...