# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
255485 | shrek12357 | Raisins (IOI09_raisins) | C++14 | 367 ms | 20984 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |