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的更多相关文章
随机推荐
- Spring注解配置定时任务<task:annotation-driven/>
http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...
- swift 2.0 语法 字符串
//: Playground - noun: a place where people can play import UIKit /*: 字符串 * OC中的字符串是一个对象, Swift中的字符串 ...
- docker(1):virtualbox 安装CoreOS系统,配置registry-mirror
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/51093850 未经博主同意不得转载. 博主地址是:http://blog.csd ...
- ORACLE 11G 怎样改动 awr 的保留期限小于8天
ORACLE 11G 怎样改动 awr 的保留期限小于8天 Oracle Database 11g 默认具备一个系统定义的Moving Window Baseline,该基线相应于 AWR 保留 ...
- 2014年最简单、快捷的美股Scottrade开户攻略
[开篇重点提示] 1.scottrade是国内用户用得最多的美股证券交易平台. 2.不用邮寄纸质资料,网上开户全搞定. 3.申请表格填写优惠代码,获取免费3次交易费用,鄙人的推荐优惠代码是 87195 ...
- bzoj3894: 文理分科(还是那道最小割)
3894: 文理分科 题目:传送门 感谢波老师没有来D飞我,让我做出了这题... 题解: 这题其实和我做的上一题(bzoj2132)很像,所以就不写题意了. 依然是那最小割... 这题给出了四个利益矩 ...
- remove namespace from xml config file
从xml配置文件中移除命令空间 https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-wit ...
- 【转】ACM 2567 -- 树的Prufer编码
本文介绍北京大学ACM网站2567号题目的解法.介绍部分基本翻译自网站上的题目介绍. 题目介绍: 给定一棵各节点编号为整数1,2,3...n的树(例如,无环连通图),其Prufer编码(Pruf ...
- hdu 1512 Monkey King —— 左偏树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1512 很简单的左偏树: 但突然对 rt 的关系感到混乱,改了半天才弄对: 注意是多组数据! #includ ...
- ADTS结构
ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式.转载请注明来自:http://www.binkery.com/ ADTS内容及结构一般情况下 ...