【动态规划】HDU 1081 & XMU 1031 To the Max
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1081
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1031
题目大意:
给一个n*n(n<=100)的矩阵,求一个矩形覆盖的值最大是多少。
题目思路:
【动态规划】
二维的最大字段和。先考虑一维的情况。f[i]=max(f[i-1]+a[i],a[i])
只要之前的部分和大于零则一起取一定比只取当前位置的要优。
因此只要判断局部段的和是否大于零。同时每多取一个数就要更新答案。
之后只要将一维扩展到二维就行。枚举行的始末位置。
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 104
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int f;
char map[N][N];
int tot[N][N];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,l;
// for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
while(~scanf("%d",&n))
{
ans=-MAX;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
scanf("%d",&map[i][j]);
tot[i][j]=tot[i-][j]+map[i][j];
}
}
for(l=;l<n;l++)
{
for(i=;i+l<=n;i++)
{
for(j=,f=;j<=n;j++)
{
f+=tot[i+l][j]-tot[i-][j];
ans=max(ans,f);
if(f<)f=;
}
}
}
printf("%d\n",ans);
}
return ;
}
/*
// //
*/
千万不要点
【动态规划】HDU 1081 & XMU 1031 To the Max的更多相关文章
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- (DP)To The Max --HDU -- 1081
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1081 这道题使用到的算法是:预处理+最大连续子串和 如果会做最大连续子串和,那么理解这题就相对简单一些, ...
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- dp - 最大子矩阵和 - HDU 1081 To The Max
To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)
点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...
- ACM HDU 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- LINUX 内核与 systemtap +GO 专家博客 一个[ 系统软件工程师] 的随手涂鸦
http://nanxiao.me/category/%E3%80%8Anix-hacking%E3%80%8B%E6%9D%82%E5%BF%97/ 月刊 https://github.co ...
- Java基础知识强化之IO流笔记18:FileOutputStream写入数据
1. 创建字节输出流对象,做了几件事情: (1)调用系统功能去创建文件(2)创建fos对象(3)把fos对象指向这个文件 2. 代码示例: package com.himi.fileoutputstr ...
- Block小结
Blocks是C语言的扩充功能.用一句话来表示Blocks的扩充功能:带有自动变量(局部变量)的匿名函数. block其实是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++ ...
- Day10 - Python协程、异步IO、redis缓存、rabbitMQ队列
Python之路,Day9 - 异步IO\数据库\队列\缓存 本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...
- codevs2622数字序列( 连续子序列最大和O(n)算法)
/* 算法描述:维护一个s[p]表示累加和 并且更新最大值ans 如果s[p]<0 则从p+1重新累加 证明:设某个区间的起点和终点分别为s t 分两种情况 1.t<p:设s2表示1到s的 ...
- (转)PHP模板smarty简单入门教程
转之--http://blog.163.com/zf_2011@126/blog/static/166861361201062595057962/ 如何在smarty中开始我们程序设计.PHP代码:- ...
- MSChart使用之动态生成多个多行ChartArea
前台代码: <asp:Chart ID=" > <Titles> <asp:Title Name="Title1" runat=" ...
- MySQL DELETE
MySQL DELETE 语句 你可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 你可以在mysql>命令提示符或PHP脚本中执行该命令. 语法 以下是S ...
- 卸载mysql时,如何卸载干净!
相信很多朋友在使用mysql的过程中都会遇到这样的问题,安装过程出错,或者想要换个版本,或者不想使用了,这个时候我们都需要完全卸载mysql呢?下面,就来谈一谈我的经验. 1.控制面板——>所有 ...
- zookeeper集群一次性启动
编写shell脚本 新建文本,命名为start-zookeeper.sh #!/bin/sh echo "start zkServer…" for i in master work ...