Codeforces #380(Div.2)B.Spotlights【思维+前缀和优化】

B. Spotlights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.

You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.

A position is good if two conditions hold:

  • there is no actor in the cell the spotlight is placed to;
  • there is at least one actor in the direction the spotlight projects.

Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

Input

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.

The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Output

Print one integer — the number of good positions for placing the spotlight.

Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note

In the first example the following positions are good:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.


题目大意:

给你一个N*M的空间,其中0表示没有人,1表示有人,对应一个好位子以及方向的定义为:

①首先这个位子不能有人。

②其次对应这个位子安排一个照明方向,这个方向上必须有人才行。

让你求一共有多少个这样满足的放置方案。


思路:


1、首先我们O(n*m)暴力枚举出所有的没有人的位子。然后我们如果暴力判断其一行一列的四个方向是否有人的话,时间复杂度会高达:O(n^3)【我们若视n==m的情况下】显然会TLE。


2、那么我们考虑优化:

①设定sum【i】【j】表示第i行,从第一个数加到第j个数的和(前缀和),那么如果我们此时保证(i,j)是没有人的,并且sum【i】【j】>0,那么说明位子(i,j)的左侧有人,那么对应这个位子放置照明方向为左,即是一个可行解。那么同理,如果sum【i】【m】-sum【i】【j】>0,那么说明位子(i,j)的右侧有人,那么对应这个位子放置照明方向为右,即也是一个可行解。

②同理,再设定sum2【i】【j】表示第j列,从第一个数加到第i个数的和,那么同理,如果我们此时保证(i,j)是没有人的,并且sum2【i】【j】>0,那么说明位子(i,j)的上边有人,那么对应这个位子放置照明方向为上,即是一个可行解。那么也是同理,如果sum2【n】【j】-sum2【i】【j】>0,那么说明位子(i,j)的下边有人,那么对应这个位子放置照明方向为下,即也是一个可行解。

③那么此时我们暴力枚举出没有人的位子之后,只需要常数级的操作既可以搞定这个问题了。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int a[1005][1005];
int sum[1005][1005];
int sum2[1005][1005];
int n,m;
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum2,0,sizeof(sum2));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
                sum[i][j]=sum[i][j-1]+a[i][j];
                sum2[i][j]=sum2[i-1][j]+a[i][j];
            }
        }
        int output=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==0)
                {
                    if(sum2[n][j]-sum2[i][j]>0)output++;
                    if(sum2[i][j]>0)output++;
                    if(sum[i][m]-sum[i][j]>0)output++;
                    if(sum[i][j]>0)output++;
                }
            }
        }
        printf("%d\n",output);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值