POJ 2479 Maximum sum(2次最大字段和)
来源:互联网 发布:适合编程的笔记本电脑 编辑:程序博客网 时间:2024/05/22 11:29
题目地址:http://poj.org/problem?id=2479
思路:先从前往后扫一遍最大字段和,再从后往前加上从前往后的最大字段和,即可求出2个最大的字段和
AC代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x7f7f7f7f;//2139062143typedef long long ll;using namespace std;int a[50010];int dp[50010];int main(){ int t,n; scanf("%d",&t); while(t--) { int max = -10010; scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d",&a[i]); } int sum = 0; for(int i=0; i<n; i++) { sum += a[i]; if(sum > max) { max = sum; } dp[i] = max; if(sum < 0) { sum = 0; } } max = -10010; int max1 = -inf; sum = 0; for(int i=n-1; i>0; i--) { sum += a[i]; if(sum > max) { max = sum; } if(dp[i-1] + max > max1) { max1 = dp[i-1] + max; } if(sum < 0) { sum = 0; } } printf("%d\n",max1); } return 0;}
0 0
- POJ 2479 Maximum sum(2次最大字段和)
- poj-2479 Maximum sum 【最大字串和】
- poj 2479 Maximum sum (子序列和最大问题)
- POJ 2479 Maximum sum(dp—求最大的两个不相交的字段和)
- POJ 2479 Maximum sum (求2个不相交的连续字段和的最大值)
- POJ-2479-Maximum sum-双向dp-2次连续区间和
- Maximum sum(poj 2479 最大m子段和)
- POJ 2479 Maximum sum (DP&双最大子段和)
- poj 2479 Maximum sum 两段不相交最大子段和
- poj 2479 Maximum sum(dp&最大子段和)
- Poj 2479 Maximum sum【双向DP/最大连续和】
- poj 2479 Maximum sum 最大数字连续和
- POJ 2479 Maximum sum & POJ 2573 Max Sequence (DP,最大连续子串和)
- POJ 1050 To the Max && POJ 2479Maximum sum(DP最大连续子段和)
- poj 2479 Maximum sum(求最大子段和的延伸)
- poj 2479 Maximum sum(求最大子段和的延伸)
- poj 2479 Maximum sum(简单dp,最大连续和拓展)
- uva108 - Maximum Sum(最大和)
- ssh (MVC软件开发模式)
- 策略模式
- Android:BaseListAdapter——摒弃ViewHolder使开发更高效的BaseAdapter
- CRITICAL_SECTION //critical_section
- View滑动动画的选择及其left, marginLeft, translationX参数的总结
- POJ 2479 Maximum sum(2次最大字段和)
- 如何让Window批处理文件可以支持带有空格的路径
- Defaulted 和 Deleted 函数--C++11
- Troubleshooting: 'Log file sync' Waits
- 围住神经猫
- Android Project Butter分析
- Lock与synchronized 的区别
- 小计一下
- 79.View the Exhibit and examine the structure of the CUSTOMERS table. In the CUSTOMERS table, the CU