121552024-12-04 22:03:00kukkermanFestés (50 pont)cpp11Elfogadva 50/50284ms12344 KiB
#include <stdio.h>
#include <stdint.h>

int row_costs[4];
int col_costs[100000][4][4];
int rows, cols;

int64_t dp_cache[5];

static inline int64_t min(int64_t a, int64_t b) {
    return a < b ? a : b;
}

static inline int64_t max(int64_t a, int64_t b) {
    return a > b ? a : b;
}

static inline int is_row_painted(int rows_painted, int row) {
    return (rows_painted >> row) & 1;
}

void read_input(FILE *f) {
    fscanf(f, "%d %d", &rows, &cols);

    for (int r = 0; r < rows; r++) {
        fscanf(f, "%d", &row_costs[r]);
    }

    for (int c = 0; c < cols; c++) {
        for (int start_r = 0; start_r < rows; start_r++) {
            for (int end_r = start_r; end_r < rows; end_r++) {
                fscanf(f, "%d", &col_costs[c][start_r][end_r]);
            }
        }
    }
}

int64_t opt_col_cost_top_down(int row_count, int col, int rows_painted) {
    if (row_count == 0) {
        return 0;
    }

    if (dp_cache[row_count] == -1) {
        int64_t opt = INT64_MAX;
        int64_t top_opt = INT64_MAX;
        for (int paint_from = row_count - 1; paint_from >= 0; paint_from--) {
            top_opt = min(top_opt, opt_col_cost_top_down(paint_from, col, rows_painted));
            opt = min(opt, top_opt + col_costs[col][paint_from][row_count - 1]);
        }

        if (is_row_painted(rows_painted, row_count - 1)) {
            opt = min(opt, opt_col_cost_top_down(row_count - 1, col, rows_painted));
        }
        dp_cache[row_count] = opt;
    }

    return dp_cache[row_count];
}

int64_t opt_col_cost_top_down_entry(int col, int rows_painted) {
    for (int i = 0; i <= rows; i++) {
        dp_cache[i] = -1;
    }
    return opt_col_cost_top_down(rows, col, rows_painted);
}

int64_t min_cost() {
    int64_t opt = INT64_MAX;
    for (int rows_painted = (1 << rows) - 1; rows_painted >= 0; rows_painted--) {
        int64_t total_cost = 0;
        for (int i = 0; i < rows; i++) {
            if (is_row_painted(rows_painted, i)) {
                total_cost += row_costs[i];
            }
        }

        for (int i = 0; i < cols; i++) {
            total_cost += opt_col_cost_top_down_entry(i, rows_painted);
        }

        opt = min(opt, total_cost);
    }

    return opt;
}

int main() {
    read_input(stdin);
    printf("%lld", min_cost());

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms320 KiB
2Elfogadva0/01ms320 KiB
3Elfogadva2/2138ms10036 KiB
4Elfogadva2/21ms320 KiB
5Elfogadva3/33ms320 KiB
6Elfogadva2/226ms1364 KiB
7Elfogadva2/2268ms11320 KiB
8Elfogadva2/2270ms11184 KiB
9Elfogadva2/2266ms11320 KiB
10Elfogadva2/2272ms11320 KiB
11Elfogadva2/2268ms11320 KiB
12Elfogadva2/2248ms10392 KiB
13Elfogadva2/2254ms10808 KiB
14Elfogadva2/264ms8004 KiB
15Elfogadva3/365ms8248 KiB
16Elfogadva3/3133ms9268 KiB
17Elfogadva2/2131ms9272 KiB
18Elfogadva3/3134ms9272 KiB
19Elfogadva2/2238ms9784 KiB
20Elfogadva2/2254ms10552 KiB
21Elfogadva2/2264ms10996 KiB
22Elfogadva2/2266ms11064 KiB
23Elfogadva2/2266ms11064 KiB
24Elfogadva2/2266ms11064 KiB
25Elfogadva2/2284ms12344 KiB