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

    动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...

  2. fbx sdk

    autodesk fbx review autodesk fbx review http://www.greenxf.com/soft/169025.html autodesk fbx review( ...

  3. Linux系统编程——进程间通信(System V IPC 对象)

    基本查看命令 ipcs  -m查看共享内存        ipcs -s查看信号量        ipcs -q查看消息队列 ipcrm  -m  id 删除共享内存   -M+key值 ipcrm ...

  4. leetcode 之Reverse Nodes in k-Group(22)

    这题有点繁琐,在更新指针时很容易出错. ListNode *reverseKGroup(ListNode *head, int k) { )return head; ListNode dummy(-) ...

  5. java获取项目路径,url路径

    我的web项目名iamgeModel. 工作空间在D盘 先获取url相关: 需要是HttpServletRequest request; 获取IP: request.getServerName() / ...

  6. mysql远程访问cannot connect(10038) 问题解决的过程

    今天用Navicat访问虚拟机上的mysql,无法访问报cannot connect(10038). 首先看是否可以telnet,本机cmd,telnet 192.168.209.128 3306,结 ...

  7. JAVA二叉树的创建以及各种功能的实现

    直接上代码了,代码说得很清楚了 package BTree; public class BTree { private Node root; private class Node { private ...

  8. System.Web.HttpContext.Current.Request用法

    public static void SetRegisterSource() { if (System.Web.HttpContext.Current.Request["website&qu ...

  9. ng配置301及反向代理示例

    server { listen 80; server_name fpb.com; return 301 http://www.fpb.com$request_uri; } server { liste ...

  10. 解决ssh登录过慢问题

    1.首先打开debug,看看慢在哪里 [root@BC-NFS1 ~]# ssh -v root@192.168.102.41 -p 22 OpenSSH_6.6.1, OpenSSL 1.0.1e- ...