codeforces 788-A. Functions again(dp)
来源:互联网 发布:搞笑的日剧 知乎 编辑:程序博客网 时间:2024/04/30 07:12
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】满足公式要求,也就是相邻两个数之差的绝对值乘以-1的次数次方,奇数位为正,偶数位为负,要求最后和最大。
解题思路:
很容易想到dp,如果从奇数位开始,那么奇数位必然为正,偶数位必然为负,反之,如果从偶数位开始,那么偶数位必然为正,奇数位必然为负。只要分奇偶循环各自走一遍,求最大和就好了。
#include<iostream>#include<cmath>#include<algorithm>using namespace std;long long d[100005],s[100005],a[100005],p[100005];int main(){ long long n; while(cin>>n) { for(int i=0;i<n;i++) cin>>s[i]; for(int i=1;i<n;i++) a[i]=abs(s[i]-s[i-1]); d[1]=a[1]; d[2]=max(d[1]-a[2],a[2]); for(int i=3;i<n;i+=2) //奇数位开头 d[i]=max(a[i],d[i-2]-a[i-1]+a[i]); p[2]=a[2]; p[3]=max(a[3],p[2]-a[3]); for(int i=4;i<n;i+=2) //偶数位开头 p[i]=max(a[i],p[i-2]-a[i-1]+a[i]); long long ans=d[1]; for(int i=3;i<n;i+=2) ans=max(ans,d[i]); for(int i=2;i<n;i+=2) ans=max(ans,p[i]); cout<<ans<<endl; } return 0;}
- codeforces 788-A. Functions again(dp)
- Codeforces-788A Functions again(dp)
- codeforces 788A Functions again(dp之最大子段和)【模板】
- Codeforces 789 C Functions again (dp)
- Codeforces#407C. Functions again(DP)
- Codeforces Round #407 Functions again(dp,贪心)
- codeforces 788 A. Functions again(最大连续子序列和)
- Codeforces 789C A. Functions again
- codeforce 789 C Functions again (dp)
- CodeForces 789C Functions again
- Codeforces Round #407 (Div. 2) C. Functions again(前缀和)
- Codeforces Round #407 (Div. 2) C.Functions again(789C)
- Functions again
- Codeforces 538E. Demiurges Play Again (树形DP)
- CodeForces 256A (dp)
- codeforces 666A (DP)
- Codeforces Round #407 (Div. 2) C. Functions again
- CodeForces 789C Functions again(奇偶分类与递推)
- 渣渣渣变渣渣系列(3)
- 线程池
- JAVA-2.3-Fibonacci数列趣味思考
- Java虚拟机中的Class类文件结构
- matlab数字图像分析
- codeforces 788-A. Functions again(dp)
- 数字求特征值
- Shell函数
- 链表
- JavaScript之Date类型(引用类型)
- 2017年3月30日考试总结
- 数据结构(c++)(1)-- 栈
- <转>计算机专业书单推荐
- Game of Cards Gym