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 land 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

In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.

类似于最大循环字段和,不难发现最大子段和出现的情况分为两种:

1  +-+-+-...

2  -+-+-+...(此时一定不是从第一个数开始的所以将第一个数也置负即可)

所以只要更改+-求两次最大子段取最大值就ok

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define inf 0x7fffffff
LL a[100005];
LL LLS(int n)
{
LL sumn=0,maxn=-inf,i;
//cout<<"ssss:"<<maxn<<endl;
for(i=1;i<=n;++i){
sumn+=a[i];
maxn=max(maxn,sumn);
if(sumn<0) sumn=0;
}
return maxn;
}
int main()
{
LL n,i,j;
while(cin>>n){
for(i=1;i<=n;++i){
scanf("%lld",&a[i]);
if(i==1) continue;
a[i-1]=abs(a[i-1]-a[i]);
}
//for(i=1;i<n;++i) cout<<a[i]<<" ";
for(i=2;i<=n;i+=2) a[i]=-a[i];
j=LLS(n-1); for(i=1;i<=n-1;++i) a[i]=-a[i];
cout<<max(j,LLS(n-1))<<endl;
}
return 0;
}

cf188C(最大子段和&&思维)的更多相关文章

  1. 旋转子段 (思维stl)

    题目: 大概意思就是给你一个序列,你可以选择一段区间使它左右翻折一遍,然后呢,从1到n找一遍,看a[i]==i的数最多是多少. 其实刚才我已经把暴力思路说出来了,枚举每一个区间长度,枚举每一个左端点, ...

  2. 51nod-1065 最小正子段和 【贪心 + 思维】

    N个整数组成的序列a[1],a[2],a[3],-,a[n],从中选出一个子序列(a[i],a[i+1],-a[j]),使这个子序列的和>0,并且这个和是所有和>0的子序列中最小的. 例如 ...

  3. 假·最大子段和 (sdutoj 4359 首尾相连)(思维)

    题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2736/pid/4359 具体思 ...

  4. 51nod 最大M子段和系列(1052、1053、1115)

    51nod1052 数据量小,可使用O(N*M)的DPAC,递推公式: dp[i][j]=max(dp[i-1][j-1], dp[i][j-1])+a[j]; dp[i][j]表示前j个数取 i 段 ...

  5. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  6. Photoshop、Illustrator思维导图笔记

    半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.

  7. CYQ.Data 从入门到放弃ORM系列:开篇:自动化框架编程思维

    前言: 随着CYQ.Data 开始回归免费使用之后,发现用户的情绪越来越激动,为了保持这持续的激动性,让我有了开源的念头. 同时,由于框架经过这5-6年来的不断演进,以前发的早期教程已经太落后了,包括 ...

  8. 最大子段和(c++)

    // 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...

  9. 计算机程序的思维逻辑 (8) - char的真正含义

    看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...

随机推荐

  1. Maximum execution time of 30 seconds exceeded解决错误方法

    Maximum execution time of 30 seconds exceeded解决错误方法Fatal error: Maximum execution time of 30 seconds ...

  2. cojs 二分图计数问题1-3 题解报告

    OwO 良心的FFT练手题,包含了所有的多项式基本运算呢 其中一部分解法参考了myy的uoj的blog 二分图计数 1: 实际是求所有图的二分图染色方案和 我们不妨枚举这个图中有多少个黑点 在n个点中 ...

  3. MySQL-MHA集群部署(binlog复制)

    MHA的理论知识网上有很多教程,这里不会说明:仅推荐博客链接! MHA的理论说明:http://www.ywnds.com/?p=8094 MHA的安装包需要在google上面下载,或者就是csdn上 ...

  4. Jquery 给Js动态新添加的元素 绑定的点击事件

    //one $('.class').on("click",function(){ alert('one') }); //相当于$('.class').bind("clic ...

  5. troubleshooting-执行导数shell脚本抛异常error=2, No such file or directory

    Cannot run program "order_log.sh" (in directory "/data/yarn/nm/usercache/chenweidong/ ...

  6. 20145122 《Java程序设计》课程总结

    课程总结 每周读书笔记链接汇总 第一周读书笔记:http://www.cnblogs.com/20145122chengzhiyin/p/5244949.html 第二周读书笔记:http://www ...

  7. sqlserver数据库出错的解决方法

    1.SQLServer2008数据库sa账户登录127.0.0.1失败 http://blog.sina.com.cn/s/blog_546a30270102weh2.html 2.SqlServer ...

  8. NOIP2018 退役记

    NOIP挂完,OI再见 AFO Day 0 早上的高铁,1点多到广州,2点多到酒店,下午就是颓颓颓,然后晚上随便刷了一下板子,反正PJ也没啥板子可以刷 就这样浪费了一天,我到底在干嘛 Day 1 早上 ...

  9. [TODO]The way to Go(9): 基本类型和运算符

    参考: Github: Go Github: The way to Go 基本类型和运算符 表达式是一种特定的类型的值,它可以由其它的值以及运算符组合而成. 每个类型都定义了可以和自己结合的运算符集合 ...

  10. 处理div 在IE6 IE7 IE8 不居中的问题

    具体处理方式如下:1 .html 顶部加入:DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...