Codeforces_738B
1 second
256 megabytes
standard input
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.
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.
Print one integer — the number of good positions for placing the spotlight.
2 4
0 1 0 0
1 0 1 0
9
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 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.
一道水题,比赛时没看复杂度,终测时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=; i<n; i++)
for(int j=; j<m; j++)
scanf("%d",&gra[i][j]);
int res=;
for(int i=; i<n; i++)
{
int st=-,en=-;
for(int j=; j<m; j++)
{
if(gra[i][j]==)
{
if(st==-)
st=j;
en=j;
}
}
if(st>=)
{
res+=st;
for(int k=;k<st;k++)
if(gra[i][k]==)
res--;
if(en>st)
{
res+=(en-st-)*;
for(int k=st+;k<en;k++)
if(gra[i][k]==)
res-=;
}
res+=(m--en);
for(int k=en+;k<m;k++)
if(gra[i][k]==)
res--;
}
} for(int j=; j<m; j++)
{
int st=-,en=-;
for(int i=; i<n; i++)
{
if(gra[i][j]==)
{
if(st==-)
st=i;
en=i;
}
}
if(st>=)
{
res+=st;
for(int k=;k<st;k++)
if(gra[k][j]==)
res--;
if(en>st)
{
res+=(en-st-)*;
for(int k=st+;k<en;k++)
if(gra[k][j]==)
res-=;
}
res+=(n--en);
for(int k=en+;k<n;k++)
if(gra[k][j]==)
res--;
}
}
printf("%d\n",res);
return ;
}
Codeforces_738B的更多相关文章
随机推荐
- Ubuntu查看系统版本的方法
1. less /etc/issue 2. less /proc/version 3. uname -a 4. lsb_release -a
- MYSQL常用的性能指标
(1) QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Questio ...
- 如何在eclipse中用maven编译
在eclipse中用maven编译的方法: 在项目中的“pom.xml”文件上点击右键,在弹出的菜单中选择“Run AS”à“Maveninstall”来编译和生成项目.如下图所示: 在编译和生成过程 ...
- Clojure: 寻找项目依赖项目
Clojure寻找项目依赖项目: lein deps :tree
- 【CV论文阅读】Two stream convolutional Networks for action recognition in Vedios
论文的三个贡献 (1)提出了two-stream结构的CNN,由空间和时间两个维度的网络组成. (2)使用多帧的密集光流场作为训练输入,可以提取动作的信息. (3)利用了多任务训练的方法把两个数据集联 ...
- 【翻译自mos文章】OGG replicat 进程使用的 TCP port
OGG replicat 进程使用的 TCP port 来源于: TCP PORT USED BY REPLICAT PROCESSES (文档 ID 1060954.1) 适用于: Oracle G ...
- 【一】注入框架RoboGuice使用:(A brief example of what RoboGuice does)
在我们平时开发Android项目的时候比如常常须要使用各种View控件,然后进行声明.findViewById.而且进行强转.每次都要写这种代码就显得很繁琐,而且easy出错哦.那么针对这种情况且不限 ...
- oc 和swift混编的时候打包ipa安装不了
做了一个oc项目.里面实用到swift第三方,真机执行的时候是正常的,用企业级证书打包成ipa之后安装完毕之后闪退.通过导出手机log日志发现错误提示: Dyld Error Message: L ...
- Spring自我总结
1.InitializingBean Spring设置完一个bean的合作者后,会检查bean是否实现InitializingBean接口,实现的话会调用afterPropertiesSet(Init ...
- Batch 拷贝远程机器文件到本机指定目录下
net use * /del /yesNET USE Y: \\远程机IP\d$ 登录密码 /user:domain\登录用户 set sourcePath="Y:\DOAutomatio ...