# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
655598 | horiseun | 건포도 (IOI09_raisins) | C++11 | 695 ms | 30316 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int n, m;
int choc[55][55];
int pref[55][55];
bool seen[55][55][55][55];
int cache[55][55][55][55];
int dp(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2) return 0;
if (seen[x1][y1][x2][y2]) return cache[x1][y1][x2][y2];
cache[x1][y1][x2][y2] = INT_MAX;
seen[x1][y1][x2][y2] = true;
for (int i = x1; i < x2; i++) {
cache[x1][y1][x2][y2] = min(cache[x1][y1][x2][y2], dp(x1, y1, i, y2) + dp(i + 1, y1, x2, y2));
}
for (int i = y1; i < y2; i++) {
cache[x1][y1][x2][y2] = min(cache[x1][y1][x2][y2], dp(x1, y1, x2, i) + dp(x1, i + 1, x2, y2));
}
cache[x1][y1][x2][y2] += (pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1]);
return cache[x1][y1][x2][y2];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> choc[i][j];
}
}
pref[1][1] = choc[1][1];
for (int i = 2; i <= m; i++) {
pref[1][i] = pref[1][i - 1] + choc[1][i];
}
for (int i = 2; i <= n; i++) {
pref[i][1] = pref[i - 1][1] + choc[i][1];
for (int j = 2; j <= m; j++) {
pref[i][j] = pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1] + choc[i][j];
}
}
cout << dp(1, 1, n, m) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |