考虑枚举每个子串开头的位置,然后答案转化成询问这个位置之后 哪个位置的前缀和 - 这位置的前缀和 最大(当然是已经把绝对值和正负的情况处理好了,可以发现按奇偶,这个序列只有两种情况),只需要预处理这两个序列的前缀和,以及这两个前缀和序列的后缀max即可。

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
int Abs(int x){
return x<0 ? (-x) : x;
}
int n;
int a[100010],b[100010];
ll d[100010],c[100010],e[100010],f[100010],ans;
int main(){
// freopen("c.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
}
for(int i=1;i<n;++i){
b[i]=Abs(a[i+1]-a[i]);
}
int op=1;
for(int i=1;i<n;++i){
c[i]=c[i-1]+(ll)(b[i]*op);
op=-op;
}
op=-1;
for(int i=1;i<n;++i){
d[i]=d[i-1]+(ll)(b[i]*op);
op=-op;
}
ll maxn=-1e18;
for(int i=n-1;i>=1;--i){
maxn=max(maxn,c[i]);
e[i]=maxn;
}
maxn=-1e18;
for(int i=n-1;i>=1;--i){
maxn=max(maxn,d[i]);
f[i]=maxn;
}
for(int i=1;i<n;i+=2){
ans=max(ans,e[i]-c[i-1]);
}
for(int i=2;i<n;i+=2){
ans=max(ans,f[i]-d[i-1]);
}
cout<<ans<<endl;
return 0;
}

【预处理】Codeforces Round #407 (Div. 2) C. Functions again的更多相关文章

  1. Codeforces Round #407 (Div. 2) B+C!

    B. Masha and geometric depression 被这个题坑了一下午,感觉很水,一直WA在第14组,我那个气啊,结束后发现第14组有点小争议,于是找出题人解释,然后出题人甩给了我一段 ...

  2. Codeforces Round #407 (Div. 1)

    人傻不会B 写了C正解结果因为数组开小最后RE了 疯狂掉分 AC:A Rank:392 Rating: 2191-92->2099 A. Functions again 题目大意:给定一个长度为 ...

  3. Codeforces Round #407 (Div. 2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.Anastasia and ...

  4. Codeforces Round #407 (Div. 2)A B C 水 暴力 最大子序列和

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. Codeforces Round #407 (Div. 1) B. Weird journey —— dfs + 图

    题目链接:http://codeforces.com/problemset/problem/788/B B. Weird journey time limit per test 2 seconds m ...

  6. Codeforces Round #407 (Div. 2) D,E

    图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #407 (Div. 2) D. Weird journey(欧拉路)

    D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. 【分类讨论】Codeforces Round #407 (Div. 2) D. Weird journey

    考虑这个二元组中有一者是自环,则必然合法. 考虑这两条边都不是自环,如果它们不相邻,则不合法,否则合法. 坑的情况是,如果它是一张完整的图+一些离散的点,则会有解,不要因为图不连通,就误判成无解. # ...

  9. 【分类讨论】【set】Codeforces Round #407 (Div. 2) B. Masha and geometric depression

    模拟一下那个过程,直到绝对值超过l,或者出现循环为止. 如果结束之后,绝对值是超过l的,就输出当前写在黑板上的数量. 如果出现循环,则如果写在黑板上的数量非零,则输出inf(注意!如果陷入的循环是一个 ...

随机推荐

  1. VMware Workstation Pro 14 序列号

    VMware Workstation Pro 14 序列号: AA702-81D8N-0817Y-75PQT-Q70A4 YC592-8VF55-M81AZ-FWW5T-WVRV0 FC78K-FKE ...

  2. 移动端 H5 页面注意事项

    1. 单个页面内容不能过多 设计常用尺寸:750 x 1334 / 640 x 1134,包含了手机顶部信号栏的高度. 移动端H5活动页面常常需要能够分享到各种社交App中,常用的有 微信.QQ 等. ...

  3. LeetCode 19 Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. python实战===itchat

    import itchat itchat.login() friends=itchat.get_friends(update=True)[0:] male=female=other=0 for i i ...

  5. mybaits-spring demo 记

    代码:https://github.com/bobby96333/demo_spring_batis /pom.xml <?xml version="1.0" encodin ...

  6. redis cluster 实现

    Redis cluster是一个redis官方提供的集群功能,集群节点最小3个节点,配置比较多,记录下来,以供下次使用.我在这使用的redis 4.0.6. 因为最新的ruby redis扩展需要ru ...

  7. 算法题之Median of Two Sorted Arrays

    这道题是LeetCode上的题目,难度级别为5,刚开始做没有找到好的思路,以为是自己智商比较低,后来发现确实也比较低... 题目: There are two sorted arrays nums1  ...

  8. 【HDU5306】Gorgeous Sequence

    这个题目是Segment-Tree-beats的论文的第一题. 首先我们考虑下这个问题的不同之处在于,有一个区间对x取max的操作. 那么如何维护这个操作呢? 就是对于线段树的区间,维护一个最大值标记 ...

  9. vscode和phpStorm使用xdebug调试设置

    phpStorm http://www.cnblogs.com/cxscode/p/7045944.html http://www.cnblogs.com/cxscode/p/7050781.html ...

  10. http之post方法 提交数据的四种方法

    http协议中,post方法用来向服务端提交数据, 这里介绍四种方式: application/x-www-form-urlencoded multipart/form-data applicatio ...