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< ...
随机推荐
- Amphetamine的cf日记
之前挂上的 今天填坑 2018.2.14 #462 A 给两个集合,B分别可以从一个集合中选一个数,B想乘积最大,A想最小,A可以删除一个第一个集合中的元素,问最小能达到多少. 这题..水死啦.我居然 ...
- 流媒体协议之RTP详解20170921
1.RTP介绍 实时传输协议RTP(Real-time Transport Protocol)是一个网络传输协议,它是由IETF的多媒体传输工作小组1996年在RFC 1889中公布的,后在RFC35 ...
- ORACLE获取某个时间段之间的月份列表
返回1-31,或者1-12,或者某个 select rownum from dual connect by rownum<31 就是connect by http://marcospri ...
- Nginx多个配置文件共用location配置
一.应用情况 很多时候我们在一台服务器上部署了不止 一个项目,我们通过Nginx来代理,为了方便管理往往会将各个项目的配置分开写到不同的配置文件中,如: 在nginx.conf 文件中加上 incl ...
- mysql cpu 占用高
vi /etc/my.cnf [mysqld]tmp_table_size=200M mysql> show global status like ‘created_tmp%‘; +—————— ...
- svnserver配置详解
svnserve是SVN自带的一个轻型服务器,客户端通过使用以svn://或svn+ssh://为前缀的URL来访问svnserve服务器,实现远程访问SVN版本库. svnserve可以通过配置文件 ...
- JavaScript Promises 实现框架一览及对比
In my previous post in Danish I looked at how to perform asynchronous calls by using promises. Now t ...
- DHTML Object Model&DHTML&DOM
DHTML Object Model:DHTML对象模型,利用DHTML Object Model可以单独操作页面上的对象,每个HTML标记通过它的ID和NAME属性被操纵,每个对象都具有自己的属性. ...
- 【SRM20】数学场
第一题 n个m位二进制,求异或值域总和. [题解]异或值域--->使用线性基,解决去重问题. m位二进制--->拆位,每位根据01数量可以用组合数快速统计总和. #include<c ...
- 【BZOJ】1798: [Ahoi2009]Seq 维护序列seq 线段树多标记(区间加+区间乘)
[题意]给定序列,支持区间加和区间乘,查询区间和取模.n<=10^5. [算法]线段树 [题解]线段树多重标记要考虑标记与标记之间的相互影响. 对于sum*b+a,+c直接加上即可. *c后就是 ...