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

51 4 2 3 1

Output

3

Input

41 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 }

时间: 2024-11-07 12:25:49

Codeforces Round #407 (Div. 2) C Functions again(最大子序列和)的相关文章

Codeforces Round #407 (Div. 2)(争取明天24点前补掉)

A - Anastasia and pebbles(水题) 题意: 一个人有俩口袋,每个口袋最多装k个,然后每天每个口袋里的石头颜色必须都相同,问你最少用几天能装完. 思路: 水题..一不小心写瓷了,以为A题大水题,瞎写也不会T,没仔细想..写了一个T的代码. 1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 int n, k; 6 cin >> n >> k; 7 vector&l

Codeforces Round #407 (Div. 2)解题报告

好遗憾没参加这场比赛--vp取得了目前最高的名次orz A. Anastasia and pebbles 向上取整处理即可. 1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 typedef long long ll; 8 typed

Codeforces Round #407 (Div. 2)

A 模拟 B 大力分类讨论 |b1|?>?l - answer is 0. b1?=?0 - if 0 is present in array a than answer is 0, else inf. q?=?1 - if b1 is present in array a than answer is 0, else inf. q?=??-?1 - if both b1 and ?-?b1 are present in array a than answer is 0, otherwise i

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #Pi (Div. 2) (STL专场)

Codeforces Round #Pi (Div. 2) A - Lineland Mail 水题,拼手速. /* * @author Novicer * language : C++/C */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd