• Spotlights


    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 good positions in this example.

    分析:预处理前缀和,模拟;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <unordered_map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define intxt freopen("in.txt","r",stdin)
    const int maxn=1e3+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,a[maxn][maxn],b[maxn][maxn];
    ll ans;
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)rep(j,1,m)scanf("%d",&a[i][j]);
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(i,1,n)
        {
            rep(j,1,m)
            {
                b[i][j]+=b[i][j-1];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(i,1,n)
        {
            for(j=m;j>=1;j--)
            {
                b[i][j]+=b[i][j+1];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(j,1,m)
        {
            for(i=n;i>=1;i--)
            {
                b[i][j]+=b[i+1][j];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j];
        rep(j,1,m)
        {
            rep(i,1,n)
            {
                b[i][j]+=b[i-1][j];
                if(b[i][j]&&!a[i][j])ans++;
            }
        }
        printf("%lld
    ",ans);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    关于 Span 的一切:探索新的 .NET 明星:5. .NET 运行时的处理
    关于 Span 的一切:探索新的 .NET 明星: 3.什么是 Memory<T>,以及为什么你需要它?
    关于 Span 的一切:探索新的 .NET 明星: 4. Span<T> 和 Memory<T> 是如何与 .NET 库集成的?
    Configuring Kerberos with OpenLDAP backend
    gradle:secure protocol (like HTTPS) or allow insecure protocols
    Kerberos and LDAP
    EV录屏
    [转]在 WIN10 环境下,设置自己写的 程序 开机自动 启动的方法
    [转]go context之WithTimeout的使用
    [转]公司为什么要使用OKR,目的是什么?
  • 原文地址:https://www.cnblogs.com/dyzll/p/6095586.html
Copyright © 2020-2023  润新知