F - 最大子矩形

Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u

Submit



Status

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

<span style="color:#3333ff;">/*
_________________________________________________________________________________________________________________
author : Grant Yuan
time : 2014.7.19
source : Hdu1081
algorithm : 暴力枚举+动态规划
explain : 暴力枚举第k1行到第k2行每一列各个元素的和sum[i],然后对sum[i]进行一次最大子序列求和
___________________________________________________________________________________________________________________
*/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<functional>
#include<algorithm>
using namespace std; int a[105][105];
int l;
int sum[105];
int dp[105]; int main()
{
while(~scanf("%d",&l)){
for(int i=0;i<l;i++)
for(int j=0;j<l;j++)
scanf("%d",&a[i][j]); int ans=-9999999,ans1;
for(int k1=0;k1<l;k1++)
for(int k2=0;k2<l;k2++){
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
for(int i=0;i<l;i++){
for(int j=k1;j<=k2;j++)
{
sum[i]+=a[i][j];
}
for(int m=0;m<l;m++)
{
dp[m+1]=max(dp[m]+sum[m],sum[m]);
}
ans1=dp[1];
for(int d=1;d<=l;d++)
if(dp[d]>ans1)
ans1=dp[d];
if(ans1>ans)
ans=ans1;}} printf("%d\n",ans);} }
</span>

区间Dp 暴力枚举+动态规划 Hdu1081的更多相关文章

  1. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  2. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  3. Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)

    题目链接  2016 EC-Final 题意  现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数.   求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...

  4. 动态规划(区间DP):HDU 5115 Dire Wolf

    Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not ...

  5. 【BZOJ-1055】玩具取名 区间DP

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1560  Solved: 907[Submit][Statu ...

  6. [luogu1090 SCOI2003] 字符串折叠(区间DP+hash)

    传送门 Solution 区间DP,枚举断点,对于一个区间,枚举折叠长度,用hash暴力判断是否能折叠即可 Code #include <cstdio> #include <cstr ...

  7. 石子合并——区间dp

    石子合并(3种变形) <1> 题目: 有N堆石子排成一排(n<=100),现要将石子有次序地合并成一堆,规定每次只能选相邻的两堆合并成一堆,并将新的一堆的石子数,记为改次合并的得分, ...

  8. 2020.7.19 区间dp阶段测试

    打崩了-- 事先说明,今天没有很在状态,所以题解就直接写在代码注释里的,非常抱歉 T1 颜色联通块 此题有争议,建议跳过 题目描述 N 个方块排成一排,第 i 个颜色为 Ci .定义一个颜色联通块 [ ...

  9. 2020.7.19 区间 dp 阶段测试

    打崩了-- 事先说明,今天没有很在状态,所以题解就直接写在代码注释里的,非常抱歉 T1 颜色联通块 此题有争议,建议跳过 题目描述 N 个方块排成一排,第 i 个颜色为 Ci .定义一个颜色联通块 [ ...

随机推荐

  1. iOS苹果官方Demo合集

    Mirror of Apple’s iOS samples This repository mirrors Apple’s iOS samples. Name Topic Framework Desc ...

  2. linux下编译.so 和.a 可能出现的问题 ?

    1. 静态函数库 这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持 ...

  3. 你真的知道为什么不推荐使用@import?

    Difference between @import and link in CSS Use of @import <style type="text/css">@im ...

  4. Linux中TCP wrapper的使用

    Linux中TCP wrapper的使用 tcpwrapper的目的是对那些访问控制功能较弱的服务提供访问控制功能要想了解访问控制就必须先知道服务监听的概念: 服务监听的两种方式: listen    ...

  5. 基于visual Studio2013解决C语言竞赛题之1046矩阵计算

       题目 解决代码及点评 /************************************************************************/ /* 46 ...

  6. EasyUI - DataGrid 组建 - [ 排序功能 ]

    效果: 红框的字段看,为设置了,列排序,向后台Post数据sort/order. 原理:向后台POST数据,sort/post数据. html代码: <table id="tab&qu ...

  7. ASP.NET - 在线编辑器(FreeTextBox)

    1.首先下载FreeTextBox程序集,再次使用3.3.1 · 官网:http://freetextbox.com/ · 百度云: 2.在程序中引入程序集 3.在工具箱中添加控件. 4.之后拖动控件 ...

  8. linux命令:scp

    有时候ftp被禁用了, 就用scp替代; 命令行: scp from to_user@to_ip:dir_to/file_name 执行该命令之后,按照提示输入to_host的登陆密码即可. scp ...

  9. Qt之开机自启动及拥有管理员权限

    源地址:http://blog.sina.cn/dpool/blog/s/blog_a6fb6cc90101feia.html Windows开机自启动的程序很多,包括系统软件.杀毒软件.一些其他安装 ...

  10. Swift - 系统声音服务的使用(播放声音,提醒,震动)

    1,系统声音服务介绍: 系统声音服务提供了一个Api,用于播放不超过30秒的声音.它支持的文件格式有限,具体的说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. 但此函数没有提供操 ...