洛谷 P2004 领地选择
题目传送门
解题思路:
二维前缀和.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int n,m,c,g[1001][1001],ansi = 1,ansj = 1; 7 long long a[1001][1001],k[1001][1001],ans = -1999999999; 8 //ans一定要初始化,-9999999都不行 9 10 int main() { 11 scanf("%d%d%d",&n,&m,&c); 12 for(int i = 1;i <= n; i++) 13 for(int j = 1;j <= m; j++) 14 scanf("%d",&g[i][j]); 15 for(int i = 1;i <= n; i++) 16 for(int j = 1;j <= m; j++) { 17 a[i][j] = a[i-1][j] + a[i][j-1] - a[i-1][j-1] + g[i][j]; 18 if(i >= c && j >= c) { 19 k[i][j] = a[i][j] - a[i-c][j] - a[i][j-c] + a[i-c][j-c]; 20 if(k[i][j] > ans) { 21 ans = k[i][j]; 22 ansi = i - c + 1; 23 ansj = j - c + 1; 24 } 25 } 26 } 27 printf("%d %d",ansi,ansj); 28 return 0; 29 }