2021牛客暑期多校训练营1 - D - Determine the Photo Position - 题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021牛客暑期多校训练营1 - D - Determine the Photo Position - 题解相关的知识,希望对你有一定的参考价值。
目录
2021牛客暑期多校训练营1 - D - Determine the Photo Position
传送门
Time Limit: 1 second
Memory Limit: 262144K
题目描述
You have taken the graduation picture of graduates. The picture could be regarded as a matrix A A A of n × n n \\times n n×n, each element in A is 0 or 1, representing a blank background or a student, respectively.
However, teachers are too busy to take photos with students and only took a group photo themselves. The photo could be regarded as a matrix B B B of 1 × m 1 \\times m 1×m where each element is 2 representing a teacher.
As a master of photoshop, your job is to put photo B into photo A with the following constraints:
you are not allowed to split, rotate or scale the picture, but only translation.
each element in matrix B should overlap with an element in A completely, and each teacher should overlap with a blank background, not shelter from a student.
Please calculate the possible ways that you can put photo B into photo A.
输入描述:
The first line contains two integers n , m ( 1 ≤ n , m ≤ 2000 ) n,m(1 \\le n,m \\le 2000) n,m(1≤n,m≤2000) indicating the size of photos A and B.
In the next n n n lines,each line contains n {n} n characters of ‘0’ or ‘1’,representing the matrix A A A.
The last line contains m {m} m characters of ‘2’, representing matrix B B B.
输出描述:
Output one integer in a line, indicating the answer.
样例
样例一
输入
5 3
00000
01110
01110
01110
00000
222
输出
6
样例二
输入
3 2
101
010
101
22
输出
0
样例三
输入
3 1
101
010
101
2
输出
4
题目大意
拍毕业照有 n × n n\\times n n×n个位置,其中0是空位1是学生
有 m m m个老师(一排挨着)
问有多少种方式能把老师正好插入到学生的空位中(老师仍然一排挨着)
解题思路
那就对于学生的每一排,看这一排中有多少个连续的空位。
连续空位 t t t大于等于 m m m个就有 t − m + 1 t-m+1 t−m+1种插入方式。
连续空位数小于 m m m则不能插入。
比如长度为3的漂亮数组的最大值是 1 + 3 + 5 = 9 1+3+5=9 1+3+5=9,那么8可表示成 1 + 3 + 4 1+3+4 1+3+4是长度为3的漂亮数组。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int getOne(int lx, int m) // 参数:连续的空位数、要插入的老师数 | 返回:有多少种插入方式
{
if(lx<m)return 0; // 空位数小于老师数,不能插入,插入方式是0
return lx-m+1; // 空位数≥老师数,插入方式是空位数-老师数+1
}
int main()
{
int n,m;
cin>>n>>m;
int ans=0;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
s = '1'+s+'1'; // 前后各添加一个学生,对空位不产生影响,相当于是哨兵,最后必定会遍历到1,遍历到1就把连续的空位累加到结果中。
int lx=0; // 连续的空位数
for(int j=1;j<=n+1;j++) // 遍历这一排
{
if(s[j]=='0')lx++; // 是空位,连续的空位数就+1
else // 不是空位,前面的空位(如果有的话)将不连续
{
ans+=getOne(lx,m); // 累加到结果中
lx=0; // 连续的空位数变成0
}
}
}
cout<<ans<<endl; // 输出结果即可。
return 0;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/118857745
以上是关于2021牛客暑期多校训练营1 - D - Determine the Photo Position - 题解的主要内容,如果未能解决你的问题,请参考以下文章
2021牛客暑期多校训练营4 G.Product(容斥,模型转化)