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的更多相关文章
随机推荐
- # [libx264 @ 00000275eb57fec0] height not divisible by 2 (520x325)
# [libx264 @ 00000275eb57fec0] height not divisible by 2 (520x325)
- open_basedir restriction in effect,解决php引入文件权限问题 lnmp
1.配置了虚拟域名 vim /usr/local/nginx/conf/vhost/siemens.conf server { listen 80; #listen [::]:80 default_s ...
- B1072 [SCOI2007]排列perm 状压dp
很简单的状压dp,但是有一个事,就是...我数组开大了一点,然后每次memset就会T,然后开小就好了!!!震惊!以后小心点这个问题. 题干: Description 给一个数字串s和正整数d, 统计 ...
- Python的包管理工具easy_install, setuptools, pip,distribute介绍
1.相互之间的关联 easy_install, setuptools, pip,distribute,这几个工具的联系,如下图: 可以看到distribute是setuptools的取代,pip是ea ...
- Centos搭建图形界面VNC
#!/bin/sh unset SESSION_MANAGER unset DBUS_SESSION_BUS_ADDRESS exec /etc/X11/xinit/xinitrc
- Notepad++ - 通过语言格式设置自定义语法高亮颜色
http://blog.csdn.net/onceing/article/details/51554399 Global Styles Indent guideline style 缩进参考线的颜色 ...
- PCB javascript解析钻孔(Excellon)格式实现方法
解析钻孔(Excellon)格式前首先得了解此格式,这样才能更好的解析呀. 一个钻孔里面包含的基本信息如下: 1.单位:公式mm,英制inch 2.省零方式:前省零,后省零 3.坐标方式:绝对坐标,相 ...
- JavaScript中对象转换为原始值的规则
JavaScript中对象转换为原始值遵循哪些原则? P52 对象到布尔值对象到布尔值的转换非常简单:所有的对象(包括数字和函数)都转换为true.对于包装对象亦是如此:new Boolean(fal ...
- Java经典算法之冒泡排序(Bubble Sort)
原理:比较相邻的两个值,将值大的元素交换至右端 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后.然后比较第2个数和第3个数,将小数 ...
- RT-Thread 设备驱动I2C浅析及使用
由于 I2C 可以控制多从机的属性,设备驱动模型分为 I2C总线设备(类似与Linux里面的I2C适配器) + I2C从设备: 系统I2C设备驱动主要实现 I2C 总线设备驱动,而具体的I2C 从设 ...