Codeforce 729B. Spotlights
2016-11-21 22:28
363 查看
B. Spotlights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print one integer — the number of good positions for placing the spotlight.
Examples
input
output
input
output
Note
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.
把图的外面一层都设置成1,那么对于所有0的贡献都是4,然后再减去多算的就行了,多算的就是贡献为为1的(按每行每列来看),所以从头或尾最长连续0的个数就是要减去的; 还有一种方法设置cnt记录1之前的0的个数,flag标记前面是否有1,然后每行每列统计即可;
AC代码:
#include<cstdio>
const int MAXN=1e3+11;
int G[MAXN][MAXN];
int main()
{
int N,M;
while(~scanf("%d%d",&N,&M)) {
int ans=0;
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
scanf("%d",&G[i][j]);
ans+=(!G[i][j]);
}
}
ans*=4;
for(int i=0;i<N;++i) {
int cnt=0;
int j=0;
while(!G[i][j]&&j<M) cnt++,++j;
j=M-1;
while(!G[i][j]&&j>=0) cnt++,--j;
ans-=cnt;
}
for(int i=0;i<M;++i) {
int cnt=0;
int j=0;
while(!G[j][i]&&j<N) cnt++,++j;
j=N-1;
while(!G[j][i]&&j>=0) cnt++,--j;
ans-=cnt;
}
printf("%d\n",ans);
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print one integer — the number of good positions for placing the spotlight.
Examples
input
2 4 0 1 0 0 1 0 1 0
output
9
input
4 4 0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0
output
20
Note
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.
把图的外面一层都设置成1,那么对于所有0的贡献都是4,然后再减去多算的就行了,多算的就是贡献为为1的(按每行每列来看),所以从头或尾最长连续0的个数就是要减去的; 还有一种方法设置cnt记录1之前的0的个数,flag标记前面是否有1,然后每行每列统计即可;
AC代码:
#include<cstdio>
const int MAXN=1e3+11;
int G[MAXN][MAXN];
int main()
{
int N,M;
while(~scanf("%d%d",&N,&M)) {
int ans=0;
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
scanf("%d",&G[i][j]);
ans+=(!G[i][j]);
}
}
ans*=4;
for(int i=0;i<N;++i) {
int cnt=0;
int j=0;
while(!G[i][j]&&j<M) cnt++,++j;
j=M-1;
while(!G[i][j]&&j>=0) cnt++,--j;
ans-=cnt;
}
for(int i=0;i<M;++i) {
int cnt=0;
int j=0;
while(!G[j][i]&&j<N) cnt++,++j;
j=N-1;
while(!G[j][i]&&j>=0) cnt++,--j;
ans-=cnt;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- Codeforce Flea CodeForces - 32C 规律|思维
- codeforce-343D:Water Tree(线段树+脑洞)
- codeforce C. Valera and Elections (DFS)
- Codeforce C. Barcode
- codeforce_844A
- codeforce-【A. Dima and Guards】
- codeforce Gym 100500A Poetry Challenge(博弈,暴搜)
- codeforce contest 103 problem D Time to Raid Cowavans(根号算法)
- flag for codeforce
- codeforce 486B
- codeforce 855C. Helga Hufflepuff's Cup 树形DP
- codeforce 374C. Journey(dp)
- Codeforce 219 div1
- CodeForce 724D. Dense Subsequence bY Assassin
- codeforce 367dev2_c dp
- Codeforce 731A. Night at the Museum
- CodeForce 446 Div.2 Wrath
- codeforce 827A String Reconstruction(模拟)
- One Day One Step 之Codeforce(2014.03.06)
- codeforce Round #232 Div2 map + 质因子 +组合