区间Dp 暴力枚举+动态规划 Hdu1081

时间:2020-12-07 02:54:10

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 线段树区间合并&plus;暴力枚举

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

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

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

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

    题目链接  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. &lbrack;luogu1090 SCOI2003&rsqb; 字符串折叠(区间DP&plus;hash)

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

  7. 石子合并——区间dp

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

  8. 2020&period;7&period;19 区间dp阶段测试

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

  9. 区间dp的感悟

    学区间dp似乎也很久了...对区间dp的通用模型都了解了一些 但是做题还是很坑 上了一点难度的题基本想不出什么思路.. 目前的做题方式就是看题 想一会发现自己不会做 看题解 好巧妙啊 理解后写一发.. ...

随机推荐

  1. Cookie使用时需要注意个数及大小限制

    各浏览器对Cookie有一定的限制,在使用时需要格外注意. 各浏览器之间对cookie的不同限制:   IE6.0 IE7.0/8.0/9.0+ Opera FF Safari Chrome cook ...

  2. 移动web开发问题集

    一.让微信内置浏览器(x5)支持 flex .item-flex { display: -webkit-box; -webkit-box-pack: center; -webkit-box-align ...

  3. RAF(RandomAccessFile)类

    作用:读取文件 /** * */ package com.io.file; import java.io.File; import java.io.IOException; import java.i ...

  4. List与字符串转换

    1.将list元素用单引号引起来:List<TransferFocusxfSummaryTop3> topList = getTransferFocusxfSummaryTop3(user ...

  5. Android开发之获取相册照片和获取拍照照片

    在Android的开发过程中,我们可能会读取手机里面的照片或者通过相机拍摄获取照片,这是两种常用的获取图片的方式,在做项目过程中也会经常遇到,下面来介绍一下这两种获取方式.. 1.从本地相册获取照片: ...

  6. C&plus;&plus; 全排列函数 nyoj 366

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  7. Android客户端与服务端交互之登陆示例

    Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...

  8. 关于&lt&semi;超文本&gt&semi;定义

    百度百科定义: 1, 超文本是用超链接的方法,将各种不同空间的文字信息组织在一起的网状文本.它更是一种用户界面范式,用以显示文本及与文本之间相关的内容.现时超文本普遍以电子文档方式存在,其中的文字包含 ...

  9. java continue break 关键字 详解 区别 用法 标记 标签 使用 示例 联系

    本文关键词: java continue break 关键字 详解 区别  用法 标记  标签 使用 示例 联系   跳出循环 带标签的continue和break 嵌套循环  深入continue ...

  10. UVA-10375 唯一分解定理

    #include<iostream> #include<string.h> #include<algorithm> #include<math.h> # ...