POJ #2479 - Maximum sum
Hi, I'm back.
This is a realy classic DP problem to code.
1. You have to be crystal clear about what you are going to solve.
2. Apparently there are 2 DP sections
3. Think carefully about recurrence relations
5. Details: take care of indices boundaries - it costed me 1hr to find an indexing bug!
6. Again: think crystal clear before you code!
And, #2593 is exactly the same.
Here is my AC code:
#include <stdio.h> #define MAX_INX 50010
#define Max(a,b) (a)>(b)?(a):(b) int max_sum2(int *pData, int cnt)
{
// DP: Max sum of sub-sequence: dp[i] = MAX(num[i], dp[i-1] + num[i]) // Left -> Right
int lt[MAX_INX] = { };
lt[] = pData[];
for (int i = ; i < cnt; i ++)
{
lt[i] = Max(pData[i], lt[i - ] + pData[i]);
} // Right -> Left
int rt[MAX_INX] = { };
int rtm[MAX_INX] = { };
rt[cnt-] = pData[cnt-];
for (int i = cnt-; i >= ; i--)
{
rt[i] = Max(pData[i], rt[i + ] + pData[i]);
}
rtm[cnt-] = rt[cnt-];
for (int i = cnt-; i >=; i--)
{
rtm[i] = Max(rtm[i+], rt[i]);
} // O(n) to find real max
int ret = -;
for (int i = ; i < cnt - ; i ++)
{
ret = Max(ret, lt[i] + rtm[i+]);
} return ret;
} int main()
{
int nTotal = ;
scanf("%d", &nTotal); while (nTotal--)
{
int num[MAX_INX] = { }; // Get Input
int nCnt = ; scanf("%d", &nCnt);
for (int i = ; i < nCnt; i ++)
{
scanf("%d", num + i);
} printf("%d\n", max_sum2(num, nCnt));
} return ;
}
POJ #2479 - Maximum sum的更多相关文章
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- (线性dp 最大连续和)POJ 2479 Maximum sum
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44459 Accepted: 13794 Des ...
- POJ 2479 Maximum sum(双向DP)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36100 Accepted: 11213 Des ...
- poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 2479 Maximum sum POJ 2593 Max Sequence
d(A) = max{sum(a[s1]..a[t1]) + sum(a[s2]..a[t2]) | 1<=s1<=t1<s2<=t2<=n} 即求两个子序列和的和的最大 ...
- [poj 2479] Maximum sum -- 转载
转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410 ...
- poj 2479 Maximum sum(递推)
题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...
- poj----Maximum sum(poj 2479)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30704 Accepted: 9408 Desc ...
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
随机推荐
- Yii2中自带分页类实现分页
1.首先写控制器层 先引用pagination类 use yii\data\Pagination; 写自己的方法: function actionFenye(){ $data = Fie ...
- UVa 11988 破损的键盘(链表)
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- JavaScript学习记录总结(四)——js函数的特殊性
<script type="text/javascript"> //当局部变量与全局变量 重名的时候 var v="全局变量";//定义全局变 ...
- LB负载均衡集群及DR模式配置
一.系统环境准备: 1.dir服务器 主机名称:dir 系统环境:CentOS release 6.5 (Final) 外网ip:192.168.1.203(网络模式桥接) vip:192.168.1 ...
- kafka的推和拉的问题
之前学习过这一问题,但是面试又被问道了.再次记录下 推还是拉? Kafka最初考虑的问题是,customer应该从brokes拉取消息还是brokers将消息推送到consumer,也就是pull还p ...
- C++@冒号(:)和双冒号(::)的用法
转自:http://blog.csdn.net/zimingjushi/article/details/6549390 1.冒号(:)用法 (1)表示机构内位域的定义(即该变量占几个bit空间) ty ...
- /bin/rm: Argument list too long解決方法
rm.cp.mv是unix下面常用到的檔案處理指令,當我們需要刪除大量的log檔案,如果檔案數太多就會出現此訊息[/bin/rm: Argument list too long]解決方式如下: 例如要 ...
- Nanopore sensors for nucleic acid analysis 论文阅读笔记
Nanopore sensors for nucleic acid analysis Bala Murali Venkatesan and Rashid Bashir 用于核酸分析的纳米孔传感器 纳米 ...
- Oracle升级前备份和失败回退
一.升级前备份 1.软件备份[root@localhost backup]# su - oracle [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@loca ...
- expect安装去测试
1.下载expect和tcl 下载地址:http://download.csdn.net/download/tobyaries/5754943 2.安装expect tar -zxvf tcl8.4. ...