D. Kindergarten

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/484/D

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

5
1 2 3 1 2

Sample Output

3

HINT

题意

给你n个数,然后让你分成若干组,只有连续的才能分成一组

然后每组的分数是,这组的最大值减去这组的最小值

问你最大能拿多少分

题解:

很显然单调的必然会分在一个组,这儿有一个问题就是拐点怎么办,分在左边还是右边?

那就dp咯

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1000005
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
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;
}
//************************************************************************************** ll dp1[maxn];
ll dp2[maxn];
ll dp3[maxn];
int a[maxn];
int main()
{
int n=read();
for(int i=;i<=n;i++)
dp1[i]=dp2[i]=dp3[i]=-infll;
for(int i=;i<=n;i++)
a[i]=read();
dp1[]=a[];
dp2[]=-a[];
dp3[]=;
for(int i=;i<=n;i++)
{
dp3[i]=max(dp3[i-],max(dp1[i-]-a[i],dp2[i-]+a[i]));
dp1[i]=max(dp1[i-],dp3[i-]+a[i]);
dp2[i]=max(dp2[i-],dp3[i-]-a[i]);
}
cout<<dp3[n]<<endl;
return ;
}

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

  1. Codeforces Round #276 (Div. 1)D.Kindergarten DP贪心

    D. Kindergarten     In a kindergarten, the children are being divided into groups. The teacher put t ...

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

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

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

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

  4. 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 ...

  5. Codeforces Round #276 (Div. 1)

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

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

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

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

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

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

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

  9. Codeforces Round #202 (Div. 1) D. Turtles DP

    D. Turtles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/problem/B ...

随机推荐

  1. linux(centos7)下安装tomcat7

    1.下载tomcat1.7.tar.gz 2.将文件放到/usr/local中 #cp tomcat1.7.tar.gz /usr/local 3.进入到/usr/local中,解压缩tomcat1. ...

  2. Python的pep8(代码规范)

    Python的pep8-代码规范 1.    代码布局设计 1.1    缩进 A.   使用四个空格来进行缩进 B.   换行的时候可以使用反斜杠,最好的方法是使用园括号,在使用反斜杠的时候,在反斜 ...

  3. jboss服务器配置多实例

    jboss配置多实例的重要性 在开发, 测试项目的过程中, 我们经常需要在同一台主机上, 同一个服务器上配置多个运行实例.这样做有一下几点好处: 在项目开发, 调试阶段能最大限度的节省资源 某个实例出 ...

  4. For Microsoft Azure Network VNET to VNET Connection

    将一个 Azure 虚拟网络 (VNet) 连接到另一个 Azure 虚拟网络非常类似于将虚拟网络连接到本地站点位置.这两种连接类型都使用虚拟网络网关通过 IPsec/IKE 提供安全隧道.连接的 V ...

  5. 子元素过滤器nth-child解释

    jQuery中的子元素过滤器nth-child是指:选取每个父元素下的第index个子元素或者奇偶元素(index从1算起) 这里有几点要注意: 1. index 从1开始算 2. 过滤器filter ...

  6. Dagger学习笔记

    @Inject 提供依赖的构造函数,或者需要依赖的成员变量 @Module 提供依赖,实例化的地方( 使用module实例化,方便测试的时候替换成其他对象,而这也是和构造方法注入的区别,如果用构造方法 ...

  7. POJ 2318 TOYS (计算几何,叉积判断)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8661   Accepted: 4114 Description ...

  8. hdu 5655 CA Loves Stick

    CA Loves Stick Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  9. MSSQL手札四 MSSQL的函数

    和oracle一样,sql也可以自己定义函数 一个返回值,引用DEMO如下: 编写一个函数,该函数,可以通过输入借书时间来判断是否到期,当借阅时间大于30天,返回已经过期:否则返回还未到期. CREA ...

  10. Client Dependency学习

    Client Dependency Framework ---CDF CDF is a framework for managing CSS & JavaScript dependencies ...