POJ2479【线性DP】
来源:互联网 发布:unity3d 怎么加模型 编辑:程序博客网 时间:2024/05/21 14:58
看完题还懵逼了。。。
题意:
给一个序列,求连续两个序列的最大值。
思路:
正着做一次一段连续序列的最大值。
反着做一次一段连续序列的最大值。
然后对每个位置可得前面最大+后面最大,判断枚举一下就好了。
//#include <bits/stdc++.h>#include<iostream>#include<vector>#include<cstdio>#include<string.h>#include<algorithm>using namespace std;typedef pair<int,int> PII;typedef long long LL;//#pragma comment(linker, "/STACK:102400000,102400000")const int INF=0x3f3f3f3f;const int N=5e4+10;int n;int a[N];int temp1[N],temp2[N];void solve1(){ int cur = a[1]; int ans = a[1]; temp1[1]=ans; for(int i=2;i<=n;i++){ if(cur > 0) cur += a[i]; else cur = a[i]; if(cur > ans) ans = cur; temp1[i] = ans; }}void solve2(){ int cur = a[n]; int ans = a[n]; temp2[n]=ans; for(int i=n-1;i>=1;i--){ if(cur>=0) cur += a[i]; else cur = a[i]; if(cur > ans) ans = cur; temp2[i] = ans; }}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(temp1,0,sizeof(temp1)); memset(temp2,0,sizeof(temp2)); solve1(); solve2(); int ans=-INF; for(int i=1;i<n;i++) ans = max(temp1[i]+temp2[i+1],ans); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ2479【线性DP】
- POJ2479 Maximum sum 线性dp
- poj2479(DP)
- POJ2479 DP
- POJ2479 第一次的DP....
- POJ2479 Maximum sum DP
- poj2479 DP重新50题
- poj2479 DP重新50题
- poj2479
- poj2479
- poj2479
- POJ2479
- POJ2479
- poj2479
- poj2479
- poj2479
- 【POJ2479】Maximum sum(动态规划,DP)
- poj2479(子串和变形,DP)
- redis队列的实现
- springmvc统一的异常处理
- n a^o7 !(山东省第三届ACM大学生程序设计竞赛 )
- 【笔试】try{}finally{}中的return
- 网络流——洛谷P2774 方格取数问题
- POJ2479【线性DP】
- C#:使用MVC和Mybatis(IBatis.Net)搭建项目(二)
- apache ab测试介绍
- 使用Epoll 在 Linux 上开发高性能应用服务器
- Map接口与泛型集合
- Android中设计模式--观察者模式(一对多的关系,对象改变时依赖者都会收到通知)
- c++基础知识(3)
- 内存管理
- git工作模式图示