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的更多相关文章
随机推荐
- 类似于SVN的文档内容差异对比工具winmerge
原文:http://www.jianshu.com/p/99282a4f3870 https://sourceforge.net/projects/winmerge/?source=typ_redir ...
- wpf slider进度条的样式模板,带有进度颜色显示
效果图: 仅仅需在前台加上这段代码就可以: <UserControl.Resources> <!--笔刷--> <LinearGradientBrush x:Key=&q ...
- [dfs] UVALive 3667 Ruler
题目链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=1668">https://ic ...
- swift 学习笔记一
看 Developing IOS 8 Apps with swift 的时候看到一些比較实用的点,记录一下: 1.将函数作为參数传递. 比如: 写一个两个数求和的函数:performOperation ...
- 微软将支持.net开源并跨平台,新特性会体现于VS2015
http://news.microsoft.com/2014/11/12/microsoft-takes-net-open-source-and-cross-platform-adds-new-dev ...
- Mariadb-lib
mariadb-libs-5.5.44-2.el7.centos.x86_64
- 湖南集训day6
难度:☆☆☆☆☆☆☆☆ /* 对于第一问:f[i][j]表示前i个数,当前黑板上的数为j的概率 当前有三种情况 1.当前数不是j的倍数—>黑板上的数字改变. 2.当前数是j的倍数且当前数在前i个 ...
- [App Store Connect帮助]二、 添加、编辑和删除用户(5)创建一个沙盒测试员帐户
如果您的 App 使用了 App 内购买项目或 Apple Pay,您可以在 App Store Connect 中创建沙盒测试员帐户,以便您向用户提供该 App 前,可以使用该帐户在测试环境中运行您 ...
- C#使用Parallel处理数据同步写入Datatable并使用BulkInsert批量导入数据库
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL). 业务流程:选择照片文件夹,分别访问照片-->调用DLL接口传递照片路径-->接收处理返回值-->写入数据 ...
- ACM算法目录
数据结构 栈,队列,链表 •哈希表,哈希数组 •堆,优先队列 双端队列 可并堆 左偏堆 •二叉查找树 Treap 伸展树 •并查集 集合计数问题 二分图的识别 •平衡二叉树 •二叉排序树 •线段树 一 ...