D. Kindergarten
 
 

In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero).

The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.

Input

The first line contains integer n — the number of children in the line (1 ≤ n ≤ 106).

The second line contains n integers ai — the charisma of the i-th child ( - 109 ≤ ai ≤ 109).

Output

Print the maximum possible total sociability of all groups.

Sample test(s)
input
5
1 2 3 1 2
output
3
 
Note

In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.

In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.

题意:给你n个连续的数,让你划分成连续的区间,每个区间的价值为此区间内最大最小值之差,问你这n个数形成的最大价值是多少

题解:贪心的一个思想就是单调必须在同一区间,知道这个就好做了

考虑v形倒v形就好了

///

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define mod 10000007
#define inf 1000000001
#define maxn 10000 int a[N];
ll dp[N]; int main() {
int n=read();a[]=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}dp[]=;
for(int i=;i<=n;i++) {
if(a[i]>=a[i-]&&a[i-]>=a[i-]) dp[i]=dp[i-]+a[i]-a[i-];
else if(a[i]<=a[i-]&&a[i-]<=a[i-]) dp[i]=dp[i-]+a[i-]-a[i];
else if(a[i]>=a[i-]&&a[i-]<=a[i-]) dp[i]=max(dp[i-]+a[i]-a[i-],dp[i-]);
else if(a[i]<=a[i-]&&a[i-]>=a[i-]) dp[i]=max(dp[i-]+a[i-]-a[i],dp[i-]);
}
cout<<dp[n]<<endl; return ;
}

代码

最新文章

  1. MD5加密、时间戳转换、base64算法加密、解密
  2. 【架构】Google的大规模集群管理工具Borg
  3. sql函数PadLeft与PadRight代码实例
  4. python基础05 缩进与选择
  5. U3D包大小优化之microlib
  6. ps调整图层
  7. JavaScript之四种继承方式讲解
  8. Protobuf一键生成代码bat文件
  9. Oracle 追踪回话SQL几种方法
  10. ONIX 实例
  11. 2440test 裸机測试 调试不进main 设置改动方法
  12. 排序(5)---------高速排序(C语言实现)
  13. 使用python操作RabbitMQ,Redis,Memcache,SQLAlchemy 其二
  14. POJ 2208 已知边四面体六个长度,计算体积
  15. String类型
  16. Visual Studio 项目依赖
  17. Python文件读写(open(),close(),with open() as f...)
  18. 我的java学习之旅--一些基础
  19. Atlas &amp; mysql-proxy
  20. cuda和显卡驱动版本

热门文章

  1. Android Thermal-engine
  2. CUDA-GPU编程
  3. css 众妙之门 学习笔记
  4. 剔除重复jar包,查找重复类
  5. JavaScript学习书签
  6. Ubuntu下获取内核源码
  7. (转) SolrCloud之分布式索引及与Zookeeper的集成
  8. Html test
  9. centOS安装python3 以及解决 导入ssl包出错的问题
  10. UpLoadify在IE下兼容问题