답안 #705152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705152 2023-03-03T12:56:35 Z esomer 건포도 (IOI09_raisins) C++17
100 / 100
977 ms 24800 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define endl "\n"
 
const int MOD = 1e9 + 7;
 
int dp[50][50][50][50];
 
int solve2(vector<vector<int>>& table){
    int n = table.size();
    int m = table[0].size();
    vector<vector<int>> prefix(n, vector<int>(m, 0));
    for(int i = 0; i < n; i++){
		prefix[i][0] = table[i][0];
		for(int j = 1; j < m; j++){
			prefix[i][j] = prefix[i][j-1] + table[i][j];
		}
	}
    function<int(int, int, int, int)> DFS = [&](int lx, int rx, int ly, int ry){
        if(rx - lx == 0 && ry - ly == 0) return 0;
        if(dp[lx][rx][ly][ry] == -1){
            int ans = 2e9;
            for(int i = lx; i < rx; i++){
                ans = min(ans, DFS(lx, i, ly, ry) + DFS(i + 1, rx, ly, ry));
            }
            int add = 0;
            for(int i = ly; i <= ry; i++){
				if(i < ry) ans = min(ans, DFS(lx, rx, ly, i) + DFS(lx, rx, i + 1, ry));
                add += prefix[i][rx];
                if(lx > 0) add -= prefix[i][lx - 1];
            }
            ans += add;
            dp[lx][rx][ly][ry] = ans;
            return (int)ans;
        }else return dp[lx][rx][ly][ry];
    };
    return DFS(0, m - 1, 0, n - 1);
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    //freopen("inpt.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
 
    //int tt; cin >> tt;
    int n, m; cin >> n >> m;
	vector<vector<int>> table(n, vector<int>(m));
    for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin >> table[i][j];
		}         
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			for(int q = 0; q < m; q++){
				for(int l = 0; l < n; l++){
					dp[j][q][l][i] = -1;
				}
			}
		}         
	}
	cout << solve2(table)<< endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 980 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 15 ms 4180 KB Output is correct
9 Correct 25 ms 4504 KB Output is correct
10 Correct 33 ms 5844 KB Output is correct
11 Correct 28 ms 6868 KB Output is correct
12 Correct 101 ms 9684 KB Output is correct
13 Correct 161 ms 12380 KB Output is correct
14 Correct 46 ms 9592 KB Output is correct
15 Correct 214 ms 13072 KB Output is correct
16 Correct 16 ms 1560 KB Output is correct
17 Correct 77 ms 4728 KB Output is correct
18 Correct 500 ms 16972 KB Output is correct
19 Correct 834 ms 24004 KB Output is correct
20 Correct 977 ms 24800 KB Output is correct