<JZOJ1329>旅行
贪心大水题
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #define rint register int using std::max; using std::min; template <class T>inline void read(T &X) { X=0;int W=0;char ch=0; while(!isdigit(ch))W|=ch=='-',ch=getchar(); while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); X=W?-X:X;return; } int n; long long a[100010]; long long ans=0; int main() { read(n); for(rint i=1;i<=n;++i) read(a[i]); for(rint i=2;i<=n;++i) { int lst=a[i]; if(a[i]>a[i+1] && a[i]>a[i-1]) a[i]=max(a[i-1],a[i+1]),ans+=lst-a[i]; if(a[i]<a[i+1] && a[i]<a[i-1]) a[i]=min(a[i-1],a[i+1]),ans+=a[i]-lst; } for(rint i=2;i<=n;++i) ans+=abs(a[i]-a[i-1]); printf("%lld\n",ans); return 0; }