poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127)。
题目链接:http://poj.org/problem?id=1050
——>>将二维压缩为一维。对一维进行dp求解。
将二维压缩成一维:
1、第1行
2、第2行加第1行
3、第3行加第2行加第1行
……
N、第N行加第N-1行加……加第1行
1、第2行
2、第3行加第2行
……
1、第N行
对于一维情况。设dp[i]表示以第i个元素结尾的最大连续和,则状态转移方程为:
dp[i] = max(nBuf[i], dp[i - 1] + nBuf[i]);
加上滚动数组思想优化空间。。
总时间复杂度:O(N ^ 3)
#include <cstdio>
#include <cstring>
#include <algorithm> using std::max; const int MAXN = 100 + 1;
const int INF = 0x3f3f3f3f; int N;
int nMatrix[MAXN][MAXN]; void Read()
{
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= N; ++j)
{
scanf("%d", &nMatrix[i][j]);
}
}
} void Dp()
{
int nRet = -INF;
int nBuf[MAXN]; for (int i = 1; i <= N; ++i)
{
memset(nBuf, 0, sizeof(nBuf));
for (int j = i; j <= N; ++j)
{
for (int k = 1; k <= N; ++k)
{
nBuf[k] += nMatrix[j][k];
} int dp = 0;
for (int k = 1; k <= N; ++k)
{
dp = max(nBuf[k], dp + nBuf[k]);
nRet = max(nRet, dp);
}
}
} printf("%d\n", nRet);
} int main()
{
while (scanf("%d", &N) == 1)
{
Read();
Dp();
} return 0;
}
poj - 1050 - To the Max(dp)的更多相关文章
- poj - 1953 - World Cup Noise(dp)
题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:id=1953" target="_blank">h ...
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- POJ 2168 Joke with Turtles(DP)
Description There is a famous joke-riddle for children: Three turtles are crawling along a road. One ...
- POJ 1485:Fast Food(dp)&& 面试题
题目链接 题意 给出 n 个餐厅,m 个停车场,现在要将 n 个餐厅中的 m 个变成停车场,使得每个餐厅到最近的停车场的距离之和最短,输出哪个餐厅变成停车场和它服务哪些餐厅,还有最短距离之和. 思路 ...
- POJ 2533——Longest Ordered Subsequence(DP)
链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...
- 【POJ 3176】Cow Bowling(DP)
题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...
- HDU 1081 To The Max (dp)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- 【POJ】3616 Milking Time(dp)
Milking Time Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10898 Accepted: 4591 Des ...
随机推荐
- JAVA实现将GeoHash转化为相应的经纬度坐标
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50568428 http://www.llwjy.com/blogdetail/f ...
- 【Android 系统开发】使用 Source InSight 阅读 Android 源代码
1. 安装 Source Insight (1) Source Insight 相关资源 安装相关资源 : -- 下载地址 : http://www.sourceinsight.com/down35. ...
- 赵雅智_android获取本机运营商,手机号部分能获取
手机号码不是全部的都能获取.仅仅是有一部分能够拿到. 这个是因为移动运营商没有把手机号码的数据写入到sim卡中.SIM卡仅仅有唯一的编号.供网络与设备 识别那就是IMSI号码,手机的信号也能够说是通过 ...
- SOAPUI 安装及破解
转自:https://blog.csdn.net/henni_719/article/details/79000130 先下载SOAPUI,我这里用的是5.1.2 PRO 版 下载路径:http:/ ...
- 在Android源码下编译jni所需要知道的事~
以下只是自己的一些总结,欢迎讨论 通过NDK编译jni网上有很多例子,在这我只总结在Android源码下编译 1.android源码环境下编译so包,编出来的.so的包前面不会自动给添加lib,NDK ...
- User_Login_Register_Shopping+装饰器 3.0
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/5/27 0027 14:07# @Author : Anthony.Waa# @ ...
- windows phone LongListSelector加载下一页
LongListSelector利用ListHeader.ListFooter加载上一页和下一页XAML代码: <phone:LongListSelector> <phone:Lon ...
- java 遍历list 模糊查询用
java 遍历list 模糊查询用 /** * 筛选list * @param list 要筛选的list * @param key 筛选的key * @param <T> * @retu ...
- Unity3d 拖拽脚本报错 Can’t add script
报错截图: 报错原因: c#文件创建以后再改名,会报错找不到对应类. 类名和文件名要一致才行.(这个是Unity要求,c#本身不要求一致)
- jquery获取自定义属性的值
//获取属性值 1 <div id="text" value="黑哒哒的盟友"><div> jQuery取值: $("#tex ...