计蒜客- The writing on the wall
链接:https://nanti.jisuanke.com/t/30991
题意:给一个n*m红色格子组成的矩形,其中k个各自被涂黑,问红色格子的子矩阵个数
思路:我的思路参考这位大佬的 https://blog.csdn.net/Sirius_han/article/details/82313029
代码:
#include <iostream> #include <algorithm> #include <string.h> #define ll long long #define maxn 100009 #define maxm 109 #define inf 0x3f3f3f3f using namespace std; int n, m, k; int rec[maxn][maxm]; int disn[maxn][maxm]; int main() { int t; int x, y; scanf("%d", &t); for (int ii = 1; ii <= t; ii++) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= k; i++) { scanf("%d%d", &x, &y); rec[x][y] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (!rec[i][j]) disn[i][j] = disn[i - 1][j] + 1; } } int mini; ll ans = 0; for (int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { mini = inf; for (int k = j; k >= 1; k--) { mini = min(mini, disn[i][k]); ans += mini; } } } printf("Case #%d: %lld\n", ii, ans); memset(disn, 0, sizeof disn); memset(rec, 0, sizeof rec); } return 0; }