区间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 .定义一个颜色联通块 [ ...
随机推荐
- 重新签名apk文件(手工用命令行)
re-sign.jar中后自动去除签名这个方法,经试验不可用! 1.去除准备重新签名SinaVoice.apk软件本身的签名 将apk文件后缀改为.zip,然后从winrar中删除META-INF文件 ...
- MFC对话框Style说明
Popup:弹出窗口 Overlapped:重叠窗口 Child:子窗口 在它们之间并没有太多内在的差异,但是使用不同的窗口风格,它们的外观是 不同的. 重叠窗口通常用于建立应用程序主窗口.事实上,有 ...
- __sleep和__wakeup
魔术方法__sleep和__wakeup 串行化serialize可以把变量包括对象,转化成连续bytes数据. 你可以将串行化后的变量存在一个文件里或在网络上传输. 然后再反串行化还原为原来的数据. ...
- QT对话框中show和exec的区别
转自:http://hi.baidu.com/wangjuns8/blog/item/24b382460dd1c1338694737d.html QDialog的显示有两个函数show()和exec( ...
- js正则验证手机号码有效性
验证130-139,150-159,180-189号码段的手机号码 <script type="text/javascript"> [-]{})|([-]{})|([- ...
- Linux下select函数的使用
一.Select 函数详细介绍 Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect. accept.recv ...
- 浏览器打开URL的方式和加载过程
不同浏览器的工作方式不完全一样,大体上,浏览器的核心是浏览器引擎,目前市场占有率最高的几种浏览器几乎都使用了不同的浏览器引擎:IE使用的是Trident.Firefox使用的是Gecko.Safari ...
- Oracle 经常使用的改动语句
一.表Table 測试表:MY_TEST_TABLE -- Create table create table MY_TEST_TABLE ( A VARCHAR2(30), B NUMBER(10) ...
- 自绘XP风格菜单
这是以前写的代码,自绘XP风格的菜单,硬盘坏了后以为没了,最后写的一个软件要自定义风格,“翻箱倒柜”的终于在我可爱的古董机^_^上找到了一个应用的例子.还是把它放到Blog上来,即可共享又可作为备用 ...
- Oracle Autonomous Transactions(自治事务)
Oracle Autonomous Transactions Autonomous transactions allow you to leave the context of the calling ...