답안 #698146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698146 2023-02-12T13:36:18 Z europium 건포도 (IOI09_raisins) C++17
100 / 100
360 ms 24908 KB
// - 12/2/23
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <iterator>
#include <set>
#include <map>
#include <math.h>
#include <iomanip>
#include <unordered_set>
#include <queue>
#include <climits>
using namespace std;
// clang++ -std=c++17 IOI09_Raisins.cpp && ./a.out

using ll = long long;
int n, m;
vector<vector<int>> grid, pref;
const int MAXN = 50;
int dp[MAXN][MAXN][MAXN][MAXN];

int sum(int x1, int y1, int x2, int y2){
    x2++;
    y2++;
    return pref[x2][y2] - pref[x2][y1] - pref[x1][y2] + pref[x1][y1];
}

int get(int x1, int y1, int x2, int y2){
    if (dp[x1][y1][x2][y2] != -1) return dp[x1][y1][x2][y2];
    if (x1 == x2 && y1 == y2) return 0;

    int res = INT_MAX;

    // horizontal cuts
    for (int i = x1; i < x2; i++){
        res = min(res, get(x1, y1, i, y2) + get(i + 1, y1, x2, y2) + sum(x1, y1, i, y2) + sum(i + 1, y1, x2, y2));
    }

    // vertical cuts
    for (int j = y1; j < y2; j++){
        res = min(res, get(x1, y1, x2, j) + get(x1, j + 1, x2, y2) + sum(x1, y1, x2, j) + sum(x1, j + 1, x2, y2));
    }

    dp[x1][y1][x2][y2] = res;

    // cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << ' ' << res << '\n';
    return res;
}

void solve(){
    cin >> n >> m;

    grid.resize(n, vector<int> (m));
    pref.resize(n + 1, vector<int> (m + 1));

    for (int i = 0; i < MAXN; i++){
        for (int j = 0; j < MAXN; j++){
            for (int k = 0; k < MAXN; k++){
                for (int l = 0; l < MAXN; l++) dp[i][j][k][l] = -1;
            }
        }
    }

    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++) cin >> grid[i][j];
    }

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

    cout << get(0, 0, n - 1, m - 1);
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // freopen("input.txt", "r", stdin);
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24660 KB Output is correct
2 Correct 11 ms 24660 KB Output is correct
3 Correct 12 ms 24744 KB Output is correct
4 Correct 10 ms 24660 KB Output is correct
5 Correct 11 ms 24716 KB Output is correct
6 Correct 9 ms 24660 KB Output is correct
7 Correct 11 ms 24768 KB Output is correct
8 Correct 14 ms 24660 KB Output is correct
9 Correct 24 ms 24712 KB Output is correct
10 Correct 21 ms 24788 KB Output is correct
11 Correct 23 ms 24700 KB Output is correct
12 Correct 50 ms 24788 KB Output is correct
13 Correct 67 ms 24788 KB Output is correct
14 Correct 25 ms 24788 KB Output is correct
15 Correct 81 ms 24908 KB Output is correct
16 Correct 18 ms 24792 KB Output is correct
17 Correct 39 ms 24788 KB Output is correct
18 Correct 206 ms 24800 KB Output is correct
19 Correct 316 ms 24788 KB Output is correct
20 Correct 360 ms 24812 KB Output is correct