Codeforces_789C_(dp)
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 ≤ 10^5) — the size of the array a.
The second line contains n integers a1, a2, ..., an (-10^9 ≤ ai ≤ 10^9) — 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
题意:求公式的最大值。
思路:
b[i]:abs(num[i]-num[i+1]),c[N]:-abs(num[i]-num[i+1]) (i%2==1)
b[i]:-abs(num[i]+num[i+1]),c[N]:abs(num[i]-num[i+1]) (i%2==0)
然后分别求b,c的区间连续最大值,二者中较大着就是答案。
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define LL long long
#define INF 10000000005
int num[N],b[N],c[N]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&num[i]);
for(int i=;i<n-;i++)
{
if(i%)
{
b[i]=abs(num[i]-num[i+]);
c[i]=-abs(num[i]-num[i+]);
}
else
{
b[i]=-abs(num[i]-num[i+]);
c[i]=abs(num[i]-num[i+]);
}
}
LL res1=-INF,res2=-INF,tmp=,l=;
for(int i=;i<n-;i++)
{
tmp+=b[i];
if(tmp>res1)
res1=tmp;
if(tmp<)
tmp=;
}
tmp=;
for(int i=;i<n-;i++)
{
tmp+=c[i];
if(tmp>res2)
res2=tmp;
if(tmp<)
tmp=;
}
printf("%I64d\n",max(res1,res2));
return ;
}
Codeforces_789C_(dp)的更多相关文章
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- px、dp和sp,这些单位有什么区别?
DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...
- android px转换为dip/dp
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...
随机推荐
- python库学习笔记——爬虫常用的BeautifulSoup的介绍
1. 开启Beautiful Soup 之旅 在这里先分享官方文档链接,不过内容是有些多,也不够条理,在此本文章做一下整理方便大家参考. 官方文档 2. 创建 Beautiful Soup 对象 首先 ...
- BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树
BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树 题意:给出每个果汁的价格p,美味度d,最多能放的体积l.定义果汁混合后的美味度为果汁的美味度的最小值. m次询问,要求花费不大于g, ...
- 【JLOI 2011】 不重复的数字
[题目链接] 点击打开链接 [算法] 本题用map很好写,笔者用的是哈希的写法 [代码] #include<bits/stdc++.h> using namespace std; #def ...
- 如何编写linux下nand flash驱动-4
2. 软件方面 如果想要在Linux下编写Nand Flash驱动,那么就先要搞清楚Linux下,关于此部分的整个框架.弄明白,系统是如何管理你的nand flash的,以及,系统都帮你做 ...
- Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找
ylbtech-Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找 1.返回顶部 1. Java 实例 - 数组排序及元素查找 Java 实例 以下实例演示 ...
- springMVC访问根路径问题
当web.xml没有配置欢迎页:如下 <welcome-file-list> <welcome-file>login.jsp</welcome-file> < ...
- Ubuntu 环境变量及 ADB 配置 (转载)
转自:http://blog.csdn.net/ithomer/article/details/7307449 同Windows一样,Ubuntu Linux系统包含两类环境变量:系统环境变量和用户环 ...
- linux文件名乱码时删除或改名的方式(转载)
转自:http://www.linuxsa.cn/when-linux-file-name-topsy-turvy-deleted-or-renamed.html linux文件名乱码时删除或改名的方 ...
- 洛谷 P3357 最长k可重线段集问题【最大流】
pre:http://www.cnblogs.com/lokiii/p/8435499.html 和最长k可重区间集问题差不多,也就是价值的计算方法不一样,但是注意这里可能会有x0==x1的情况也就是 ...
- ionic2 angular2 模板指令补充
向div中插入带有html标签的数据 [innerHTML]="item.content" 字符串截取指令 {{item.de ...