제출 #255485

#제출 시각아이디문제언어결과실행 시간메모리
255485shrek12357건포도 (IOI09_raisins)C++14
100 / 100
367 ms20984 KiB
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <climits> #include <cmath> #include <fstream> #include <queue> using namespace std; int pre[51][51]; int calc(int a, int b, int c, int d) { int ans = pre[c][d]; if (a > 0) { ans -= pre[a - 1][d]; } if (b > 0) { ans -= pre[c][b - 1]; } if (a > 0 && b > 0) { ans += pre[a - 1][b - 1]; } return ans; } int main() { int n, m; cin >> n >> m; int grid[51][51]; int dp[51][51][51][51]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } pre[0][0] = grid[0][0]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == 0 && j == 0) { continue; } if (i == 0) { pre[i][j] = pre[i][j - 1] + grid[i][j]; } else if (j == 0) { pre[i][j] = pre[i - 1][j] + grid[i][j]; } else { pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + grid[i][j]; } } } for (int len1 = 0; len1 < n; len1++) { for (int len2 = 0; len2 < m; len2++) { for (int i = 0; i < n - len1; i++) { for (int j = 0; j < m - len2; j++) { if (len1 == 0 && len2 == 0) { dp[i][j][i + len1][j + len2] = 0; } else { dp[i][j][i + len1][j + len2] = INT_MAX; for (int a = 0; a < len1; a++) { dp[i][j][i + len1][j + len2] = min(dp[i][j][i + len1][j + len2], dp[i][j][i + a][j + len2] + dp[i + a + 1][j][i + len1][j + len2] + calc(i, j, i + len1, j + len2)); } for (int a = 0; a < len2; a++) { dp[i][j][i + len1][j + len2] = min(dp[i][j][i + len1][j + len2], dp[i][j][i + len1][j + a] + dp[i][j + a + 1][i + len1][j + len2] + calc(i, j, i + len1, j + len2)); } } } } } } cout << dp[0][0][n - 1][m - 1] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...