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 ;
}

代码

Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心的更多相关文章

  1. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  2. Codeforces Round #276 (Div. 1) A. Bits 二进制 贪心

    A. Bits Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/484/problem/A Des ...

  3. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  4. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  5. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  6. Codeforces Round #276 (Div. 1)

    a. 给俩数, 求他俩之间二进制数中1最多的,有多个输出最小的: 贪心,从小到大加能加就加,最后可能碰到一个不能加了但是当前数比l小,那么就加上这个数,然后从大到小,能减就减,见到符合条件 #incl ...

  7. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  8. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  9. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

随机推荐

  1. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  2. 项目关联到svn

    最近因为要升级项目,改用64位的eclipse,原先已经被svn管理的项目需要被复制到另一个工作空间,只需要导入并复制项目到新的工作空间即可 右键,点import 选择已经存在的项目导入工作空间 选择 ...

  3. input按钮的事件处理大全

    input按钮的事件处理大全   input按钮的事件处理大全1.<INPUT onclick=document.all.WebBrowser.ExecWB(1,1) type=button v ...

  4. acedinitget

    // 提示用户选择选择方式 acedInitGet(0, _T("W CP")); int nRs = acedGetKword(_T("\n请输入关键字确定选择方式[窗 ...

  5. wpf绑定静态变量,模拟rem单位

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  6. vue03 axios

    4. 通过axios实现数据请求 vue.js默认没有提供ajax功能的. 所以使用vue的时候,一般都会使用axios的插件来实现ajax与后端服务器的数据交互. 注意,axios本质上就是java ...

  7. Python总结2

    时间:25日上午'''列表定义:在[]内,可以存放多个任意类型的值,并以逗号隔开''''students=['sb','2b']print(students[1])student_info=['min ...

  8. JavaScript day3(数据类型)

    数据类型(data type) JavaScript提供七种不同的数据类型(data types),它们是string(字符串), symbol(符号), number(数字), undefined( ...

  9. 找零钱的算法实现(Java)

    简单的算法 基本思路就是将面值从大到小统计(外循环), 若当前金额大于某面值, 则当前金额减掉该面值, 并将面值对应张数+1, 继续往下判断(内循环) public void Change(int m ...

  10. MySql 内存表使用

    MySql 内存表使用 内存表使用哈希散列索引把数据保存在内存中,因此具有极快的速度,适合缓存中小型数据库,但是使用上受到一些限制,以下是蓝草使用的一些感受. 1.heap对所有用户的连接是可见的,这 ...