king's trouble II SCU - 4488
Time Limit: 1000 MS Memory Limit: 131072 K
Description
Long time ago, a king occupied a vast territory.
Now there is a problem that he worried that he want to choose a largest square of his territory to build a palace.
Can you help him?
For simplicity, we use a matrix to represent the territory as follows:
0 0 0 0 0
0 1 0 1 0
1 1 1 1 0
0 1 1 0 0
0 0 1 0 0
Every single number in the matrix represents a piece of land, which is a 1*1 square
1 represents that this land has been occupied
0 represents not
Obviously the side-length of the largest square is 2
Input
The first line of the input contains a single integer t (1≤t≤5) — the number of cases.
For each case
The first line has two integers N and M representing the length and width of the matrix
Then M lines follows to describe the matrix
1≤N,M≤1000
Output
For each case output the the side-length of the largest square
Sample Input
2
5 5
0 0 0 0 0
0 1 0 1 0
1 1 0 1 0
0 1 1 0 0
0 0 1 0 0
5 5
0 0 0 0 0
0 1 0 1 0
1 1 1 1 0
0 1 1 0 0
0 0 1 0 0
Sample Output
1
2
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=1e9+7;
const int maxn=1010;
typedef long long ll;
//单调栈
//先做 poj 2559后再看单调栈可能会好理解一点
int a[1010][1010];
struct node
{
int idx,num;//下标和延伸值
};
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j])
a[i][j]+=a[i-1][j];
}
a[i][m+1]=-1;
}
int ans=-1;
for(int i=1; i<=n; i++)
{
int top=0;
node vis[1010];
for(int j=1; j<=m+1; j++)
{
if(top==0)
{
vis[top].idx=j,vis[top++].num=1;
}
else
{
printf("top=%d\n",top);
int v=vis[top-1].idx,cont=0;
while(top>0&&a[i][v]>=a[i][j])
{
cont+=vis[top-1].num;
if(cont>=a[i][v])
ans=max(ans,a[i][v]);
top--;
v=vis[top-1].idx;
}
vis[top].idx=j,vis[top++].num=cont+1;
printf("vis[top].idx=%d vis[top].num=%d\n",vis[top-1].idx,vis[top-1].num);
}
}
}
printf("%d\n",ans);
}
}
//暴力
//先把每一列累加,类似于条形图,然后从每一行的开头开始遍历到每一行结束,
//(不过是因为这一题的后台数据水,
//暴力才能过,所以如果数据范围太大不推荐暴力)
int mp[maxn][maxn];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,x;
memset(mp,0,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
scanf("%d",&x);
if(x&&mp[i-1][j])
mp[i][j]=mp[i-1][j]+1;
else
mp[i][j]=x;
}
}
int maxx=0;
int k=1;//代表此时最大的面积
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(mp[i][j]>=k)
{
int p=0;
for(; mp[i][p+j]>=k; p++);//每一行从头遍历到尾
if(p>=k)
{
maxx=k;
k++;
}
}
}
}
printf("%d\n",maxx);
}
return 0;
}
//动规
//以每一个点作为正方形的右上角,
//每次取四个角的最小值加一即为此时的最大面积(因为本题规定的每个矩形面积为1)
//自己画图可以理解的
int a[maxn][maxn];
int dp[maxn][maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
scanf("%d",&a[i][j]);
int ans=0;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
{
if(a[i][j]==1)
{
dp[i][j]=min(dp[i][j-1],min(dp[i-1][j-1],dp[i-1][j]))+1;
}
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
}
king's trouble II SCU - 4488的更多相关文章
- ZOJ 2058 The Archaeologist's Trouble II(贪心+模拟)
[题目大意] 一个n高的塔,由@ * ?三种字符组成.每行相邻两个字符不能相邻. '?' 表示未确定是 '@' 还是 '*' . 求'@' 可能出现的最多和最少次数. [分析] 在可以填的情况下 先填 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- stochastic noise and deterministic noise
在机器学习中,导致overfitting的原因之一是noise,这个noise可以分为两种,即stochastic noise,随机噪声来自数据产生过程,比如测量误差等,和deterministic ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- PID控制器(比例-积分-微分控制器)- II
Table of Contents Practical Process Control Proven Methods and Best Practices for Automatic PID Cont ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- Saving Tang Monk II(bfs+优先队列)
Saving Tang Monk II https://hihocoder.com/problemset/problem/1828 时间限制:1000ms 单点时限:1000ms 内存限制:256MB ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
随机推荐
- k8s+docker学习连接汇总
http://guide.daocloud.io/dcs/docker-9153982.html http://www.dczou.com/viemall/802.html https://wangl ...
- 【BZOJ】1875: [SDOI2009]HH去散步 矩阵快速幂
[题意]给定n个点m边的无向图,求A到B恰好经过t条边的路径数,路径须满足每条边都和前一条边不同.n<=20,m<=60,t<=2^30. [算法]矩阵快速幂 [题解]将图的邻接矩阵 ...
- 小程序Openid 获取,服务器 encryptedData 解密 遇到的坑
获取客户 openId 和 unionId 需要以下步骤(都为必须步骤) 1.从验证从客户端传上来code, 获取sessionKey (需要配合小程序appid ,secret 发送到微信服务器) ...
- 使用vscode实现git同步
用了git最方便的就是项目同步管理,回到家打开vscode只需要点击一下pull就能全部同步过来.是不是很方便....毕竟之前我都是拿u盘拷贝回家或者存到云盘再下载下来.. 我这里之前用的是国内的 ...
- 用户空间与内核空间数据交换的方式(9)------netlink【转】
转自:http://www.cnblogs.com/hoys/archive/2011/04/10/2011722.html Netlink 是一种特殊的 socket,它是 Linux 所特有的,类 ...
- Ubuntu命令设置ip网关dns
本文系转载,介绍Ubuntu如何设置IP和网络来连接网络 如果是在虚拟机中使用Ubuntu,那么设置之前请先参照我的上一遍文章虚拟机Net方式设置连接外网中的网络设置部分,先设置好主机的网络,然后配置 ...
- pycharm双击无响应,打不开问题解决办法
之前好好的pycharm,突然双击打不开了,怎么办? 亲测有效方案: 第一步:进入如下路径,找到cmd.exe,右键选择“以管理员身份运行”: 第二步:在打开的cmd窗口中,输入 netsh wins ...
- 集合框架之Map学习
Map接口的实现类有HashTable.HashMap.TreeMap等,文章学习整理了“ Map和HashMap的使用方法”. /** * Map和HashMap的使用方法 */public sta ...
- linux 实现自动创建ftp用户并创建文件夹
创建一个 createuser.sh的脚本文件 #!/bin/sh #传入的文件名 name=$1 #创建该用户所对应的ftp文件夹 /srv/ftp是我的ftp服务器的根目录 mkdir /sr ...
- 字典对象的 Pythonic 用法(上篇)
字典对象在Python中作为最常用的数据结构之一,和数字.字符串.列表.元组并列为5大基本数据结构,字典中的元素通过键来存取,而非像列表一样通过偏移存取.笔者总结了字典的一些常用Pyhonic用法,这 ...