题目链接:

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. [WF] Quickstart Sample

    [WF] Quickstart Sample 前言 Workflow Foundation(WF),总是给人一种很有用.可是却不知道怎么用的印象.这主要是因为前置的功课太多.要整合很多底层知识,才能完 ...

  2. 美丽的阴影(剖析jquery主页)

    jquery Download API Documentation Blog Plugins Browser Support jquery主页第一眼看到的就是这个玻璃渣效果,设计者利用高光和阴影制造出 ...

  3. android notification 传值关键

    android notification 传值关键在 onNewIntent方法里获取 @Override protected void onCreate(Bundle savedInstanceSt ...

  4. MacBook Air 使用技巧

    1.Finder             相当于资源管理器 Launchpad       相当于桌面 Safari              浏览器 2.快捷键 * cmd+shift+3: 捕获整 ...

  5. iOS 杂笔-20(UIView和CALayer的区别与联系)

    iOS 杂笔-20(UIView和CALayer的区别与联系) 每个 UIView 内部都有一个 CALayer 在背后提供内容的绘制和显示,并且 UIView 的尺寸样式都由内部的 Layer 所提 ...

  6. iOS 自定义进度条

    自定义条形进度条(iOS) ViewController.m文件 #import "ViewController.h" @interface ViewController () @ ...

  7. iOS网络检测Reachability 使用 Demo,可检测2、3、4G

    你可以在Github下载这个Demo https://github.com/JanzTam/Reachability_Demo 首先,引入系统的Reachability类,不知道怎么引入的话,在Xco ...

  8. new与malloc的区别

    看起来,它们的不同只是new比malloc用起来更方便而已.仅仅是这样吗?不是吗? 1 来源不同: 1)new/delete是C++中的操作符,而malloc/free是C中的标准库函数,需要库文件支 ...

  9. Hive安装与配置(靠谱亲测)

     Hive是hadoop生态环境的组成之一.通过Hive,可以使得直接用SQL操作HDFS.最大的好处就是让熟悉SQL,但是不了解JAVA的数据分析师使用.其机制就是一个将SQL语言转化为MapRed ...

  10. 【AdaBoost算法】积分图代码实现

    一.积分图介绍 定义:图像左上方的像素点值的和: 在Adaboost算法中可用于加速计算Haar或MB-LBP特征值,如下图: 二.代码实现 #include <opencv/highgui.h ...