Codeforces Round #380 (Div. 2) B 两种记录方式(模拟)

54 篇文章 0 订阅
6 篇文章 0 订阅


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的矩形场。导演给你舞台的计划,谁的演员会遵循。对于每个单元格,在计划中说明在该单元格中是否有一个actor。
你会在舞台上放置一个聚光灯在一个良好的位置。聚光灯将投射光在四个方向之一(如果你从上面看舞台) - 左,右,上或下。因此,聚光灯的位置是它被放置的单元格和它发光的方向。
如果两个条件成立,则位置是好的:
1在单元格中没有放置聚光灯的演员;
2在聚光灯投射的方向上至少有一个演员。
计算放置聚光灯的良好位置的数量。如果位置单元或投影方向不同,则聚光的两个位置被认为是不同的。



先正着循环一遍,有1就把这一行这一列都都标记,遇到0,就看行跟列是不是被标记了,被标记了就说明前面或者上面有1,然后倒着来一次,看后面跟右面。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
int matrix[maxn][maxn], book1[maxn], book2[maxn];
int main()
{
        int n, m;
        scanf("%d%d", &n, &m);
        int ans = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
        {
            scanf("%d", &matrix[i][j]);
            if(matrix[i][j]) book1[i] = 1, book2[j] = 1;
            else
            {
                if(book1[i]) ans++;
                if(book2[j]) ans++;
            }
        }
        memset(book1, 0, sizeof(book1));
        memset(book2, 0, sizeof(book2));
        for(int i = n; i >= 1; i--)
            for(int j = m; j >= 1; j--)
        {
            if(matrix[i][j]) book1[i] = book2[j] = 1;
            else
            {
                if(book1[i]) ans++;
                if(book2[j]) ans++;
            }
        }
        printf("%d\n", ans);
    return 0;
}

直接用一个二维数组,看每一行的前几列有几个1,每一列前几行有几个1,这样看前面有没有1就是直接看sum的和,看后面就是用sum[n][j] - sum[i][j];

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e3 + 5;
int sum1[maxn][maxn], sum2[maxn][maxn],a[maxn][maxn];
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
    {
        scanf("%d", &a[i][j]);
        sum1[i][j] = sum1[i-1][j] + a[i][j];
        sum2[i][j] = sum2[i][j-1] + a[i][j];
    }
    int ans = 0;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
    {
        if(!a[i][j])
        {
            if(sum1[i][j]) ans++;
            if(sum2[i][j]) ans++;
            if(sum1[n][j]-sum1[i][j]) ans++;
            if(sum2[i][m]-sum2[i][j]) ans++;
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值