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-][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的更多相关文章
- Must practice programming questions in all languages
To master any programming languages, you need to definitely solve/practice the below-listed problems ...
- 利用tensorflow训练简单的生成对抗网络GAN
对抗网络是14年Goodfellow Ian在论文Generative Adversarial Nets中提出来的. 原理方面,对抗网络可以简单归纳为一个生成器(generator)和一个判断器(di ...
- Lesson 16 The modern city
What is the author's main argument about the modern city? In the organization of industrial life the ...
- UVALive 4867 Maximum Square 贪心
E - Maximum Square Time Limit:4500MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- Codeforces Round #599 (Div. 2) A. Maximum Square 水题
A. Maximum Square Ujan decided to make a new wooden roof for the house. He has
- 【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 ...
- 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 ...
- 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 ...
- 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< ...
随机推荐
- 同时装了Python3和Python2,怎么用pip?
问题:同时装了Python3和Python2,怎么用pip? Ubuntu13.04, 系统内同时装了Python3.3 和 2.7 用sudo apt-get install python-pip ...
- Linux之进程通信20160720
好久没更新了,今天主要说一下Linux的进程通信,后续Linux方面的更新应该会变缓,因为最近在看Java和安卓方面的知识,后续会根据学习成果不断分享更新Java和安卓的方面的知识~ Linux进程通 ...
- shell中的引用
By francis_hao Mar 31,2018 引用,用来移除某个字符或单词对于shell的特殊含义 每个元字符对于shell都有特殊含义,可分割单词,如果想使用其本身的含义就需要用到 ...
- 000. 规范类的设计(ing)
1.变量命名规范 变量命名有许多约定俗成的规范,下面的这些规范能有效提高程序的可读性: 标识符要能体现实际含义(顾名思义). 变量名一般用小写字母,如index,不要使用Index或INDEX. 用户 ...
- vue使用插件 使用库
用插件1.引用import VueResource from 'vue-resource'2.使用Vue.use(VueResource); 用库(bootstrap alertify )1.引入: ...
- MSBuild问题积累
我想要当属性ConfigurationType定义为StaticLibrary时,将其重新定义为StaticLibrary,按照以下来做,实现不了. <ConfigurationType> ...
- Eclipse 导入Hadoop 2.6.0 源码
1. 首先前往 官网(Hadoop 2.6 下载地址)上下载Hadoop的源码文件,并解压 2. 事先请确定已经安装好jdk以及maven(Maven安装教程 这是其他人写的一篇博文,保存profil ...
- Jquery validate验证表单时多个name相同的元素只验证第一个的问题
下面搜集了五种方法,主要还是前两个提供了解决方案,第三种需要修改jQuery源码: 修复jquery.validate插件中name属性相同(如name='a[]')时验证的bug 使用jquery. ...
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- bootstrap下laydate样式错乱问题
查看发现bs使用了 * {box-sizing:border-box;}重置了盒子模型 那么我们再把它重置回来,在样式中加入以下代码 .laydate_box, .laydate_box * { bo ...