$b=x,x+\Delta_1,x+\Delta_1+\Delta_2,...$

$c=a_1-x,a_1-x+\Gamma _1,a_1-x+\Gamma_1+\Gamma_2,...$

$x-(-\Delta_1-\Delta_2-...)$

$x-(a_1-\Gamma_1-\Gamma_2-...)$

#include<bits/stdc++.h>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define op(x) ((x&1)?x+1:x-1)
#define odd(x) (x&1)
#define even(x) (!odd(x))
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define next Cry_For_theMoon
#define il inline
#define pb(x) push_back(x)
#define is(x) insert(x)
#define sit set<int>::iterator
#define mapit map<int,int>::iterator
#define pi pair<int,int>
#define ppi pair<int,pi>
#define pp pair<pi,pi>
#define fr first
#define se second
#define vit vector<int>::iterator
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const int MAXN=4e5+10;
ll n,a[MAXN],db[MAXN],dc[MAXN],b[MAXN],c[MAXN],d[MAXN],ans;
int main(){
cin>>n;
rep(i,1,n){
cin>>a[i];
}
rep(i,1,n-1){
if(a[i]<a[i+1]){
db[i]=a[i+1]-a[i];
}else{
dc[i]=a[i]-a[i+1];
}
}
ll sum=0;
rep(i,1,n){
d[i]=sum;
sum-=db[i];
}
sum=a[1];
rep(i,1,n){
d[i+n]=sum;
sum-=dc[i];
}
sort(d+1,d+1+2*n);
b[1]=d[n];
c[1]=b[1]-a[1];
rep(i,1,n){
b[i+1]=b[i]+db[i];
c[i+1]=c[i]+dc[i];
}
rep(i,1,n){
ans+=abs(b[i]);
ans+=abs(c[i]);
}
cout<<ans<<endl;
return 0;
}



DP 解法有时间再补（之前写了那么多斜率优化都没想到 DP 解法确实需要好好学习）

posted on 2021-07-19 15:15  Cry_For_theMoon  阅读(231)  评论(5编辑  收藏  举报