Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 605    Accepted Submission(s): 127


Problem Description
It's really a simple problem. 

Given a "01" matrix with size by n*n (the matrix size is n*n and only contain "0" or "1" in each grid), please count the number of "1" matrix with size by k*k (the matrix size is k*k and only contain "1" in each grid).
 

Input
There is an integer T (0 < T <=50) in the first line, indicating the case number.

Each test case begins with two numbers n and m (0<n, m<=1000), specifying the size of matrix and the query number.

Then n lines follow and each line contains n chars ("0" or "1").

Then m lines follow, each lines contains a number k (0<k<=n).
 

Output
For each query, output the number of "1" matrix with size by k*k.
 

Sample Input

2
2 2
01
00
1
2
3 3
010
111
111
1
2
2
 

Sample Output

1
0
7
2
2
 

这题用二维树状数组T了,改成用dp做,371ms过了。我们枚举每一个点作为右下角的点对各部分的贡献,那么就可以dp了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define pi acos(-1.0)
#define maxn 1006
int n;
char s[maxn][maxn];
int b[maxn],dp[maxn][maxn],heng[maxn][maxn],shu[maxn][maxn];
int lowbit(int x){
return x&(-x);
}
void update(int pos,int num)
{
while(pos<=maxn){
b[pos]+=num;pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int m,i,j,T,d;
int l,r,mid;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
for(i=1;i<=n;i++){
b[i]=0;
for(j=1;j<=n;j++){
shu[i][j]=0;
heng[i][j]=0;
}
}
/*
memset(b,0,sizeof(b));
memset(shu,0,sizeof(shu));
memset(heng,0,sizeof(heng));
memset(dp,0,sizeof(dp));
*/
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==1 && j==1){
if(s[i][j]=='1'){
dp[i][j]=shu[i][j]=heng[i][j]=1;
update(1,1);
update(2,-1);
}
else{
dp[i][j]=shu[i][j]=heng[i][j]=0;
}
continue;
}
if(s[i][j]=='0'){
dp[i][j]=shu[i][j]=heng[i][j]=0;
continue;
}
if(i==1){
dp[i][j]=1;
heng[i][j]=heng[i][j-1]+1;
shu[i][j]=1;
update(1,1);
update(dp[i][j]+1,-1); }
else if(j==1){
dp[i][j]=1;
heng[i][j]=1;
shu[i][j]=shu[i-1][j]+1;
update(1,1);
update(dp[i][j]+1,-1);
}
else{
heng[i][j]=heng[i][j-1]+1;
shu[i][j]=shu[i-1][j]+1;
int len=min(heng[i][j],shu[i][j]);
len=min(len,dp[i-1][j-1]+1);
dp[i][j]=len;
update(1,1);
update(len+1,-1);
}
}
}
for(i=1;i<=m;i++){
scanf("%d",&d);
printf("%d\n",getsum(d));
}
}
return 0;
}

hdu 01 Matrix的更多相关文章

  1. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

  2. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

  4. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  5. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  6. leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings

    542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. hdu 0-1背包

    题目地址http://acm.hdu.edu.cn/showproblem.php?pid=2602 #include <stdio.h> #include <string.h> ...

  9. hdu 2686 Matrix 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...

随机推荐

  1. 【Redis3.0.x】数据类型

    Redis3.0.x 数据类型 五大数据类型 String(字符串) string 是 redis 最基本的类型.可以理解成与 Memcached 一模一样的类型,一个 key 对应一个 value. ...

  2. 开源:AspNetCore 应用程序热更新升级工具(全网第一份公开的解决方案)

    1:下载.开源.使用教程 下载地址:Github 下载 .其它下载 开源地址:https://github.com/cyq1162/AspNetCoreUpdater 使用教程: 解压AspNetCo ...

  3. 实现strStr

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. SQL LEN()函数用法

    含义: LEN 函数返回文本字段中值的长度. 返回字符表达式中的字符数 SQL LEN() 语法 SELECT LEN(column_name) FROM table_name 举例: 1.LEN对相 ...

  5. 音视频入门-20-BMP、PNG、JPG、GIF静态图生成GIF动态图

    * 音视频入门文章目录 * 静态图 -> 动态图 前面 [18-手动生成一张GIF图片] 和 [19-使用giflib处理GIF图片] 生成的 GIF 每一帧都是一个颜色,平时用到的 GIF 每 ...

  6. pg_rman的安装与使用

    1.下载对应数据库版本及操作系统的pg_rman源码 https://github.com/ossc-db/pg_rman/releases 本例使用的是centos6.9+pg10,因此下载的是pg ...

  7. 前端知识(一)04 Vue.js入门-谷粒学院

    目录 一.介绍 1.Vue.js 是什么 2.初识Vue.js 二.基本语法 1.基本数据渲染和指令 2.双向数据绑定 3.事件 4.修饰符 5.条件渲染 6.列表渲染 7.实例生命周期 一.介绍 1 ...

  8. Python爬虫:数据分析小能手:JSON库的用法

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 给大家推荐一个Python交流的q裙,大家在学习遇到了什么问题都可以进群一起交流,大家 ...

  9. 前端面试之JavaScript中的闭包!

    前端面试之JavaScript中的闭包! 闭包 闭包( closure )指有权访问另一个函数作用域中变量的函数. ----- JavaScript 高级程序设计 闭包其实可以理解为是一个函数 简单理 ...

  10. Connections could not be acquired from the underlying database!

    Connections could not be acquired from the underlying database! 报错截图: 报错内容: Exception in thread &quo ...