답안 #840479

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
840479 2023-08-31T12:11:27 Z overwatch9 건포도 (IOI09_raisins) C++17
100 / 100
154 ms 25948 KB
#include <iostream>
#include <vector>
using namespace std;
vector <vector <int>> grid;
vector <vector <int>> pfx;
const int maxn = 50 + 1;
int dp[maxn][maxn][maxn][maxn];
int solve(int r1, int r2, int c1, int c2) {
    if (r1 == r2 && c1 == c2)
        return 0;
    if (dp[r1][r2][c1][c2] != -1)
        return dp[r1][r2][c1][c2];
    int ans = 1e9;
    int x = pfx[r2][c2] - pfx[r2][c1-1] - pfx[r1-1][c2] + pfx[r1-1][c1-1];
    for (int i = r1; i < r2; i++) {
        ans = min(ans, solve(r1, i, c1, c2) + solve(i+1, r2, c1, c2) + x);
    }
    for (int i = c1; i < c2; i++) {
        ans = min(ans, solve(r1, r2, c1, i) + solve(r1, r2, i+1, c2) + x);
    }
    dp[r1][r2][c1][c2] = ans;
    return ans;
}
int main() {
    int n, m;
    cin >> n >> m;
    grid = pfx = vector <vector <int>> (n+1, vector <int> (m+1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++)
            cin >> grid[i][j];
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            for (int l = 1; l <= m; l++) {
                for (int k = 1; k <= m; k++)
                    dp[i][j][l][k] = -1;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            pfx[i][j] = grid[i][j];
            pfx[i][j] += pfx[i-1][j] + pfx[i][j-1] - pfx[i-1][j-1];
        }
    }

    cout << solve(1, n, 1, m) << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 3 ms 3028 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 6 ms 5828 KB Output is correct
11 Correct 6 ms 4180 KB Output is correct
12 Correct 17 ms 10580 KB Output is correct
13 Correct 26 ms 12832 KB Output is correct
14 Correct 7 ms 4828 KB Output is correct
15 Correct 35 ms 14292 KB Output is correct
16 Correct 6 ms 9428 KB Output is correct
17 Correct 17 ms 14608 KB Output is correct
18 Correct 78 ms 22892 KB Output is correct
19 Correct 126 ms 23892 KB Output is correct
20 Correct 154 ms 25948 KB Output is correct