codeforces788A Functions again
来源:互联网 发布:javascript if 编辑:程序博客网 时间:2024/06/07 00:47
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.
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.
Print the only integer — the maximum value of f.
51 4 2 3 1
3
41 5 4 7
6
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.
对于题目给定的式子,求出最大值
只需要分奇偶分别跑一遍最大子串和,取大的就可以了
#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;long long a[100001];long long s[100001];long long ss[100001];inline long long absx(long long x){if(x<0)x=-x;return x;}int main(){int n;scanf("%d",&n);int i;for(i=1;i<=n;i++)scanf("%I64d",&a[i]);for(i=1;i<=n-1;i++){s[i]=a[i+1]-a[i];if(s[i]<0)s[i]=-s[i];}long long ans=0;long long la=0,f=1;for(i=1;i<=n-1;i++){if(la<=0)la=s[i]*f;elsela=la+s[i]*f;ans=max(ans,la);f=-f;}la=0;f=-1;for(i=1;i<=n-1;i++){if(la<=0)la=s[i]*f;elsela=la+s[i]*f;ans=max(ans,la);f=-f;}printf("%I64d\n",ans);return 0;}
- Codeforces788A Functions again
- codeforces788A Functions again
- codeforces788A —— Functions again —— dp
- Functions again
- CodeForces 789C Functions again
- codeforces 788-A. Functions again(dp)
- Codeforces-788A Functions again(dp)
- C. Functions again-最长连续子序列
- Codeforces 789 C Functions again (dp)
- codeforce 789 C Functions again (dp)
- Codeforces#407C. Functions again(DP)
- Codeforces 789C A. Functions again
- Codeforces Round #407 Functions again(dp,贪心)
- 【codeforce】C. Functions again 最大子段和
- Codeforces Round #407 (Div. 2) C. Functions again
- codeforces 788 A. Functions again(最大连续子序列和)
- CodeForces 789C Functions again(奇偶分类与递推)
- functions
- Java 集合——HashMap
- 第225讲:Spark Shuffle Pluggable框架SortShuffle解析以及创建源码详解
- 793A
- 商品展示,SQLite数据库的使用
- [BZOJ 1178][Apio2009]CONVENTION会议中心:贪心+倍增
- codeforces788A Functions again
- JavaScript---对象学习(二)Array对象、Date对象及其重要函数学习
- VMware+ubuntu
- Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机
- 将博客搬至CSDN
- Fire(广搜里写广搜)
- Software Company LightOJ
- Zookeeper安装
- 漫步数理统计二十四——伽玛、卡方与贝塔分布