POJ 1050 To the Max【DP】

这篇博客详细介绍了如何解决POJ 1050 - To the Max的问题,重点在于使用动态规划(DP)方法。作者建议在尝试此题之前先理解HDU 1003,并将二维问题转化为一维。博客提供了问题的输入、输出格式,示例输入和输出,以及AC(Accepted)代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To the Max
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 48554 Accepted: 25678

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


原题链接:http://poj.org/problem?id=1050

做这题前,先弄清楚HDU1003这题。

然后再把二维压缩为一维就可以了。

1,12,123,1234,2,23,234,3,34,4

按上面几种情况压缩,求出最大的即可。

AC代码:

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  *
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100+5;
int a[maxn][maxn];
int dp[maxn];
int n;
int getMax(int *a)
{
    int maxx=a[0];
    int sum=a[0];
    for(int i=1;i<n;i++)
    {
        if(sum+a[i]>a[i])
            sum+=a[i];
        else
            sum=a[i];
        if(sum>maxx)
            maxx=sum;
    }
    return maxx;
}
int main()
{
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                cin>>a[i][j];
        }
        int p;
        int maxx=0;
        for(int i=0;i<n;i++)
        {
            memset(dp,0,sizeof(dp));
            for(int j=i;j<n;j++)
            {
                p=0;
                for(int k=0;k<n;k++)
                {
                    dp[p]+=a[j][k];
                    p++;
                }
                int ans=getMax(dp);
                if(ans>maxx)
                    maxx=ans;
            }
        }
        cout<<maxx<<endl;
    }
    return 0;
}



### 关于拔河问题的动态规划实现 拔河问题是经典的 **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、付费专栏及课程。

余额充值