# Codeforces_484D:Kindergarten(贪心/DP)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define ll long long
const int N=1000008;
ll n,a[N];
ll maxx(ll x,ll y){if(x>y)y=x;return y;}
ll minx(ll x,ll y){if(x<y)y=x;return y;}
ll maxx(ll x,ll y,ll z){if(x>z)z=x;if(y>z)z=y;return z;}
ll minx(ll x,ll y,ll z){if(x<z)z=x;if(y<z)z=y;return z;}
int main(void)
{
ll i,p1,p2,p3,p4,p,pre,mi,ma;
scanf("%I64d",&n);
for(i=1;i<=n;i++)scanf("%I64d",a+i);a[0]=a[1];a[n+1]=a[n];
p1=p2=p=0;pre=1;mi=ma=a[1];
for(i=1;i<=n;i++)
if(a[i]<a[i+1])
{
if(p>=0){p=1;mi=minx(mi,a[i]);ma=maxx(ma,a[i]);}
else
{
p3=maxx(p1+(maxx(a[pre],ma)-minx(a[pre],mi))*(pre+1!=i),p2+(ma-mi)*(pre+1!=i));
p4=maxx(p1+maxx(a[pre],ma,a[i])-minx(a[pre],mi,a[i]),p2+maxx(ma,a[i])-minx(mi,a[i]));
p1=p3;p2=p4;pre=i;p=1;mi=ma=a[i+1];
}
}
else if(a[i]>a[i+1])
{
if(p<=0){p=-1;mi=minx(mi,a[i]);ma=maxx(ma,a[i]);}
else
{
p3=maxx(p1+(maxx(a[pre],ma)-minx(a[pre],mi))*(pre+1!=i),p2+(ma-mi)*(pre+1!=i));
p4=maxx(p1+maxx(a[pre],ma,a[i])-minx(a[pre],mi,a[i]),p2+maxx(ma,a[i])-minx(mi,a[i]));
p1=p3;p2=p4;pre=i;p=-1;mi=ma=a[i+1];
}
}
else{mi=minx(mi,a[i]);ma=maxx(ma,a[i]);}
p2=maxx(p1+maxx(a[pre],ma,a[n])-minx(a[pre],mi,a[n]),p2+maxx(ma,a[n])-minx(mi,a[n]));
cout<<p2;
return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
ll num[1000010],ans;
int main()
{
int T,t,n,m,i,j,k;
ll ans=0,a=-1e9,b=-1e9,val;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%I64d",&val);
a=max(a,ans+val);
b=max(b,ans-val);
ans=max(ans,a-val);
ans=max(ans,b+val);
}
printf("%I64d\n",ans);
}

• 本文已收录于以下专栏：

## HDU4009 Transfer water 【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others...

## [Codeforces 484D Kindergarten] DP

[Codeforces 484D Kindergarten] DP知识点：dynamic programming greedy1. 题目链接[Codeforces 484D Kindergarten]...

## Codeforces - 831D. Office Keys - dp、二分+贪心

D. Office Keys 题目链接 分类：dp、二分、贪心 1.题意概述 一个坐标轴上有n个人，他们坐标分别为a[1...n]a[1...n]，而且有k把钥匙坐标分别在b[1...k...

## codeforces-486【C-贪心-思维】【D-树状DP】

举报原因： 您举报文章：Codeforces_484D:Kindergarten(贪心/DP) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)