poj-2479 简单DP
来源:互联网 发布:gta5帧数怎么优化 编辑:程序博客网 时间:2024/05/18 13:27
题目来源:http://poj.org/problem?id=2479
题目大意就是求一个数组里面的两端连续值得最大和
DP从前往后扫一遍 再从后往前扫一遍
然后求1~i 和i+1~n的和的最大值
注意题目要求必须得最低取两个数
#include <iostream>#include <cstdio>#define MAXN 50010#define MIN -10000using namespace std;int leftmax[MAXN],rightmax[MAXN]; //两个数组记录从左到右和从右到左前i个的最大值int a[MAXN];int n;void maxl(){ int max=a[0]; leftmax[0]=max; for(int i=1; i<n; i++) { max+=a[i]; if(max>0) { if(leftmax[i-1]<max) leftmax[i]=max; else leftmax[i]=leftmax[i-1]; } else { max=0; leftmax[i]= a[i]>leftmax[i-1]? a[i]:leftmax[i-1]; } }}void maxr(){ int max=a[n-1]; rightmax[n-1]=a[n-1]; for(int i=n-2; i>0; i--) { max+=a[i]; if(max>0) { if(rightmax[i+1]<max) rightmax[i]=max; else rightmax[i]=rightmax[i+1]; } else { max=0; rightmax[i]=a[i]>rightmax[i+1]?a[i]:rightmax[i+1]; } }}int main(){ int T; scanf("%d",&T); while(T--) { int max=MIN; scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d",&a[i]); maxl(); maxr(); for(int i=0; i<n-1; i++) //必须最低取两个数 if(leftmax[i]+rightmax[i+1]>max) max=leftmax[i]+rightmax[i+1]; printf("%d\n",max); }}
- poj-2479 简单DP
- POJ 2479 (简单dp)
- POJ 2181简单DP
- POJ-2004 简单DP..
- poj 2385 简单dp
- poj 3486 简单dp
- poj 2353 简单dp
- poj-2355-简单dp
- poj 3267 简单dp
- poj 3132简单dp
- poj 3280 简单dp
- poj 2948 简单dp
- POJ 3176 简单DP
- POJ 3176 简单DP
- poj 3616(简单dp)
- poj 3920(简单dp)
- poj 2355(简单dp)
- poj 3616 简单dp
- Linux系统中 bashrc 和 profile 的区别
- php缓存小技巧
- 使用Spring JavaMail发送邮件总结
- Hadoop中的fsimage和edits(能力工场--Hadoop)
- MFC实现数据库的基本操作
- poj-2479 简单DP
- JS面试题及答案
- Hello CSDN Blog!
- 两个View同步滚动
- 在浏览器中检查是否安装jre
- 在VC多文档中新建指定文档和关闭指定文档
- 如何利用VS2010安装和部署应用程序
- fgets()、gets()、EOF、feof()
- [leetcode刷题系列]ZigZag Conversion