C. Functions again
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

——————————————————————————————————————

题目的意思是选取一段区间,使得满足关系式的值最大

思路:观察关系式我们可以知道,它由相邻两项之差,由某项开始奇数项取正偶数项取负求和而得,我们可以预处理出相邻的差,构造两个序列,一个奇正偶负,一个相反,然后求最大字段和

#include <iostream>
#include<queue>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define LL long long
LL a[100005];
LL b[100005];
LL c[100005]; int main()
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%lld",&a[i]);
}
for(int i=1; i<n; i++)
{
LL x=fabs(a[i]-a[i-1]);
if(i%2) b[i]=x;
else b[i]=-x;
c[i]=-b[i];
}
LL s=0,mx=0;
for(int i=1; i<n; i++)
{
if(s+c[i]<0) s=0;
else s+=c[i];
if(s>mx) mx=s;
}
s=0;
for(int i=1; i<n; i++)
{
if(s+b[i]<0) s=0;
else s+=b[i];
if(s>mx) mx=s; }
printf("%lld\n",mx);
return 0;
}

Codeforces788A Functions again 2017-04-12 18:22 56人阅读 评论(0) 收藏的更多相关文章

  1. Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏

    Doubles Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19954   Accepted: 11536 Descrip ...

  2. highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏

    /*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...

  3. 移植QT到ZedBoard(制作运行库镜像) 交叉编译 分类: ubuntu shell ZedBoard OpenCV 2014-11-08 18:49 219人阅读 评论(0) 收藏

    制作运行库 由于ubuntu的Qt运行库在/usr/local/Trolltech/Qt-4.7.3/下,由makefile可以看到引用运行库是 INCPATH = -I/usr//mkspecs/d ...

  4. iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏

    关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...

  5. iOS 消息推送原理及实现总结 分类: ios技术 2015-03-01 09:22 70人阅读 评论(0) 收藏

    在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服 ...

  6. Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏

    全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...

  7. HDU6023 Automatic Judge 2017-05-07 18:30 73人阅读 评论(0) 收藏

    Automatic Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. How to create your own custom 404 error page and handle redirect in SharePoint 分类: Sharepoint 2015-07-08 00:22 4人阅读 评论(0) 收藏

    1. In your MOSS server, make a copy of %systemdrive%\Program Files\Common Files\Microsoft Shared\Web ...

  9. *** glibc detected *** malloc(): memory corruption 分类: C/C++ Linux 2015-05-14 09:22 37人阅读 评论(0) 收藏

    *** glibc detected *** malloc(): memory corruption: 0x09eab988 *** 发现是由于memset越界写引起的. 在Linux Server上 ...

随机推荐

  1. ExtJs 扩展类CheckColumn的使用(事件触发)

    [javascript] view plain copy print? 使用 Extjs 在进行数据库编程经常会遇到 checkbox 的问题(奇怪网上却没有此类文章不知道其他人是怎么解决的,在此贴上 ...

  2. Bootstrap学习之路(1)---开篇-登陆页

    Bootstrap是现在很流行的一套前端框架,尤其是它的自适应,真的很不错,而且对移动设备也很友好,可以达到快速开发的效果,最近想把自己的网站弄个手机版,很果断的就选用了bootstrap,跟大家分享 ...

  3. 洛谷[P1002]过河卒

    原题地址:https://www.luogu.org/problemnew/show/P1002 题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点 ...

  4. mariadb master and salve configure

    mariadb master and salve configure 主从复制配置: master:192.168.8.200 salve:192.168.8.201 主服务器配置: 主服务器需要启动 ...

  5. ubuntu安装了mysql 但是编译报错 mysql.h: No such file or directory

    在Ubuntu体系中,已经安装了mysql,即应用sudo apt-get install mysql-server mysql-client 但是用C编译mysql数据库时,报错fatal erro ...

  6. sql在外键存在的情况下删除表

    SQL Server 批量 停用/启用 外键约束 今天百度知道上面,看到这样一个要求: 现在有一个库,有很多张表想要删除一张表的记录的时候,由于外键关联太多,所以,没法删除相应的记录,谁能帮忙写个存储 ...

  7. js 取元素相对页面的高度和宽度

    function pos(elem) {    var x = elem.offsetLeft,  y = elem.offsetTop;    while (elem = elem.offsetPa ...

  8. cpu真实核数

    判断依据: 1.具有相同core id的cpu是同一个core的超线程. 2.具有相同physical id的cpu是同一颗cpu封装的线程或者cores. 英文版: 1.Physical id an ...

  9. nginx正向代理访问百度地图API

    正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器这个代理服务器呢,他能访问那个我不能访问的网站于是我先连上代 ...

  10. Sublime Text 3 安装+注册+汉化

    参考:https://www.cnblogs.com/h--d/p/7555119.html