区间Dp 暴力枚举+动态规划 Hdu1081
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的更多相关文章
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- Codeforces Gym 101194C Mr. Panda and Strips(2016 EC-Final,区间DP预处理 + 枚举剪枝)
题目链接 2016 EC-Final 题意 现在要找到数列中连续两个子序列(没有公共部分).要求这两个子序列本身内部没有重复出现的数. 求这两个子序列的长度的和的最大值. 首先预处理一下.令$ ...
- 动态规划(区间DP):HDU 5115 Dire Wolf
Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not ...
- 【BZOJ-1055】玩具取名 区间DP
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1560 Solved: 907[Submit][Statu ...
- [luogu1090 SCOI2003] 字符串折叠(区间DP+hash)
传送门 Solution 区间DP,枚举断点,对于一个区间,枚举折叠长度,用hash暴力判断是否能折叠即可 Code #include <cstdio> #include <cstr ...
- 石子合并——区间dp
石子合并(3种变形) <1> 题目: 有N堆石子排成一排(n<=100),现要将石子有次序地合并成一堆,规定每次只能选相邻的两堆合并成一堆,并将新的一堆的石子数,记为改次合并的得分, ...
- 2020.7.19 区间dp阶段测试
打崩了-- 事先说明,今天没有很在状态,所以题解就直接写在代码注释里的,非常抱歉 T1 颜色联通块 此题有争议,建议跳过 题目描述 N 个方块排成一排,第 i 个颜色为 Ci .定义一个颜色联通块 [ ...
- 2020.7.19 区间 dp 阶段测试
打崩了-- 事先说明,今天没有很在状态,所以题解就直接写在代码注释里的,非常抱歉 T1 颜色联通块 此题有争议,建议跳过 题目描述 N 个方块排成一排,第 i 个颜色为 Ci .定义一个颜色联通块 [ ...
随机推荐
- 利用内容提供者插入sms(装B程序)
1.sms的权限配置 <uses-permission android:name="android.permission.READ_SMS"/> <uses-pe ...
- delphi如何加上spliter分割条,任意调整大小
如题1:如何把一个panel分割成四个小的panle 2:也就是如何加上spliter,分割条,任意调整大小 3.如何有独立的handle使用多个总共5个为什么呢,你放4个panel 然后放split ...
- linux 下vi中关于删除某段,某行,或者全部删除的命令
1,先打开某个文件: vi filename 2,转到文件结尾 在命令模式输入 G 转到10行 在命令模式输入 10G 4,删除所有内容:先用G 转到文件尾,然后使用下面命令: :1, ...
- Oracle数据库top10物理段
select owner, name, type, mega, tbs from (select owner, case when ...
- 在Android手机上获取其它应用的包名及版本
转载请注明出处:http://blog.csdn.net/jason_src/article/details/37757661 获取Android手机上其它应用的包名及版本方法有非常多,能够通过AAP ...
- easyhadoop:failed to open stream:Permission denied in /var/www/html/index.php
今天又重新部署了下easyhadoop,结果apache后台服务器报这个错误: [Fri Dec 13 10:32:41 2013] [notice] SIGHUP received. Attempt ...
- Mac 安装配置启动Tomcat
Tomcat Mac 下的安装: TomCat 下载地址,例如: http://tomcat.apache.org/download-70.cgi 在Mac 上下载的时候,下载tar.gz包 下载完成 ...
- Freemarker概念简单介绍
Freemarker概念简单介绍 1. Freemarker是什么 模板引擎:一种基于模板的,用来生成输出文本的通过工具. 基于java开发包和类库 2. Freemarker能做什么 MVC ...
- VHDL TestBench 测试终止时自动结束仿真——assert方法
可在结束仿真位置添加如下代码: assert false report "Simulation is finished!" severity Failure; 则在Modelsim ...
- TCP与UDP各自特点对比
UDP和TCP是我们最常用的两种通信方式,下面就两者之间的特点做一个对比: 1.UDP主要用在实时性要求高以及对质量相对较弱的地方,如流媒体. 2.TCP既然是面向连接的,那么运行环境必然要求其保证可 ...