POJ1050 最大子矩阵

To the Max

http://poj.org/problem?id=1050

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

解题思路
本题是典型的动态规划,实际是求矩阵的最大子矩阵(和最大)。这是将序列的连续最大段和问题扩展到二维的一题。

序列的最大连续段和问题思路:
原序列a    0 -2 -7 0 9 2 -6 2
记录    b    0  0  0 0 9 11 5 7
结果为11

将这个问题扩展到二维,则先将选中的几行,逐列相加变为一个一维数组。(如果是一行的情况,则直接使用序列的最大连续段和方法)

多行变为一行时:例如
 0 -2 -7  0
 9  2 -6  2
-4  1 -4  1
-1  8  0 -2

当i=0, j=2时,则选择0,1,2行
    0 -2 -7   0
    9  2 -6   2
   -4  1 -4   1
a: 5  1 -17 3
b: 5  6  0   3
答案为3

参考
http://blog.youkuaiyun.com/jqandjq/article/details/5060283

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
    int i, j, k, l, n, ans, sum, max;
    int a[100][100];
    int b[100];

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    ans = 0;
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (i == j) {
                for (k = 0; k < n; k++) {
                    b[k] = a[i][k];
                }
            } else {
                for (k = 0; k < n; k++) {
                    b[k] = 0;
                    for (l = i; l <= j; l++) {
                        b[k] += a[l][k];
                    }
                }
            }

            sum = 0;
            max = 0;
            for (k = 0; k < n; k++) {
                sum += b[k];
                if (sum > max) {
                    max = sum;
                }
                if (sum < 0) {
                    sum = 0;
                }
            }

            if (max > ans) {
                ans = max;
            }
        }
    }

    printf("%d\n", ans);

    return 0;
}

 

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值