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. TestNG中同一个类中执行多个test()方法如何配置testng.xml

    public class IndexInfo extends BaseTesting{ private IndexPage IndexPage1;// private AddEquipmentInfo ...

  2. jsoup 获取指定页面的所有链接(需后续完善)

    java代码如下: import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; impor ...

  3. Win10正式版激活方法有哪些?如何激活Win10?

    很多用户都想将系统升级到Win10,但是却不知道怎么激活Win10正式版的方法,其实不同版本激活正式版Win10的方法秘钥不同,下面99安卓网小编就分享一些激活Win10正式版的方法和秘钥,供大家参考 ...

  4. python中的文件

    Python文件 1.    概述 文件对象不仅可以用来访问普通的磁盘文件,也可以访问任何其他类型抽象层面上的文件. 内建函数open()以及file()提供了初始化输入输出(I/O)操作的通用接口. ...

  5. .net/c#连接sqlserver

    Webconfig代码 <configuration> <appSettings> <add key="myconnect" value=" ...

  6. 数据结构(12) -- 图的邻接矩阵的DFS和BFS

    //////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...

  7. iOS开发-你真的会用SDWebImage?(转发)

    原文地址: http://www.jianshu.com/p/dabc0c6d083e SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析 ...

  8. air开发中的requestedDisplayResolution 扫盲

    app.xml里面requestedDisplayResolution 取值可以为high/standard, 如果为high表示设备跟ios声明它需要使用高清屏(其实就是需要最大分辨率) 这里我猜测 ...

  9. emacs 操作集锦

    1.C-k 的功能并不是剪切当前行,而是剪切当前行从光标到行末的内容. Emacs 中的剪切不叫剪切(cut),叫kill,复制(copy)不叫copy ,叫kill-ring-save (这个可以理 ...

  10. delphi AES encrypt

    xe8 ok unit TntLXCryptoUtils; interface function AES128_Encrypt( Value, Password : string ) : string ...