你 什么时候这么难了
题目大意:给出一 矩阵,有些地方能用有些地方不能用,问按格式所能组成的 和 的数量。
考虑到 其实就是最上面一横+中间一短竖+下面一横,
所以我们可以先预处理好每一个点的右边、下面的连续 数量,
然后遍历每个点是否可以作为 及 的左上角的点,若可以则利用乘法原理和加法原理进行答案累加计算;
而 其实就是在 最左边一列的下面又加了一短竖而已,长度不定,乘法原理处理即可。
时间复杂度 。
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
const int mod = 998244353;
int n, m, c, f;
char map[N][N];
int from_left[N][N], from_up[N][N]; //从某个点(包括自己)开始,右边、下面最长有多少个连续的 0
inline void speedup()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline void init()
{
memset(from_left, 0, sizeof(from_left));
memset(from_up, 0, sizeof(from_up));
for (int i = 1; i <= n; i++)
for (int j = m; j >= 1; j--)
if (map[i][j] == 48)
from_left[i][j] = from_left[i][j + 1] + 1;
for (int j = 1; j <= m; j++)
for (int i = n; i >= 1; i--)
if (map[i][j] == 48)
from_up[i][j] = from_up[i + 1][j] + 1;
}
int main()
{
speedup();
int t, id;
cin >> t >> id;
while (t--)
{
cin >> n >> m >> c >> f;
for (int i = 1; i <= n; i++)
cin >> map[i] + 1;
init();
int ansc = 0, ansf = 0;
for (int i = 1; i <= n - 2; i++)
{
for (int j = 1; j <= m; j++)
{
if (from_left[i][j] < 2 || from_up[i][j] < 3)
continue;
for (int k = i + 2; k <= i + from_up[i][j] - 1; k++)
if (from_left[k][j] >= 2)
ansc = (ansc + (long long)(from_left[k][j] - 1) * (from_left[i][j] - 1) % mod) % mod,
ansf = (ansf + (long long)(from_left[k][j] - 1) * (from_left[i][j] - 1) % mod *
from_up[k + 1][j] % mod) %
mod;
}
}
cout << ansc * c % mod << " " << ansf * f % mod << endl;
}
return 0;
}非常稳2.70s
我们注意到,在上一份暴力代码中,
每遍历到一个点时,我们都要重新计算一遍这个点的 和 的下半部分( 的下面一横, 的下面一横+短竖)的合法情况数量(即 循环),
而在同一列中,上面的点的情况是很有可能包含了下面的点的情况的,
所以我们可以利用一个类似前缀和的思路,预处理好每个点作为 和 的下半部分的左上角时的总情况数,
这样在最终计算的时候就只要每个点算一次就好了,少了一层循环,大大优化了速度,且用于 循环的 数组也可以省掉了。
形式化地, 首先我们知道,以点 为左上角所能组成的 和 数量分别为:
和
于是,我们可以预处理好每个点的
和
(预处理遍历时从下往上遍历,边算前缀和边算当列连续 数量并用变量存储即可省掉 数组)
这样在最终的答案统计中,只需计算
和
即可。
可以理解为从行列的一维前缀和到整个下半部分的二维前缀和的优化。
时间复杂度 。
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e3 + 10;
const int mod = 998244353;
int n, m, c, f;
char map[N][N];
int from_left[N][N]; //从某个点(包括自己)开始,右边最长有多少个连续的 0
int cnstrct_C[N][N], cnstrct_F[N][N]; //以某个点作为 C-、F- 的下半部分的左上角的情况数
inline void speedup()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline void init()
{
memset(from_left, 0, sizeof(from_left));
memset(cnstrct_C, 0, sizeof(cnstrct_C));
memset(cnstrct_F, 0, sizeof(cnstrct_F));
for (int i = 1; i <= n; i++)
for (int j = m; j >= 1; j--)
if (map[i][j] == 48)
from_left[i][j] = from_left[i][j + 1] + 1;
for (int j = 1; j <= m; j++)
for (int i = n, cnt = map[i][j] == 48; i >= 1; i--, cnt = map[i][j] == 48 ? cnt + 1 : 0) // cnt 即为当前 from_up
if (cnt > 0)
cnstrct_C[i][j] = (cnstrct_C[i + 1][j] + (long long)(from_left[i][j] - 1)) % mod,
cnstrct_F[i][j] = (cnstrct_F[i + 1][j] + (long long)(from_left[i][j] - 1) * (cnt - 1) % mod) % mod;
}
int main()
{
speedup();
int t, id;
cin >> t >> id;
while (t--)
{
cin >> n >> m >> c >> f;
for (int i = 1; i <= n; i++)
cin >> map[i] + 1;
init();
int ansc = 0, ansf = 0;
for (int i = 1; i <= n; i++) //遍历每个 C-、F- 的左上角
for (int j = 1; j <= m; j++)
if (map[i][j] == 48 && map[i + 1][j] == 48 && map[i][j + 1] == 48) //若可以作为 C-、F- 的左上角
ansc = (ansc + (long long)(from_left[i][j] - 1) * (cnstrct_C[i + 2][j]) % mod) % mod,
ansf = (ansf + (long long)(from_left[i][j] - 1) * (cnstrct_F[i + 2][j]) % mod) % mod;
cout << ansc * c << " " << ansf * f << endl;
}
return 0;
}