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.
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).
Print the maximum possible total sociability of all groups.
5
1 2 3 1 2
3
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形就好了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
///1085422276 #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=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar(); }return x*f; } //**************************************** const int N=1000000+50; #define mod 10000007 #define inf 1000000001 #define maxn 10000 int a[N]; ll dp[N]; int main() { int n=read();a[1]=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); }dp[1]=0; for(int i=2;i<=n;i++) { if(a[i]>=a[i-1]&&a[i-1]>=a[i-2]) dp[i]=dp[i-1]+a[i]-a[i-1]; else if(a[i]<=a[i-1]&&a[i-1]<=a[i-2]) dp[i]=dp[i-1]+a[i-1]-a[i]; else if(a[i]>=a[i-1]&&a[i-1]<=a[i-2]) dp[i]=max(dp[i-2]+a[i]-a[i-1],dp[i-1]); else if(a[i]<=a[i-1]&&a[i-1]>=a[i-2]) dp[i]=max(dp[i-2]+a[i-1]-a[i],dp[i-1]); } cout<<dp[n]<<endl; return 0; }