B. Spotlights
来源:互联网 发布:c语言null 编辑:程序博客网 时间:2024/06/02 00:24
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.
The first line contains two positive integers n andm (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, while0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Print one integer — the number of good positions for placing the spotlight.
2 40 1 0 01 0 1 0
9
4 40 0 0 01 0 0 10 1 1 00 1 0 0
20
In the first example the following positions are good:
- the (1, 1) cell and right direction;
- the (1, 1) cell and down direction;
- the (1, 3) cell and left direction;
- the (1, 3) cell and down direction;
- the (1, 4) cell and left direction;
- the (2, 2) cell and left direction;
- the (2, 2) cell and up direction;
- the (2, 2) and right direction;
- the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
题意:给出一个矩阵,对于每个0统计其上下左右有几个方向是1,该点的对应值就是几。计算矩阵中所有0的对应值之和。其实看样例就可以懂。
题解1;还可以把每行的第一个1和最后一个1标记一下,0在这两个1中间的就是+2,不在就+1.对于每一行每一列都是这样的,复杂度也是如下
题解2:先跑一次纵向,求出每个点在纵向上的对应值之和。再跑一次横向,求出每个点在横向上的对应值之和。总体复杂度为 O(2*n^2)
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;#define maxn 1000+10typedef long long LL;int a[maxn][maxn];int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=0;i<n;i++)for(int j=0;j<m;j++)scanf("%d",&a[i][j]);int ans=0;for(int i=0;i<n;i++){int cnt=0,flag=0;for(int j=0;j<m;j++){if(a[i][j]==0&&(!flag))cnt++;else if(a[i][j]==1){ans+=cnt;cnt=0;flag=1;}else if(a[i][j]==0&&flag){ans++;cnt++;}}}for(int j=0;j<m;++j){int cnt=0,flag=0;for(int i=0;i<n;++i){if(a[i][j]==0&&(!flag))cnt++;else if(a[i][j]==1){ans+=cnt;cnt=0;flag=1;}else if(a[i][j]==0&&flag){ans++;cnt++;}}}printf("%d\n",ans);}return 0;}
- B. Spotlights
- B. Spotlights
- codeforces 738B Spotlights
- Codeforces Round #380 B - Spotlights
- Spotlights
- Codeforces Round-#380-Div. 2-B-Spotlights
- codeforces - 738B - Spotlights (瞎搞)
- CodeForces 738B - Spotlights(前缀和)
- Codeforces 729B Spotlights 简单暴力
- Codeforces Round #380 (Div. 2) B. Spotlights (模拟)
- CodeForces 729B Spotlights(思维题+前缀和)
- Technocup 2017 — Elimination Round 2 (and Codeforces Round 380) Editorial B. Spotlights
- Codeforces #380(Div.2)B.Spotlights【思维+前缀和优化】
- Microsoft spotlights new Windows 8 apps at DEMO Mobile event
- b
- b
- b
- //b
- Android 即时音视频解决方案1——环信
- Java基础-IO流6 流的操作规律
- Kafka
- oracle数据库做定时任务(插入) 笔记
- iOS-常用工具库代码
- B. Spotlights
- 计算机网络(六) 网关--内部网关协议RIP和OSPF
- 图像缩放——双线性插值算法
- log4j配置
- 关于STM32 定时器PWM输出,关闭PWM时,输出引脚电平控制。
- 通过文件名利用NSClassFromString, 取到Class类别,通过setValue:forKey:传递值
- SurfaceView的介绍和使用
- Matlab之Treeplot
- Java和PHP在Web开发方面的比较