题号:1387
参考题解:https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P1387
作者: hychychyc 更新时间: 2017-06-06 11:25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<iostream> #include<algorithm> #include<string> #include<utility> #include<queue> #include<cstdlib> #include<cstring> #include<map> using namespace std; int n, m; int arr[105][105]; int sum[105][105]; int main(){ ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ cin >> arr[i][j]; } } for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + arr[i][j]; //二维前缀和 } } int maxn = 1; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ for (int e = maxn; i + e <= n&&j + e <= m; e++){ int w = sum[i + e][j + e] - sum[i][j + e] - sum[i + e][j] + sum[i][j]; //这个用二维前缀和计算正方形和 if (e*e == w){ maxn = max(maxn, e); } else break; } } } cout << maxn; } |
放上一张前缀和的图片,写的也很不清楚,也许有助于理解。
20180802更新:
上面代码都是错误的!!!!!!!!!
二维前缀和方程是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; int n, m; int arr[105][105]; int main(){ cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++)cin >> arr[i][j]; for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)arr[i][j] = arr[i - 1][j] + arr[i][j - 1] - arr[i - 1][j - 1] + arr[i][j]; for (int k = min(n, m); k > 0; k--) { for (int i = 1; i <= n - k + 1; i++) { for (int j = 1; j <= m - k + 1; j++) { if (k*k == arr[i + k - 1][j + k - 1] - arr[i - 1][j + k - 1] - arr[i + k - 1][j - 1] + arr[i - 1][j - 1]) { //参考这里 cout << k; return 0; } } } } } |
动态规划做法:题解https://www.luogu.org/blog/user23035/solution-p1387
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; int n, m; int arr[105][105]; int main() { cin >> n >> m; int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> arr[i][j]; if(arr[i][j])arr[i][j] = min(min(arr[i - 1][j], arr[i][j - 1]), arr[i - 1][j - 1]) + 1; ans = max(ans, arr[i][j]); } cout << ans; } |