杭电1081(动态规划)

题目:

Problem Description

Given a two-dimensional array of positive and
negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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 x 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

思路:

  把二维转换为一维,变成求一个序列的最大子串问题;

如-1 -2;-3 -4先变成-1 -2;-4 -6.

把第一行减第零行就是求矩阵第一行的最大值,第二行减第零行

就是求整个矩阵的最大值;第二行减第一行就是求矩阵第二行的

最大值!

代码:

  

 1 #include<stdio.h>
2 #include<string.h>
3 #define inf -0xfffffff
4 //#include<stdlib.h>
5 /*#include<iostream>
6 #include<algorithm>
7 using namespace std;*/
8
9
10 int main(){
11 int n, i, j, value[101][101], dp[101][101], max, zmax, k, sum;
12 while(scanf("%d", &n) != EOF){
13 memset(dp, 0, sizeof(dp));
14 for(i = 1; i <= n; i ++){
15 for(j = 1; j <= n; j ++){
16 scanf("%d", &value[i][j]);
17 dp[i][j] = dp[i - 1][j] + value[i][j];
18 }
19 }
20 zmax = inf;
21 for(i = 1; i <= n; i ++){
22 for(j = i; j <= n; j ++){
23 max = inf;
24 sum = 0;
25 for(k = 1; k <= n; k ++){
26 sum += dp[j][k] - dp[i - 1][k];
27 if(sum > max){
28 max = sum;
29 }
30 if(sum < 0){
31 sum = 0;
32 }
33 }
34 if(max > zmax){
35 zmax = max;
36 }
37 }
38 }
39 printf("%d\n", zmax);
40 }
41 return 0;
42 }

奋进

时间: 2024-08-10 15:12:03

杭电1081(动态规划)的相关文章

杭电1003动态规划

抄过来的学习下: import java.util.Scanner; /* O(N^3)这中方法就是采用暴力法咯,把所有情况都列出来!假设我们要求i--j这段下标的序列和,i从1-n,j也从1-n,有因为求i--j求和要便利一遍,最好情况是只遍历一遍 最坏是遍历n次,折中也就是n/2,所以是O(n^3); 但是我们知道,i肯定要小于j的咯,所以我们就把j从i--n循环就可以了!这可以小小优化一下, 第三种方法就是动态规划了 f(x+1)=f(x)>=0?f(x)+a[i]:a[i];用我代码中变

杭电1081 第二道 dfs题

Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听

杭电4826(动态规划)

Problem Description 度度熊是一只喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫只能从矩阵左上角第一个方格开始走,只有走到右上角的第一个格子才算走出迷宫,每一次只能走一格,且只能向上向下向右走以前没有走过的格子,每一个格子中都有一些金币(或正或负,有可能遇到强盗拦路抢劫,度度熊身上金币可以为负,需要给强盗写欠条),度度熊刚开始时身上金币数为0,问度度熊走出迷宫时候身上最多有多少金币? Input 输入的第一行是一个整数T(T < 200),表示共有T组数据.每组数据的

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电dp题集,附链接

Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋); 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)  其中,f[j]表示抢j块大洋的最大的逃脱概率,条件是f[j-q[i].money]可达,也就是

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!详解!)

Problem Description Many years ago , in Teddy's hometown there was a man who was called "Bone Collector". This man like to collect varies of bones , such as dog's , cow's , also he went to the grave - The bone collector had a big bag with a volu

ACM 五一杭电赛码&quot;BestCoder&quot;杯中国大学生程序设计冠军赛小记

对于这项曾经热爱的竞赛,不得不说这是我最后一年参加ACM比赛了,所以要珍惜每一次比赛的机会. 五一去杭电参加了赛码"BestCoder"杯中国大学生程序设计冠军赛,去的队伍包括了今年19支World final的队伍,几乎是全国最强的46所学校各出了一个代表队,十分感谢学校给了我这个大三的老年血手这次去比赛的机会. 比赛在5.2一天内完成,上午的热身赛居然是上一场Bestcoder的原题= =.虽然我们三个人都没做过...不过我还是水水的写了前两道题. 在中午的悲惨淋雨后,下午正赛开始