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. 构造无字母数字Webshell

    异或: 补充: A的ascii为65,对应二进制是01000001 <?php echo "1"^"A"; ?> 将"A"和&q ...

  2. 1.2V转5V稳压芯片,低功耗电路

    PW5100具有将低输入电压0.7V-5V之间的范围,升压型,升压到5V的稳定电压输出. 可以使其镍氢电池1.2V稳定输出5V的1.2V转5V芯片. PW5100具有极低的输入静态功耗,1.2V时,应 ...

  3. 阿里云VOD(三)

    一.视频播放器 参考文档:https://help.aliyun.com/document_detail/125570.html?spm=a2c4g.11186623.6.1083.1c53448bl ...

  4. jackson学习之一:基本信息

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. Java 8中字符串拼接新姿势:StringJoiner

    介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾.虽然这也可以在StringBuilder类的帮助 ...

  6. 基于Python的接口自动化-unittest测试框架和ddt数据驱动

    引言 在编写接口自动化用例时,我们一般针对一个接口建立一个.py文件,一条接口测试用例封装为一个函数(方法),但是在批量执行的过程中,如果其中一条出错,后面的用例就无法执行,还有在运行大量的接口测试用 ...

  7. 把Win10变成Mac OS:比任何美化主题都好用的工具

    摘要:把Win10变成Mac OS:比任何美化主题都好用的工具 - 这是一款真正意义上的把Windows变成MacOS的软件,不用更换主题,不用修改Dll,直接是程序接管了Explorer,比任何美化 ...

  8. 借助 AppleScript 一键打开工作空间

    我有个小毛病:同时只能在一个工程里工作. 假如让我开四五个 Webstorm,在工程里 A 改个Bug,然后又到工程 B 里加个需求,再去工程 C 发个版,切来切去一会儿就懵了. 于是有了这个项目:m ...

  9. Bitter.Core系列四:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 查询

    一: 单表模型驱动查询 如下示例代码演示: // 根据ID 查询: var studentquery = db.FindQuery<TStudentInfo>().QueryById(12 ...

  10. pull push 监控指标

    Prometheus 原理介绍 - 知乎 https://zhuanlan.zhihu.com/p/70090800 Prometheus由Go语言编写而成,采用Pull方式获取监控信息,并提供了多维 ...