Main TopicA column number is given, which requires modifying certain values so that the final number of columns becomes an ordered sequence, non-increasing or non-decreasing, and the minimum amount of modification is obtained.
AnalysisFirst we will find that after the final modification, or the same as the previous number, or the same as the last number, so as to minimize the amount of modification. We first sorted according to the original sequence, determine the size of the element relationship, the corresponding number is p[i]dp[i][j] to consider the first I element, the last element is the sequence of the first J small element of the optimal solution dp[i][j] = MIN (dp[i-1][k]) + ABS (A[i]-a[p[j]), (0 <K<=J) was just beginning to think of the complexity of O (n^3), and later thought that the K value would determine the minimum value at each J cycle.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6 #defineINF 1000000000000007 #defineMAXN 20058 using namespacestd;9 intN;TentypedefLong LongLL; One LL A[MAXN],B[MAXN]; A LL DP[MAXN][MAXN]; - intMain () - { the while(SCANF ("%d", &n)! =EOF) - { - for(intI=1; i<=n; i++) - { +scanf"%i64d",&a[i]); -b[i]=A[i]; + } ASort (b +1, B +1+n); at for(intI=1; i<=n; i++) - for(intj=1; j<=n; J + +) -dp[i][j]=INF; - for(intj=1; j<=n; J + +) - { -dp[1][j]= (a[1]-b[j]); in if(dp[1][j]<0) -dp[1][j]=-dp[1][j]; to } + - for(intI=2; i<=n; i++) the { *LL k=dp[i-1][1]; $ for(intj=1; j<=n; J + +)Panax Notoginseng { -K=min (dp[i-1][j],k); theLL tem= (a[i]-b[j]); + if(tem<0) Atem=-tem; theDp[i][j]=min (dp[i][j],k+tem); + //printf ("==%i64d\n", Dp[i][j]); - } $ } $LL minn=INF; - for(intI=1; i<=n; i++) -minn=min (minn,dp[n][i]); theprintf"%i64d\n", Minn); - }Wuyi return 0; the}
Hdu 3666 Making the Grade