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. C++中的extern“C”

    首先引入extern"C"的官方解释 extern "C" is meant to be recognized by a C++ compiler and to ...

  2. HashMap为什么效率高?来看看这个小demo

    一.前情回顾:在程序中有时候需要存放对象,容器应运而生.容器分为集合和Map.集合在这里不说,说说Map.Map在英语中是地图的意思,这个名字真是起的好,可以让人顾名思义.Map,就是存放键值对的结构 ...

  3. docker 数据卷的挂载和使用

    容器之间的数据共享技术, Docker容器产生的数据同步到本地 卷技术 --> 目录挂载, 将容器内的目录挂载到服务器上 使用命令来挂载 -v # 可以挂载多个目录 docker run -it ...

  4. LeetCode938. 二叉搜索树的范围和

    题目 1 class Solution { 2 public: 3 int sum = 0; 4 int rangeSumBST(TreeNode* root, int low, int high) ...

  5. C语言中左值和右值的区别(C语言学习笔记)

    重要的内容要重复强调: C语言的术语Ivalue指用于识别或定位一个存储位置的标识符.( 注意:左值同时还必须是可改变的) 其实rvalue的发明完全是为了搭配lvalue , rvalue你可以理解 ...

  6. ctfhub技能树—文件上传—.htaccess

    首先介绍一下.htaccess(来自百度百科) .htaccess文件(或者"分布式配置文件"),全称是Hypertext Access(超文本入口).提供了针对目录改变配置的方法 ...

  7. Python编程小技巧(一)

    在使用Tkinter编写代码的时候,有时候会忘记某个组件的参数是什么或者忘记某个参数怎么拼写的,此时可以通过如下方式查询组件的参数列表,以按钮组件为例: 1 # -*- coding:utf-8 -* ...

  8. 文件的上传/下载+在线游览(转化html)--不需要在线插件//自己写的小方法

    1 /// <summary> 2 /// 文件上传下载帮助类 3 /// </summary> 4 public static class FileHelper 5 { 6 ...

  9. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  10. [BJOI2016]水晶 做题心得

    [BJOI2016]水晶 做题心得 这是一个写了我两小时的傻逼题.写这个题浪费了一堆时间后,我才意识到我码力又不行了.于是整理起了实现技巧,开始练码力. 思路 不难.首先把 \((x,y,z)\) 变 ...