题目链接:
https://www.acwing.com/problem/content/101/
题解:
边界问题最复杂,画图好好模拟一下,二维前缀和还是比较容易的
枚举的所有的边长为R的正方形,AC代码是枚举的正方形的左上端点。
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int const N = 5010; int s[N][N]; int xm,ym; int r; int main(void){ int n,r; int x,y,w; scanf("%d%d",&n,&r); r = min(5001,r); xm = ym = 5000; while(n--){ scanf("%d%d%d",&x,&y,&w); x++;y++; s[x][y] = w; } for(int i=1;i<=xm;i++){ for(int j=1;j<=ym;j++){ s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + s[i][j]; } } int res = 0; for(int i=1;i+r-1<=xm;i++){ for(int j=1;j+r-1<=ym;j++){ int x1 = i,y1 = j; int x2 = i+r-1,y2= j+r-1; int sum = s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1]; res = max(sum,res); } } printf("%d\n",res); return 0; }
原文地址:https://www.cnblogs.com/doubest/p/12276122.html
时间: 2024-10-08 22:04:04