Codeforces Round #407 (Div. 2) C Functions again(最大子序列和)

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements.

Output

Print the only integer — the maximum value of f.

Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note

In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.

题意:给你一个长度为n的序列,问你在 l 与 r 取不同值时最大值是多少?

思路:能够看出这就是在abs(a[i]-a[i+1])取正负正负···和取负正负正···然后求子序列的最大值

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=100005;
 4 long long a[maxn],b[maxn],c[maxn];
 5 int main()
 6 {
 7 
 8     int n;
 9     while(~scanf("%d",&n))
10     {
11         for(int i=0; i<n; i++)
12         {
13             scanf("%I64d",&c[i]);
14         }
15         for(int i=0; i<n-1; i++)
16         {
17             if(i%2==0)
18             {
19                 a[i]=abs(c[i]-c[i+1]);
20                 b[i]=-a[i];
21             }
22             else
23             {
24                 b[i]=abs(c[i]-c[i+1]);
25                 a[i]=-b[i];
26             }
27         }
28         long long ans=0,num=0;
29         for(int i=0; i<n-1; i++)
30         {
31             num+=a[i];
32             if(num>ans)
33             {
34                 ans=num;
35             }
36             else if(num<0)
37             {
38                 num=0;
39             }
40         }
41         num=0;
42         for(int i=0; i<n-1; i++)
43         {
44             num+=b[i];
45             if(num>ans)
46             {
47                 ans=num;
48             }
49             else if(num<0)
50             {
51                 num=0;
52             }
53         }
54         printf("%I64d\n",ans);
55     }
56     return 0;
57 }
View Code

转载于:https://www.cnblogs.com/wang-ya-wei/p/6686108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值