1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const int maxn=2e4; const int inf=99999999; const float eps=1e-3; const pair<int,int> p0=make_pair(0,0);
int a[maxn],f1[maxn],f2[maxn]; pair<int,int> g1[maxn],g2[maxn];
void init(); void solve(); void outp();
int main() { int T,n,m; scanf("%d",&T); while(T--) { scanf("%d",&n); CLEAR(a); CLEAR(f1); CLEAR(f2); CLEAR(g1); CLEAR(g2); scanf("%d",&a[1]); f1[1]=f2[1]=0; g1[1]=g2[1]=p0; for(int i=2;i<=n;i++) { scanf("%d",&a[i]); if (f1[i-1]<f2[i-1]) { g1[i]=make_pair(a[i],g1[i-1].second); g2[i]=make_pair(g1[i-1].first,a[i]); f1[i]=f1[i-1]+abs(a[i]-g1[i-1].first); f2[i]=f2[i-1]+abs(a[i]-g1[i-1].second); } else { g1[i]=make_pair(a[i],g2[i-1].second); g2[i]=make_pair(g2[i-1].first,a[i]); f1[i]=f1[i-1]+abs(a[i]-g2[i-1].first); f2[i]=f2[i-1]+abs(a[i]-g2[i-1].second); }
} printf("%d\n",min(f1[n],f2[n])); } }
|