[洛谷P1369]矩形
题目大意:有$n(n\leqslant300)$个点,每个点坐标范围在$[1\sim100]$,求一个矩阵,使得边界上的点最多。
题解:做一遍二维前缀和,直接暴力枚举两个顶点
卡点:无
C++ Code:
#include <algorithm> #include <cstdio> #define maxn 105 int n, ans; int s[maxn][maxn]; inline int calc(int lx, int ly, int rx, int ry) { if (lx > rx || ly > ry) return 0; return s[rx][ry] - s[rx][ly - 1] - s[lx - 1][ry] + s[lx - 1][ly - 1]; } int main() { scanf("%d", &n); for (int i = 0, x, y; i < n; ++i) { scanf("%d%d", &x, &y); s[x][y] = 1; } for (int i = 1; i <= 100; ++i) { for (int j = 2; j <= 100; j++) s[i][j] += s[i][j - 1]; } for (int i = 2; i <= 100; ++i) { for (int j = 1; j <= 100; j++) s[i][j] += s[i - 1][j]; } for (int lx = 1; lx <= 100; ++lx) for (int ly = 1; ly <= 100; ++ly) for (int rx = lx; rx <= 100; ++rx) for (int ry = ly; ry <= 100; ++ry) { ans = std::max(ans, calc(lx, ly, rx, ry) - calc(lx + 1, ly + 1, rx - 1, ry - 1)); } printf("%d\n", ans); return 0; }