Codeforces Round #276 (Div. 1)D(贪心+dp)

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.

Sample test(s)
input
5
1 2 3 1 2
output
3
input
3
3 3 3
output
0
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-1]>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]

           还有其它几种情况类似~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          using namespace std; const int MAXN = 1000010; typedef long long ll; int a[MAXN]; ll dp[MAXN]; int main() { int n,x,y; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } dp[0]=0; for(int i=1;i<=n;i++){ if(i==1){ dp[1]=0; continue; } if(a[i-1]>=a[i-2]&&a[i-1]>=a[i])dp[i]=max(dp[i-2]+a[i-1]-a[i],dp[i-1]); else if(a[i-1]<=a[i-2]&&a[i-1]<=a[i])dp[i]=max(dp[i-2]+a[i]-a[i-1],dp[i-1]); else if(a[i-2]<=a[i-1]&&a[i-1]<=a[i])dp[i]=dp[i-1]+a[i]-a[i-1]; else if(a[i-2]>=a[i-1]&&a[i-1]>=a[i])dp[i]=dp[i-1]+a[i-1]-a[i]; } cout< 
         
           < 
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值