답안 #720037

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720037 2023-04-07T10:01:35 Z a_aguilo 건포도 (IOI09_raisins) C++14
100 / 100
1612 ms 29376 KB
#include<bits/stdc++.h>

using namespace std;

int N, M;
vector<vector<vector<vector<int>>>> DP;
//DP[x1][x2][y1][y2];
vector<vector<int>> preSum, raisinsMap;

int getSum(int x1, int x2, int y1, int y2){
	int ans;
	if(x1 <= 0){
		if(y1 <= 0) ans = preSum[x2][y2];
		else ans = preSum[x2][y2] - preSum[x2][y1-1];
	}else{
		if(y1 <= 0) ans = preSum[x2][y2]-preSum[x1-1][y2];
		else{
			ans = preSum[x2][y2] + preSum[x1-1][y1-1] - preSum[x2][y1-1] - preSum[x1-1][y2];
		}
	}
	return ans;
}

int solve(int x1, int x2, int y1, int y2){
	if(x1 == x2 && y1 == y2) return 0;
	if(DP[x1][x2][y1][y2] != -1) return DP[x1][x2][y1][y2];
	int payNow = getSum(x1, x2, y1, y2);
	if(x1 == x2){
		int ans = solve(x1, x2, y1+1, y2);
		for(int dist = 1; (y1 + dist + 1) <= y2; ++dist){
			ans = min(ans, solve(x1, x2, y1, y1+dist) + solve(x1, x2, y1+dist+1, y2));
		}
		//cout << "x from " << x1 << " to " << x2 << "\t y from " << y1 << " to " << y2 << "\t value paid " << ans << endl;
		return DP[x1][x2][y1][y2] = payNow + ans;
	}else if(y1 == y2){
		int ans = solve(x1+1, x2, y1, y2);
		for(int dist = 1; (x1 + dist + 1) <= x2; ++dist){
			ans = min(ans, solve(x1, x1+dist, y1, y2) + solve(x1+dist+1, x2, y1, y2));
		}
		//cout << "x from " << x1 << " to " << x2 << "\t y from " << y1 << " to " << y2 << "\t value paid " << ans << endl;
		return DP[x1][x2][y1][y2] = payNow + ans;
	}else{
		int ans = solve(x1, x2, y1, y1) + solve(x1, x2, y1+1, y2);
		for(int dist = 1; (y1 + dist + 1) <= y2; ++dist){
			ans = min(ans, solve(x1, x2, y1, y1+dist) + solve(x1, x2, y1+dist+1, y2));
		}
		for(int dist = 0; (x1 + dist + 1) <= x2; ++dist){
			ans = min(ans, solve(x1, x1+dist, y1, y2) + solve(x1+dist+1, x2, y1, y2));
		}
		//cout << "x from " << x1 << " to " << x2 << "\t y from " << y1 << " to " << y2 << "\t value paid " << ans << endl;
		return DP[x1][x2][y1][y2] = payNow + ans;
	}
}

int main(){
	cin >> N >> M;
	raisinsMap = vector<vector<int>>(N, vector<int>(M));
	preSum = vector<vector<int>>(N, vector<int>(M));
	for(int i = 0; i < N; ++i){
		for(int j = 0; j < M; ++j){
			cin >> raisinsMap[i][j];
			preSum[i][j] = raisinsMap[i][j];
			if(i > 0 and j > 0){
				preSum[i][j] += preSum[i-1][j] + preSum[i][j-1] - preSum[i-1][j-1];
			}else{
				if(i > 0) preSum[i][j] += preSum[i-1][j];
				if(j > 0) preSum[i][j] += preSum[i][j-1];
			}
			//cout << preSum[i][j] << endl;
		}
	}
	DP = vector<vector<vector<vector<int>>>>(N, vector<vector<vector<int>>>(N, vector<vector<int>>(M, vector<int>(M, -1))));
	cout << solve(0, N-1, 0, M-1) << endl;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 12 ms 1368 KB Output is correct
9 Correct 16 ms 1876 KB Output is correct
10 Correct 38 ms 2388 KB Output is correct
11 Correct 20 ms 2132 KB Output is correct
12 Correct 88 ms 5728 KB Output is correct
13 Correct 249 ms 8288 KB Output is correct
14 Correct 35 ms 2988 KB Output is correct
15 Correct 211 ms 9472 KB Output is correct
16 Correct 13 ms 1836 KB Output is correct
17 Correct 80 ms 4820 KB Output is correct
18 Correct 797 ms 18488 KB Output is correct
19 Correct 1385 ms 26560 KB Output is correct
20 Correct 1612 ms 29376 KB Output is correct