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. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0.zfill()方法语法:s ...

  2. C/C++之内存分配

    一.编译时与运行时的内存情况1.编译时不分配内存编译时是不分配内存的.此时只是根据声明时的类型进行占位,到以后程序执行时分配内存才会正确.所以声明是给编译器看的,聪明的编译器能根据声明帮你识别错误.2 ...

  3. CentOS7防火墙之firewalld

    今天在centos7上装mysql8,装好了之后发现主机的navicat始终连不上centos中的mysql 搜索发现是防火墙的问题,已查看iptables,嗯?没有了这个防火墙,原来centos换防 ...

  4. 20145227鄢曼君《网络对抗》Web安全基础实践

    20145227鄢曼君<网络对抗>Web安全基础实践 实验后回答问题 1.SQL注入攻击原理,如何防御? SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是 ...

  5. 20145317《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145317<网络对抗>shellcode注入&Return-to-libc攻击深入 学习任务 shellcode注入:shellcode实际是一段代码,但却作为数据发送给受攻 ...

  6. 20145334赵文豪网络对抗Web安全基础实践

    1.SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意SQL命令的目的. 对于SQL注入攻击的防范,我觉 ...

  7. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. Python之GUI的最终选择(Tkinter)

    首先,Tkinter是Python默认的GUI库,想IDLE就是用Tkinter设计出来的,因此直接导入Tkinter模块就可以啦 1 import tkinter (1)Tkinter初体验: 1 ...

  9. JavaScript:Delete属性

    以前,我就晓得delete只能够删除隐性属性(就是没有进行声明的变量),但是不知道为什么这样? 隐性属性:在页面中以前没有声明过该变量,直接进行赋值的 str='hongda' 其实这是由属性的特性决 ...

  10. C#中的DllImport使用方法

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息 DllImport属性应用于方法,要求最少要 ...