Test instructions
N number. A1...an.
For each number, each step can only be added one or minus one.
Ask at least how many steps it takes to make the new sequence a non-descending sequence.
n (1≤ n ≤5000)
Ideas:
* One does not know how to prove the conclusion (to be proved): The last new sequence b1...bn each number of BI, must be the original A1. A number in the sequence of an.
Will A1. An from small to large arranged, get c1...cn.
DP[I][J]: The first sequence before I number of operations, the number of I do not exceed c[j] The minimum number of steps spent.
Dp[i][j]=min (Dp[i-1][j]+abs (A[i]-b[j]), dp[i][j-1])
Then use the scrolling array.
Code:
int Constn=5005;intn;ll a[n],b[n];ll dp[n];intMain () {CIN>>N; Rep (I,1, N) {scanf ("%i64d",&A[i]); B[i]=A[i]; } mem (DP,0); dp[0]=INF; Sort (b+1, B +1+N); Rep (I,1, N) {//First iRep (J,1, N) {//the height of the last height not exceeding b[j]Dp[j]=min (Dp[j]+abs (A[i]-b[j]), dp[j-1] ); }} ll ans=INF; Rep (I,1, N) {ans=min (ans,dp[i]); } printf ("%i64d\n", ans); return 0;}
cf13c Sequence (DP)