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< ...
随机推荐
- LINUX内核分析第四周——扒开系统调用的三层皮
LINUX内核分析第四周--扒开系统调用的三层皮 李雪琦 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course ...
- 【loj2064】找相同字符
Portal --> loj2064 Solution 这里是用后缀数组做的版本!(晚点再用Sam写一遍qwq) 首先一个字符串的子串其实就是这个字符串某个后缀的前缀,所以我们有一个十分简单 ...
- 【bzoj4898】商旅
Portal -->bzoj4898 Solution 这题的话..首先答案的形式应该是\(01\)分数规划了 然后比较关键的一步在于,我们需要简化一下交易的过程 具体一点就是,我们 ...
- django 自定义认证
在Django中自定义身份验证 Django 自带的认证系统足够应付大多数情况,但你或许不打算使用现成的认证系统.定制自己的项目的权限系统需要了解哪些一些关键点,即Django中哪些部分是能够扩展或替 ...
- 前端PHP入门-030-文件函数API
bool file_exists ( $指定文件名或者文件路径) 功能:文件是否存在. bool is_readable ( $指定文件名或者文件路径) 功能:文件是否可读 bool is_write ...
- ZOJ 3777 B - Problem Arrangement 状压DP
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:有N(\( N <= 12 \))道题,排顺序 ...
- Java实现链式存储的二叉树
二叉树的定义: 二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的.分别称作这个根的左子树和右子树的二叉树组成. 二叉树的遍历方式主要 ...
- winform MDI子窗口闪动问题(本人测试100%有效解决闪屏问题)
将下面的代码随便放到主窗体的任何一个地方 protected override CreateParams CreateParams //解决MDI闪屏 { get { CreateParams cp ...
- 面试C++失败
到今天,面试已经整整一周,一个offer没有收到,mmp. 无奈,痛苦,迷茫. 以前活的太安逸,太舒适了. 自以为是,异想天开. 要重新振作起来. 要不断学习,保持强大,未来之路才会越走越宽.
- flex布局语法(阮一峰)
Flex 布局教程:语法篇 作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + posi ...