(线性dp 最大连续和)POJ 2479 Maximum sum
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 44459 | Accepted: 13794 |
Description
Your task is to calculate d(A).
Input
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Output
Sample Input
1 10
1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13 和poj2593几乎一样。
https://www.cnblogs.com/Weixu-Liu/p/10512447.html
C++代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = ;
int a[maxn],dpl[maxn],dpr[maxn],m1[maxn],m2[maxn];
int Inf = -0x3f3f3f3f;
int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
}
memset(dpl,,sizeof(dpl));
memset(dpr,,sizeof(dpr));
m1[] = m2[n+] = Inf;
for(int i = ; i <= n; i++){
dpl[i] = max(dpl[i-] + a[i],a[i]);
if(m1[i-] < dpl[i])
m1[i] = dpl[i];
else
m1[i] = m1[i-];
}
for(int i = n; i >= ; i--){
dpr[i] = max(dpr[i+] + a[i],a[i]);
if(m2[i+] < dpr[i])
m2[i] = dpr[i];
else
m2[i] = m2[i+];
}
int maxsum = Inf;
int tmp[maxn];
for(int i = ; i <= n-; i++){
tmp[i] = m1[i] + m2[i+];
if(maxsum < tmp[i])
maxsum = tmp[i];
}
printf("%d\n",maxsum);
}
return ;
}
(线性dp 最大连续和)POJ 2479 Maximum sum的更多相关文章
- POJ 2479 Maximum sum(双向DP)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36100 Accepted: 11213 Des ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...
- [poj 2479] Maximum sum -- 转载
转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410 ...
- 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 ...
- poj 2479 Maximum sum(递推)
题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...
- 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} 即求两个子序列和的和的最大 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
随机推荐
- 九、.net core用orm继承DbContext(数据库上下文)方式操作数据库
一.创建一个DataContext普通类继承DbContext 安装程序集:Pomelo.EntityFrameworkCore.MySql 二.配置连接字符串(MySql/SqlServer都 ...
- 四、Mysql主从同步
一.MySQL Replication介绍 MySQL Replication 官方文档 Replication可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave) ...
- captive portal
刷好lineageos后默认浏览器无法上网,实际上并不是没有连上网,而是captive portal即网关设置错误,设置一下即可上网. adb shell "settings put glo ...
- mpi4python
转载:https://zhuanlan.zhihu.com/p/25332041 前言 在高性能计算的项目中我们通常都会使用效率更高的编译型的语言例如C.C++.Fortran等,但是由于Python ...
- How to execute a Stored Procedure with Entity Framework Code First
Recently I worked on a project, which I started as code first and then I forced to switch to Databas ...
- git 出现错误时
Your local changes to the following files would be overwritten by merge: 解决办法 如果希望保留生产服务器上所做的改动,仅仅并入 ...
- BZOJ3812 主旋律(状压dp+容斥原理)
设f[S]为S点集是SCC的方案数.考虑通过去掉不合法方案转移.可以枚举入度为0的SCC所含点集S',这样显然S^S'内部的边和由S'连向S^S'的边删还是不删任选.但是这样无法保证S'包含所有入度为 ...
- HUST 1541 解方程
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6394836.html 1541 - Student’s question 时间限制:1秒 内存限制 ...
- Quartus prime 16.0 中通过JTAG固化程序
前言 下载项目sof文件到开发板中,掉电后会消失:由于开发板有JTAG口,则可以用JTAG固化jic文件到EPCS16芯片中. 流程 1.打开quartus软件并打开convert programmi ...
- 【刷题】BZOJ 2759 一个动态树好题
Description 有N个未知数x[1..n]和N个等式组成的同余方程组: x[i]=k[i]*x[p[i]]+b[i] mod 10007 其中,k[i],b[i],x[i]∈[0,10007) ...