题目链接:

D. Kindergarten

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
input
5
1 2 3 1 2
output
3
input
3
3 3 3
output
0

题意:

把n个数分成一段一段的,每段的值为这段的最大值与最小值之差,现在要分段使值得和最大,最大是多少;

思路:

不会做,看别人说要dp,然后瞎搞了一个dp,然后就过了;
哈哈,真是搞不懂自己怎么写的,反正就是一个上升段的在一起,一个下降段在一起,对于转折点就判断一下把它分到哪个段才能使值得和最大,然后就哈哈哈了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e6+20;
const int maxn=1e6+220;
const double eps=1e-12; int n,a[N];
LL dp[N][2]; int main()
{
read(n);
For(i,1,n)read(a[i]);
dp[0][0]=dp[0][1]=0;
dp[1][0]=dp[1][1]=0;
for(int i=2;i<=n;i++)
{
if(a[i]>a[i-1])
{
dp[i][1]=max(dp[i][1],dp[i-1][1]+a[i]-a[i-1]);
dp[i][1]=max(dp[i][1],dp[i-2][0]+a[i]-a[i-1]);
}
else
{
dp[i][0]=max(dp[i][0],dp[i-1][0]+a[i-1]-a[i]);
dp[i][0]=max(dp[i][0],dp[i-2][1]+a[i-1]-a[i]);
}
dp[i][0]=max(dp[i][0],dp[i-1][0]);
dp[i][1]=max(dp[i][1],dp[i-1][1]);
// cout<<i<<" "<<dp[i][0]<<" "<<dp[i][1]<<endl;
}
cout<<max(dp[n][0],dp[n][1])<<endl; return 0;
}

  

codeforces 484D D. Kindergarten(dp)的更多相关文章

  1. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  2. codeforces 484D Kindergarten (dp、贪心)

    题意:给n个数,分成若干个连续组,每组获益为max-min,输出最大获益. 参考:http://blog.csdn.net/keshuai19940722/article/details/408735 ...

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

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

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

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

  5. CodeForces 484D Kindergarten

    贪心观察+DP决策. 首先需要观察到一个结论:分割后的每一段肯定是单调增或者单调减的. 然后可以根据dp来决策如何分割价值最多. dp[i][0]表示放完第i个,最后一段是递减的情况下的最大价值 dp ...

  6. Kindergarten CodeForces - 484D (贪心,好题)

    大意: 给定序列, 求划分为若干段, 使得总贡献最大, 每段的贡献为max-min 可以发现最优解一定是连续一段递增或递减, 然后dp即可. #include <iostream> #in ...

  7. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  8. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. 虚拟机安装CentOS6.3两个问题

    虚拟机下CentOS安装教程:http://blog.csdn.net/21aspnet/article/details/6961518 由于前一阵子重新装win7,win7下的虚拟机CentOS也就 ...

  2. 一些实用但不为人知的Unix命令

    浮现在脑海的很多 Linux命令,其中一些不为人知,另一些则很常见,如下: xargs or parallel: 并行运行一些程序,命令有很多的选项 sed and awk: 广为人知并且非常有用的处 ...

  3. django 中间件

    django处理一个Request的过程是首先通过django 中间件,然后再通过默认的URL方式进行的.所以说我们要做的就是在django 中间件这个地方把所有Request拦截住,用我们自己的方式 ...

  4. jQuery Flipping Gallery 翻转画廊

    在线实例 简单配置 翻转方向 鼠标滚动 自动播放 绑定事件 使用方法 <div class="main"> <div class="page_conta ...

  5. C# 生成中间含有LOGO的二维码

    效果如下: 1.无LOGO的二维码: 2.含有LOGO的二维码: 一.下载QrCode程序集: 使用的程序集有: 下载地址: http://zxingnet.codeplex.com/ 二.QRCod ...

  6. ArcGIS补丁包下载

    http://zhihu.esrichina.com.cn/?/feature/patchdownload

  7. Mybatis学习记录(七)----Mybatis查询缓存

    1. 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 sql ...

  8. FIL Dalian Jobs

    Department Vacancies Total Skill Set Experience Language Hiring Manager Business Finance Finance Ana ...

  9. UITabBar,UINavigationBar的布局和隐藏问题

    ---恢复内容开始--- 1. 前言 UITabBar,UINavigationBar非常的好用,但是毕竟是系统自带的,不受自己完全掌握,对于布局和隐藏会有一些问题,现在就来谈谈我的想法和一些问题. ...

  10. 【读书笔记】iOS网络-HTTP-URL结构

    http://user:password@hostname:port/absolute-path?query. http:  协议 user:password@   认证 hostname:  主机名 ...