Submission #518595

#TimeUsernameProblemLanguageResultExecution timeMemory
518595blueRaisins (IOI09_raisins)C++17
100 / 100
873 ms28756 KiB
#include <iostream> using namespace std; int n, m; int** R; int** P; int gridsum(int i1, int j1, int i2, int j2) { return P[i2][j2] - P[i2][j1-1] - P[i1-1][j2] + P[i1-1][j1-1]; } int**** r; int res(int i1, int j1, int i2, int j2) { int q; if(r[i1][j1][i2][j2] == -1) { if(i1 == i2 && j1 == j2) q = 0; else { q = 2000000000; for(int I = i1; I < i2; I++) { q = min(q, res(i1, j1, I, j2) + res(I+1, j1, i2, j2) + gridsum(i1, j1, i2, j2)); } for(int J = j1; J < j2; J++) { q = min(q, res(i1, j1, i2, J) + res(i1, J+1, i2, j2) + gridsum(i1, j1, i2, j2)); } } r[i1][j1][i2][j2] = q; } return r[i1][j1][i2][j2]; } int main() { cin >> n >> m; R = new int*[n+1]; P = new int*[n+1]; r = new int***[n+1]; for(int i = 1; i <= n; i++) { r[i] = new int**[m+1]; for(int j = 1; j <= m; j++) { r[i][j] = new int*[n+1]; for(int k = 1; k <= n; k++) { r[i][j][k] = new int[m+1]; for(int l = 1; l <= m; l++) r[i][j][k][l] = -1; } } } for(int i = 0; i <= n; i++) { R[i] = new int[m+1]; P[i] = new int[m+1]; R[i][0] = P[i][0] = 0; } for(int j = 0; j <= m; j++) R[0][j] = P[0][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { cin >> R[i][j]; P[i][j] = P[i-1][j] + P[i][j-1] - P[i-1][j-1] + R[i][j]; } cout << res(1, 1, n, m) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...