The modified sequence becomes a non-descending sequence, making the objective function minimal. (There is a problem with this data, only non-diminishing
Consider from left to right, the current a[i]≥ of the previous number, when the value of the previous number is fixed, we hope the cost of the previous operation as small as possible.
So the state can be defined as DP[I][J], which indicates the minimum cost of the number of first I in the number of J.
But the problem is that J has a very large range, which can actually limit the range of J to the values in the sequence, the idea is discretized,
Assuming b[k] denotes a[], then the value range (B[k-1],b[k]) is the same as the effect on subsequent decisions. (assuming B[-1] is-inf, there is also a value range of [Max (A[i]), INF), so the value is definitely not optimal
such as A[i] take the minimum, and a[i] take the minimum-1 of the impact of the subsequent decision is the same, and spend less.
In summary, the J definition can be modified to take the value of the sequence J large.
Transfer to Dp[i][j] = min (dp[i-1][k]) + cost (i,j), K≤j. Cost is determined by the target function, this is the difference between ABS
The transfer of Min can be transferred by O (1), plus the scrolling array optimizes the space to O (n).
Time Complexity of O (n^2)
The left-leaning tree is related to the nature of the objective function, and the complexity of the time is lower, but more difficult to write. Partitioning a tree is also possible. ( neither
#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>//#include <bits/stdc++.h>using namespacestd;Const intMAXN =2001;intA[MAXN],B[MAXN];intDP[MAXN];//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intN scanf"%d",&N); for(inti =0; I < n; i++) scanf ("%d", A +i); memcpy (B,a,sizeof(int)*N); Sort (b,b+N); //DP Zero for(inti =0; I < n; i++){ intMN =1<< -; for(intj =0; J < N; J + +) {mn=min (mn,dp[j]); DP[J]= Mn+abs (a[i]-B[j]); }} printf ("%d\n", *min_element (dp,dp+N)); return 0;}
POJ 3666 Making the Grade