Codeforces788A Functions again 2017-04-12 18:22 56人阅读 评论(0) 收藏
1 second
256 megabytes
standard input
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.
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.
5
1 4 2 3 1
3
4
1 5 4 7
6
——————————————————————————————————————
题目的意思是选取一段区间,使得满足关系式的值最大
思路:观察关系式我们可以知道,它由相邻两项之差,由某项开始奇数项取正偶数项取负求和而得,我们可以预处理出相邻的差,构造两个序列,一个奇正偶负,一个相反,然后求最大字段和
#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) 收藏的更多相关文章
- Doubles 分类: POJ 2015-06-12 18:24 11人阅读 评论(0) 收藏
Doubles Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19954 Accepted: 11536 Descrip ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- 移植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 ...
- iOS开发:创建真机调试证书 分类: ios相关 2015-04-10 10:22 149人阅读 评论(0) 收藏
关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架发布问题.今天就着重说一下关于针对于苹果 ...
- iOS 消息推送原理及实现总结 分类: ios技术 2015-03-01 09:22 70人阅读 评论(0) 收藏
在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服 ...
- Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏
全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...
- 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 ...
- 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 ...
- *** glibc detected *** malloc(): memory corruption 分类: C/C++ Linux 2015-05-14 09:22 37人阅读 评论(0) 收藏
*** glibc detected *** malloc(): memory corruption: 0x09eab988 *** 发现是由于memset越界写引起的. 在Linux Server上 ...
随机推荐
- Unable to locate \.nuget\NuGet.exe 问题解决办法之一
问题出现的原因是项目下.nuget文件夹下NuGet.exe文件夹不存在导致的 解决办法: 1.右键编辑NuGet.targets文件 将下载NuGet.exe的配置节点DownloadNuGetEx ...
- 使用Spring发送Email
配置Spring发送邮件 Spring发送邮件底层还是使用JavaMail,我在http://www.cnblogs.com/lz2017/p/6882925.html 中记录过关于JavaMail的 ...
- 需登录账号与密码的网页爬取demo
public static String connect(String dataUrl){ String result = null; try { HttpClient httpclient = ne ...
- springboot 测试 有注入HttpSession的bean
question: nested exception is java.lang.IllegalStateException: No thread-bound request found: Are yo ...
- SourceTree安装和使用
SourceTree 安装 需要注意的是在指定路径下添加一个json文件.就可以跳过身份验证,直接运行软件. SourceTree 的SSH配置 软件启动后,如果需要和远程的Gitlab仓库进行交互, ...
- np的归纳总结
1. np.sqrt(input) # 求数的开方 import numpy as np print(np.sqrt(2)) 2. np.square(3) # 求数的平方 import n ...
- Tomcat 实战-调优方案
来自: http://blog.csdn.net/u010028869/article/details/51793821 来自: https://www.cnblogs.com/baihuites ...
- js动态加载数据到文本框指定的位置
html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- Git----时光穿梭机01
看这篇文章之前可以先阅读 https://www.cnblogs.com/cxq0017/p/9645944.html 创建版本库这篇文章 我们已经成功地添加并提交了一个readme.txt文件,现 ...
- Work-Stealing in .NET 4.0
[Work-Stealing in .NET 4.0] 1.线程按LIFO取Task,因为最后一个Task很可能还在Cache中,提高命中率. 2.Stealer从FIFO取Task,最先加入的Tas ...