我是靠谱客的博主 曾经红牛,这篇文章主要介绍POJ 2593&&2479:Max Sequence,现在分享给大家,希望可以做个参考。

Max Sequence
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 16329 Accepted: 6848

Description

Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). 


You should output S. 

Input

The input will consist of several test cases. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
5
-5 9 -5 11 20
0

Sample Output

复制代码
1
40

题意是给出一个序列,求这个序列中两个子序列的和的最大值。
两三年前切了POJ2479,但当时还很不理解dp (当然现在对dp的理解程度也就能切切dp水题。。。)。所以做这道题的时候无限感慨。
其实求一个序列的和的最大值很简单,即dp[i]=max(dp[i-1]+value[i], value[i])
现在它要求两个序列的和的最大值。所以想到从左边来一次,从右边来一次。
left[i]表示从第1个数字到当前第i个数字为止,左边的最大序列和。
right[i]表示从第Test个数字(从右向左)到第i个数字为止,右边的最大序列和。

代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream> #include <vector> #include <algorithm> using namespace std; int left_v[100005]; int right_v[100005]; int value[100005]; int main() { int Test; while(cin>>Test) { if(!Test) break; left_v[0]=0; right_v[0]=0; left_v[Test+1]=0; right_v[Test+1]=0; int i,max_v=-100000000; for(i=1;i<=Test;i++) { cin>>value[i]; } left_v[1]=value[1]; right_v[Test]=value[Test]; for(i=2;i<=Test;i++) { left_v[i]=max(left_v[i-1]+value[i],value[i]); } for(i=Test-1;i>=1;i--) { right_v[i]=max(right_v[i+1]+value[i],value[i]); } for(i=2;i<=Test;i++) { left_v[i]=max(left_v[i-1],left_v[i]); } for(i=Test-1;i>=1;i--) { right_v[i]=max(right_v[i+1],right_v[i]); } for(i=1;i<Test;i++) { if(left_v[i]+right_v[i+1]>max_v) max_v=left_v[i]+right_v[i+1]; } cout<<max_v<<endl; } return 0; }

自己把这道题A掉,相当开心。2015/7/5

最后

以上就是曾经红牛最近收集整理的关于POJ 2593&&2479:Max Sequence的全部内容,更多相关POJ内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部