zoukankan      html  css  js  c++  java
  • Codeforces_738B

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

    一道水题,比赛时没看复杂度,终测时TLE了,醉人。。。

    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define N 1005
    
    int gra[N][N];
    int main()
    {
            int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                scanf("%d",&gra[i][j]);
        int res=0;
        for(int i=0; i<n; i++)
        {
            int st=-1,en=-1;
            for(int j=0; j<m; j++)
            {
                if(gra[i][j]==1)
                {
                    if(st==-1)
                        st=j;
                    en=j;
                }
            }
            if(st>=0)
            {
                res+=st;
                for(int k=0;k<st;k++)
                    if(gra[i][k]==1)
                            res--;
                if(en>st)
                {
                        res+=(en-st-1)*2;
                        for(int k=st+1;k<en;k++)
                            if(gra[i][k]==1)
                                    res-=2;
                }
                res+=(m-1-en);
                for(int k=en+1;k<m;k++)
                    if(gra[i][k]==1)
                            res--;
            }
        }
    
        for(int j=0; j<m; j++)
        {
            int st=-1,en=-1;
            for(int i=0; i<n; i++)
            {
                if(gra[i][j]==1)
                {
                    if(st==-1)
                        st=i;
                    en=i;
                }
            }
            if(st>=0)
            {
                res+=st;
                for(int k=0;k<st;k++)
                    if(gra[k][j]==1)
                            res--;
                 if(en>st)
                {
                        res+=(en-st-1)*2;
                        for(int k=st+1;k<en;k++)
                            if(gra[k][j]==1)
                                    res-=2;
                }
                res+=(n-1-en);
                for(int k=en+1;k<n;k++)
                    if(gra[k][j]==1)
                            res--;
            }
        }
        printf("%d
    ",res);
        return 0;
    }
  • 相关阅读:
    Maven 打包指定子工程项目(springcloud分模块打包)
    linux常见问题: zip/unzip: command not found
    CentOS8安装jdk1.8
    nacos-docker镜像安装nacos并配置数据库
    浏览器的一个请求从发送到返回都经历了什么
    python之scrapy
    常见的爬虫与反爬虫斗争
    Python闭包与延迟绑定
    ip代理
    python编程:统计文件中单词出现次数
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6085754.html
Copyright © 2011-2022 走看看