Codeforces Round #276 (Div. 1)D(贪心+dp)_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:54:35
原创
1150人浏览过

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.

蓝心千询
蓝心千询

蓝心千询是vivo推出的一个多功能AI智能助手

蓝心千询 34
查看详情 蓝心千询

立即学习前端免费学习笔记(深入)”;

Sample test(s)

input

51 2 3 1 2
登录后复制

output

input

33 3 3
登录后复制

output

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个数,要求你把它分成若干个连续的区间,每个区间的权值定义为该区间最大值与最小值的差值,最后求所有区间的最大权值和


思路:写这个题解前先吐槽一下自己,自从区域赛回来以后忙各种杂事,好久没刷题了,就拿这道不怎么难的题来说,想了好久

      

           最后还是看别人代码才写出来也是弱成狗,想每天安安静静的做题都不行,生活就是这样啊,何时才成达成自己的目标履行自己的诺言,好了,槽点吐完,下面是题解


           首先注意到,划分的区间一定满足单调性,这是一种贪心思想,然后就是dp了


           dp[i]表示前i个数的最优值


           如果a[i-2]a[i]     dp[i]=max(dp[i-2]+a[i-1]-a[i],dp[i-1])

 

           如果a[i-2]>a[i-1]>a[i]     dp[i]=dp[i-1]+a[i-1]-a[i]


           还有其它几种情况类似~


HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号