codeforces 788-A. Functions again(dp)
2017-03-31 21:51
309 查看
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,如果从奇数位开始,那么奇数位必然为正,偶数位必然为负,反之,如果从偶数位开始,那么偶数位必然为正,奇数位必然为负。只要分奇偶循环各自走一遍,求最大和就好了。
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 788A——Functions again(动态规划)
- CodeForces 788A - Functions again [ DP ]
- codeforces 788 A. Functions again(最大连续子序列和)
- CodeForces 710E Generate a String (DP)
- Codeforces 580D Kefa and Dishes(状压DP)
- Codeforces 361D Levko and Array(二分)(DP)
- CodeForces 264C Choosing Balls dp(水
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
- codeforces-148D-Bag of mice-概率DP
- Codeforces 700B Connecting Universities(树形DP)
- codeforces 337D Book of Evil(dp)
- codeforces 894 E(scc + dp)
- CodeForces 30C Shooting Gallery 简单dp
- codeforces 148 D Bag of mice(概率dp)
- CodeForces 825F String Compression---这个KMP很DP
- codeforces 285 D Permutation Sum (状态压缩DP)
- Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp)
- CodeForces 607B:Zuma(区间DP)
- CodeForces 55D Beautiful numbers(数位dp+数论)
- 【CodeForces 261B】Maxim and Restaurant(DP,期望)