poj 1050 To the Max (dp)

To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 47234 Accepted: 25026

Description

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. 
As an example, the maximal sub-rectangle of the array: 

0 -2 -7 0 
9 2 -6 2 
-4 1 -4 1 
-1 8 0 -2 
is in the lower left corner: 

9 2 
-4 1 
-1 8 
and has a sum of 15. 

Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

Sample Output

15

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题解:dp

f[i][j][k]表示以i,j为右下角,向上延伸k行的最大子矩阵。

f[i][j][k]=max(f[i][j-1][k]+sum[i][j]-sum[i-k][j],sum[i][j]-sum[i-k][j])

其中sum[i][j]表示的是j列前i行的前缀和。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define N 103
using namespace std;
int f[N][N][N],n,m,sum[N][N],a[N][N];
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) 
	 for (int j=1;j<=n;j++) scanf("%d",&a[i][j]);
	for (int i=1;i<=n;i++)
	 for (int j=1;j<=n;j++)
	  sum[i][j]=sum[i-1][j]+a[i][j];
	memset(f,128,sizeof(f));
	int ans=f[0][0][0];
	for (int i=1;i<=n;i++)
	 for (int j=1;j<=n;j++)
	  for (int k=1;k<=i;k++)
	   f[i][j][k]=max(f[i][j-1][k]+sum[i][j]-sum[i-k][j],sum[i][j]-sum[i-k][j]),ans=max(ans,f[i][j][k]);
    printf("%d\n",ans);
}



### 关于拔河问题的动态规划实现 拔河问题是经典的 **0/1 背包变种问题**,其核心目标是将一群人分成两队,使得每队的人数最多相差 1,并且两队的体重总和尽可能接近。此问题可以通过动态规划 (Dynamic Programming, DP) 来解决。 #### 动态规划的核心思路 该问题可以转化为一个子集划分问题:给定一组重量 \( w_1, w_2, \ldots, w_n \),找到两个子集 \( A \) 和 \( B \),满足以下条件: 1. 子集 \( A \) 的权重之和与子集 \( B \) 尽可能接近。 2. 如果总人数为奇数,则其中一个子集多一个人;如果总人数为偶数,则两者人数相等。 通过定义状态转移方程来解决问题。设 \( S \) 是所有人重量的总和,\( half = S / 2 \) 表示一半的重量。我们尝试寻找不超过 \( half \) 的最大子集重量 \( sum_A \),从而另一部分的重量自然就是 \( sum_B = S - sum_A \)[^1]。 #### 实现细节 以下是基于动态规划的具体算法描述: 1. 定义数组 `dp`,其中 `dp[i]` 表示是否存在一种组合方式使其重量恰好等于 \( i \)。 2. 初始化 `dp[0] = true`,表示重量为零的情况总是可行。 3. 遍历每个人的重量 \( w_i \),更新 `dp` 数组的状态。 4. 找到最大的 \( j \leq half \) 并使 `dp[j] == true` 成立,此时 \( j \) 即为一侧的最大重量 \( sum_A \)。 下面是具体的代码实现: ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; while(cin >> n && n != 0){ vector<int> weights(n); int total_weight = 0; for(int &w : weights){ cin >> w; total_weight += w; } int half = total_weight / 2; vector<bool> dp(half + 1, false); // dp[i] means whether weight 'i' is achievable. dp[0] = true; for(auto w : weights){ for(int j = half; j >= w; --j){ if(dp[j - w]){ dp[j] = true; } } } // Find the largest possible value less than or equal to half int closest_sum = 0; for(int j = half; j >= 0; --j){ if(dp[j]){ closest_sum = j; break; } } cout << min(closest_sum, total_weight - closest_sum) << " " << max(closest_sum, total_weight - closest_sum) << endl; } } ``` 上述程序实现了如何利用动态规划求解拔河问题中的最优分配方案[^2]。 #### 常见错误分析 对于 POJ 和 UVa 上的不同表现,可能是由于输入处理上的差异所致。UVa 版本通常涉及多组测试数据,而 POJ 可能仅限单组输入。因此,在提交至 UVa 时需注意循环读取直到文件结束标志 EOF 出现为止[^3]。 另外需要注意的是边界情况以及整型溢出等问题,确保所有变量范围适当设置以容纳可能出现的最大数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值