CodeForces - 484D Kindergarten
来源:互联网 发布:手机淘宝能收藏店铺吗 编辑:程序博客网 时间:2024/06/05 21:06
Description
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 Input
51 2 3 1 2
3
33 3 3
0
Hint
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.
#include <iostream>#include <cstdio>#include<algorithm>using namespace std;#define LL long long#define mxn 1000010LL a[mxn];LL dp[mxn][2];int main(){int n;while (cin >> n){for (int i = 0; i<n; i++)scanf("%I64d", &a[i]);dp[0][0] = dp[0][1] = 0;for (int i = 1; i<n; i++){if (a[i]>a[i - 1]){dp[i][1] = max(dp[i - 1][0], dp[i - 1][1] + a[i] - a[i - 1]);dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);}else {dp[i][0] = max(dp[i - 1][1], dp[i - 1][0] + a[i - 1] - a[i]);dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]);}}printf("%I64d\n", max(dp[n - 1][0], dp[n - 1][1]));}return 0;}
- codeforces 484D kindergarten
- CodeForces 484D Kindergarten
- CodeForces - 484D Kindergarten
- Codeforces 484D-kindergarten
- CodeForces 484D Kindergarten
- Kindergarten - CodeForces 484 D dp
- Codeforces 484D Kindergarten(dp)
- 【codeforces 484D】【DP】Kindergarten
- [Codeforces 484D Kindergarten] DP
- Codeforces 484D. Kindergarten DP贪心
- codeforces 484D Kindergarten 【动态规划】
- CodeForces 484 D.Kindergarten(dp)
- [Codeforces 484D] Kindergarten (DP + 树状数组优化)
- 484D - Kindergarten DP
- Codeforces Round #276 (Div. 1) D. Kindergarten
- Codeforces Round #276 (Div. 1) D. Kindergarten dp
- 1月2日 Codeforces Round #276 (Div. 1) D. Kindergarten
- Kindergarten
- HDU - 1053 Entropy
- HDU - 2037 今年暑假不AC
- 数据结构和算法经典100题-第2题
- POJ - 1328 Radar Installation
- POJ - 3253 Fence Repair
- CodeForces - 484D Kindergarten
- HDU - 2028 Lowest Common Multiple Plus
- HDU - 1576 A/B
- HDU - 1397 Goldbach's Conjecture
- POJ - 3070 Fibonacci
- FZU - 2020 组合
- Linux中进程的几种状态
- CFLAGS=-static 静态编译 少林功夫真嘿侯
- HDU - 4497 GCD and LCM