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. linux之curl工具

    curl是一个利用URL语法在命令行下工作的文件传输工具,作用是发出网络请求,然后获取数据:它支持文件的上传和下载:支持多种通信协议. 一.查看网页源码 直接在 curl 命令后加上网址,默认会发送 ...

  2. 【Oracle】Oracle中chr()的含义

    oracle中chr含义 CHR(10)和 CHR(13)--在oracle都为换行 chr(32)--表示空格 DECLARE v_a VARCHAR2(255); v_b VARCHAR2(255 ...

  3. Flask+pin

    Flask+SSTI的新火花 记一次buu刷题记和回顾祥云杯被虐出屎的经历.题目:[GYCTF2020]FlaskApp 一 题目初见 朴实无华的页面,一个base64的小程序页面 看到有提示. 我就 ...

  4. paramiko模块简单用法

    最简单最基本的用法 1 #__*__coding:utf-8__*__ 2 import paramiko 3 hostname = '192.168.1.1' 4 username = 'root' ...

  5. 负载均衡和故障转换(Failover)的连接RAC方法

    TAF:Transparent Application Failover,透明的应用切换,即在切换的过程中,用户感知不到.可以实现会话的切换(无法实现事务的切换,即没有提交的事务会回滚),即在不断开连 ...

  6. M8 E147 不可能为条目*确立账户

    今天用BAPI做发票校验, BAPI_INCOMINGINVOICE_CREATE这个函数使用都正常,可是突然就无法做发票检验了 报了个错误,"不可能为条目BOXT TR 确立账户" ...

  7. 【图像处理】RGB Bayer Color分析

    Bayer色彩滤波阵列 拜耳色彩滤波阵列(Bayer Color Filter Array,CFA)是非常有名的彩色图片的数字采集格式.色彩滤波器的模式如上图所示,由一半的G,1/4的R,1/4的B组 ...

  8. Spring Security 实战干货:分布式对象SharedObject

    1. 前言 在上一篇我们对AuthenticationManager的初始化的细节进行了分析,其中里面有一段代码引起了不少同学的注意: ApplicationContext context = htt ...

  9. 解读腾讯敏捷研发核心驱动力 腾讯TAPD TAPD 2020-12-17

    解读腾讯敏捷研发核心驱动力 腾讯TAPD TAPD 2020-12-17

  10. java 本地方法(JNI)

    最近搞了一个调用第三方so库做登录认证的任务,以前对JNI没什么概念,最近学习了 <java核心技术> 本地方法 一章,把自己写的一些例子记录一下. 自己C语言真是渣渣,所以所有的例子都在 ...