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. mkdir -p 多层次目录创建

    mkdir的-p选项允许你一次性创建多层次的目录,而不是一次只创建单独的目录.例如,我们要在当前目录创建目录Projects/a/src,使用命令 1 mkdir -p Project/a/src 而 ...

  2. erlang的一些系统限制修改

    atom个数限制 +t xxx 进程数限制 +P xxxx ets表个数限制 +e xxx ports个数限制 +Q xxxx 查看限制 string:tokens(binary_to_list(er ...

  3. 黄聪:Microsoft office 2013版下载、安装及破解工具下载破解教程(Windows Toolkit)

    Microsoft Office 2013(Office 15)是微软的新一代Office办公软件,全面采用Metro界面.Microsoft Office 2013官方下载(Office2013专业 ...

  4. Java File文件操作 创建文件\目录,删除文件\目录

    Java手册 java.io 类 File java.lang.Object java.io.File 所有已实现的接口: Serializable, Comparable<File> p ...

  5. java之Jsch实现Linux的文件上传与下载

    一.JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功 ...

  6. tensorflow-windows下安装,python3.6

    安装: pip install tensorflow ps:我第一次安装了,但是导入却失败了. 进入\python3\Lib\site-packages\删除了tensorflow,再次pip ins ...

  7. leetcode532

    public class Solution { public int FindPairs(int[] nums, int k) { var pair = new Dictionary<strin ...

  8. How do I prevent Eclipse from hanging on startup?

    Under Eclipse 3.6 (Helios), the corresponding file seems to be .metadata/.plugins/org.eclipse.core.r ...

  9. Daylight Saving Time

    [Daylight Saving Time] 夏时制,又称日光节约时制.日光節約時間(英语:Daylight saving time)或夏令时间(英语:Summer time),是一种为节约能源而人为 ...

  10. thymeleaf 获取项目路径

    <p th:text=${salecode}></p> <a th:href="${#httpServletRequest.getScheme()+'://'+ ...