Submission #414066

# Submission time Handle Problem Language Result Execution time Memory
414066 2021-05-29T22:21:45 Z SansPapyrus683 Raisins (IOI09_raisins) C++17
100 / 100
546 ms 29352 KB
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

/**
 * https://oj.uz/problem/view/IOI09_raisins
 * 2 3 
 * 2 7 5 
 * 1 9 5 should output 77
 */
int main() {
    int row_num;
    int col_num;
    std::cin >> row_num >> col_num;
    vector<vector<int>> choco_prefs(row_num + 1, vector<int>(col_num + 1));
    for (int r = 1; r <= row_num; r++) {
        for (int c = 1; c <= col_num; c++) {
            std::cin >> choco_prefs[r][c];
            choco_prefs[r][c] += choco_prefs[r - 1][c]
                                 + choco_prefs[r][c - 1]
                                 - choco_prefs[r - 1][c - 1];
            
        }
    }
    auto sum = [&] (int rs, int cs, int re, int ce) {
        return choco_prefs[re + 1][ce + 1]
               - choco_prefs[rs][ce + 1]
               - choco_prefs[re + 1][cs]
               + choco_prefs[rs][cs];
    };

    vector<vector<vector<vector<int>>>> min_pay(
        row_num, vector<vector<vector<int>>>(
            col_num, vector<vector<int>>(row_num, vector<int>(col_num))
        )
    );
    for (int height = 1; height <= row_num; height++) {
        for (int width = 1; width <= col_num; width++) {
            if (height == 1 && width == 1) {
                continue;  // for a 1x1 block of chocolate the ans is always 0
            }
            for (int r = 0; r <= row_num - height; r++) {
                for (int c = 0; c <= col_num - width; c++) {
                    int& curr = min_pay[r][c][r + height - 1][c + width - 1];
                    curr = INT32_MAX;
                    int cost = sum(r, c, r + height - 1, c + width - 1);
                    for (int hor_split = 1; hor_split < width; hor_split++) {
                        curr = std::min(
                            curr,
                            cost
                            + min_pay[r][c][r + height - 1][c + hor_split - 1]
                            + min_pay[r][c + hor_split][r + height - 1][c + width - 1]
                        );
                    }
                    for (int vert_split = 1; vert_split < height; vert_split++) {
                        curr = std::min(
                            curr,
                            cost
                            + min_pay[r][c][r + vert_split - 1][c + width - 1]
                            + min_pay[r + vert_split][c][r + height - 1][c + width - 1]
                        );
                    }
                }
            }
        }
    }
    cout << min_pay[0][0][row_num - 1][col_num - 1] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 5 ms 1356 KB Output is correct
9 Correct 7 ms 1868 KB Output is correct
10 Correct 10 ms 2468 KB Output is correct
11 Correct 8 ms 2124 KB Output is correct
12 Correct 36 ms 5704 KB Output is correct
13 Correct 66 ms 8232 KB Output is correct
14 Correct 13 ms 2908 KB Output is correct
15 Correct 84 ms 9420 KB Output is correct
16 Correct 7 ms 1740 KB Output is correct
17 Correct 29 ms 4796 KB Output is correct
18 Correct 263 ms 18456 KB Output is correct
19 Correct 458 ms 26572 KB Output is correct
20 Correct 546 ms 29352 KB Output is correct