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;
    }
  • 相关阅读:
    ASP.NET MVC中权限控制的简单实现
    HDU1004——Let the Balloon Rise
    如何使用飞秋FeiQ实现两电脑通信(或传输文件)
    vb.net 鼠标控制
    ireport制作报表pageheader只在第一页出现的解决办法
    Keycode对照表
    leetcode第一刷_Binary Tree Zigzag Level Order Traversal
    换硬币问题
    STM32 寄存器库和固件库
    java网络编程(2)InetAddress 类及udp协议
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6085754.html
Copyright © 2011-2022 走看看