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< ...
随机推荐
- 【bzoj3105】新Nim游戏
Portal--> bzoj3105 新Nim游戏 Solution 转化一下问题 首先看一下原来的Nim游戏,先手必胜的条件是:每堆数量的异或和不为\(0\) 所以在新的游戏中,如果要保证自己 ...
- 手把手教你如何玩转Activiti工作流
手把手教你如何玩转Activiti工作流 置顶 2018年01月30日 19:51:36 Cs_hnu_scw 阅读数:24023 版权声明:本文为博主原创文章,未经博主允许不得转载. https ...
- 洛谷P1199 三国游戏
题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之 ...
- Filter 介绍
Filter 可认为是 Servlet 的一种加强版,它主要用于对用户请求进行预处理,也可以对 HttpServletResponse 进行后处理,是个典型的处理链. Filter 也可对用户请求生成 ...
- 「Linux」centos7安装python
•安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqli ...
- 修改Tomcat端口
1.自己的tomcat修改端口号,在tomcat解压缩目录中conf目录下又一个server.xml,将HTTP协议的端口修改为自己想要的即可 <Connector port="808 ...
- Xamarin Error:Could not find android.jar for API Level 23.
背景:打开别人Xamarin项目找不到android.jar文件 报错: 解决方案1:工具——Android——Amdroid SDK 管理器…出现以下窗口(根据需要的[API级别])勾选相应的Pla ...
- JAVA 日期处理工具类 DateUtils
package com.genlot.common.utils; import java.sql.Timestamp;import java.text.ParseException;import ja ...
- Codechef Course Selection
Home » Practice(Hard) » Course Selection Course Selection Problem Code: RINSubmit https://www.codech ...
- [洛谷P1709] [USACO5.5]隐藏口令Hidden Password
洛谷题目链接:[USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N< ...