
문제 링크
23247번: Ten
A real estate company IC is managing a rectangular section of land. The section is divided into $mn$ segments in $m \times n$ matrix shape, where the number of rows and that of columns are $m$ and $n$, respectively. Each segment has its own price as a posi
www.acmicpc.net
풀이
2차원 누적합을 이용하는 문제다.
2차원 누적합을 모른다면 2차원 누적합 설명을 참고해라.
모든 경우를 확인하며 넓이가 300이 되는 경우를 찾으면 된다.
그런데 그냥 찾으면 점 4개의 위치를 고정해야 하므로 O(300^4)일 것 같지만, 시작점 두 개를 고정하고 넓이가 10보다 큰 놈을 만나게 된다면 더 커져도 정답의 후보가 될 수 없는 놈들이기 때문에 확인을 안 해도 된다. 그렇기 때문에 끝 점 두 개는 시작점마다 최대 10개의 경우의 수만 생기게 된다. 그래서 O(NM * 10^2)에 풀 수 있다.
코드
#include <cstdio>
int arr[302][302], psum[302][302];
int main() {
int n, m; scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &arr[i][j]);
psum[i][j] = psum[i - 1][j] + psum[i][j - 1] + arr[i][j] - psum[i - 1][j - 1];
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = i; k <= n; k++) {
for (int l = j; l <= m; l++) {
int sum = psum[k][l] - psum[i - 1][l] - psum[k][j - 1] + psum[i - 1][j - 1];
if (sum == 10) ans++;
if (sum >= 10) break;
}
}
}
}
printf("%d", ans);
}
'Algorithm > BOJ' 카테고리의 다른 글
백준[1037] 약수 (0) | 2021.10.26 |
---|---|
백준[1270] 전쟁 - 땅따먹기 (0) | 2021.10.22 |
백준[20168] 골목 대장 호석 - 기능성 (0) | 2021.10.20 |
백준[2729] 이진수 덧셈 (0) | 2021.10.19 |
백준[1374] 강의실 (0) | 2021.10.16 |