답안 #705151

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705151 2023-03-03T12:54:15 Z esomer 건포도 (IOI09_raisins) C++17
100 / 100
1143 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 = ly; i < ry; i++){
                ans = min(ans, DFS(lx, rx, ly, i) + DFS(lx, rx, i + 1, ry));
            }
            for(int i = lx; i < rx; i++){
                ans = min(ans, DFS(lx, i, ly, ry) + DFS(i + 1, rx, ly, ry));
            }
            for(int i = ly; i <= ry; i++){
                ans += prefix[i][rx];
                if(lx > 0) ans -= prefix[i][lx - 1];
            }
            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 936 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 17 ms 4180 KB Output is correct
9 Correct 22 ms 4508 KB Output is correct
10 Correct 32 ms 5844 KB Output is correct
11 Correct 33 ms 6868 KB Output is correct
12 Correct 109 ms 9688 KB Output is correct
13 Correct 172 ms 12372 KB Output is correct
14 Correct 48 ms 9676 KB Output is correct
15 Correct 217 ms 13092 KB Output is correct
16 Correct 15 ms 1552 KB Output is correct
17 Correct 77 ms 4704 KB Output is correct
18 Correct 539 ms 16952 KB Output is correct
19 Correct 918 ms 24016 KB Output is correct
20 Correct 1143 ms 24800 KB Output is correct