10403:d. Mountain Road Time Limit:2 Sec Memory limit:128 MB submit:69 solved:23 [Submit][status][web Board] Description
Children in a mountainous area have to go to school through a rugged dirt road, and when it rains, the children are very difficult. Now the village came out of the Dr Kong decided to raise funds to re-build the road. Due to the limited funds, in order to reduce costs, the repaired road surface height can only be monotonically rising or monotonous decline.
to facilitate the road, we divided the entire dirt road into N section, the height of each section of the road respectively A1 , A2,...., an . as each section of the road to be elevated or dug down a unit cost the same costs, the total cost of road and road is proportional to the height.
now Dr Kong hope to find an exact N no ascending or descending sequence of elements B1 , B2,...., Bn , as the height of the repaired Road section. Requirements:
| A1 - B1 | + | A2 – B2 | + ... + | an - Bn |------> Minimum
Input
First line: K indicates how many sets of test data are available.
Next to each set of test data:
1 line :         n n segment
Section 2~n+1 Line: a 1 a 2 ... A N represents the height of each section of the road
2≤k≤Ten 0≤Ai≤107 0≤N≤5xx (I=1,..., N)
all data is is an integer. There is a space between the data.
Data Assurance|A1-B1|+|A2-B2|+ ... +| an-Bn|the minimum value will not exceed109
Output
For each set of test data, the output takes one line: | a1-b1|+| a2-b2|+ ... +| The minimum value of the an-bn|.
Sample Input
271 3 2 4 5 3 958 6 5 6 2
Sample Output
31
HINT Source
Seventh session of Henan Provincial race
The first is to change a series of sequences into a continuous or continuous minimum cost;
DP thought; Dp[i][j] represents the minimum cost of the first element for the first J value;
The state transition equation can be listed: Dp[i][j]=abs (m[i]-n[j]) +mn;mn is converted to the minimum cost between 1~j;
Since it is monotonically increasing or monotonically decreasing, it is only possible to keep the n array in ascending order, and the n array is the discretized array; monocytogenes or single minus;
Code:
#include <iostream> #include <cstring> #include <cstdio># Include<cmath> #include <algorithm>using namespace std; #define MEM (x, y) memset (x,y,sizeof (×)) #define SI (x ) scanf ("%d", &x) #define SL (x) scanf ("%lld", &x) #define PI (x) printf ("%d", x) #define PL (x) printf ("%lld", x) # Define P_ printf ("") const int Inf=0x3f3f3f3f;const double Pi=acos ( -1.0); typedef long Long ll;const int Maxn=510;int M[max N],n[maxn];int dp[maxn][maxn];int n;int cmp (int a,int b) {return a>b;} int solve () {int mn;for (int j=1;j<=n;j++) dp[1][j]=abs (M[1]-n[j]); for (int i=2;i<=n;i++) {mn=inf;for (int j=1;j <=n;j++) {mn=min (mn,dp[i-1][j]);DP [I][j]=abs (M[i]-n[j]) +mn;}} int ans=inf;for (int i=1;i<=n;i++) {ans=min (ans,dp[n][i]);} return ans;} int main () {int t;si (T), while (t--) {si (N); for (int i=1;i<=n;i++) si (M[i]), N[i]=m[i];int ans1,ans2;sort (n+1,n+n+1); Ans1=solve (); sort (n+1,n+n+1,cmp), Ans2=solve ();p rintf ("%d\n", Min (ans1,ans2));} return 0;}
There is a great God with the left side of the tree, dividing the tree to write ....
Links: http://blog.163.com/hacker_james/blog/static/659024432011711105241183/
People's Thinking:
2. Left-biased tree (leftist) O (NLOGN) left-biased tree as a kind of can and heap here can play a role we will each come to a point, put it alone to build a left-leaning tree, and then the left side of the previous interval in the tree of the median of the comparison, if less than the previous interval of the left side tree merge Know that the median recorded in the previous interval is less than or equal to the median of the current interval. How to record median in left-biased tree? Very simply, each left-leaning tree holds only (n+1)/2 elements, the maximum value that is stored at the top of the tree is the median note that using a left-leaning tree to find the median is wrong in some cases. In accordance with the practice of the HYH, assume that now for two sequences 4 5 6 7 8 9 and 1 2 3, the combined number of digits is 5. However, according to the left-biased tree only (n+1)/2 elements of the method, the former only reserved 4 5 6, the latter reserved 1 2, the two merged to retain 5 elements, the resulting median is 6. But this problem does not affect, as to why? Did not want to understand ..... 3. Tree O (NLOGN) Since the demand for the median, and the series is a static sequence, you can think of the partition tree. Apart from the larger left-leaning tree, the efficiency and accuracy of execution are better than that of the left-leaning tree. The constants of the left-leaning tree are relatively large.
The seventh session of Henan Province race 10403:d. Mountain Roads (DP)