Recently in the study of dynamic planning, the problem is of course dynamic planning (sorry spoilers) ... Just start to do dynamic planning, want to analyze in detail, in order to understand more deeply!

First read the question, the title meaning is asked to choose from two sets of s1,s2 n number pairs, their distance (the absolute value of the difference) and the smallest, because the S1 collection is less than S2 set, so is to select all n numbers from the S1, from the S2 also elected n numbers, 22 paired after the Min value, the title is this meaning ...

Then, I will find it difficult, because you do not know how to choose is the best, then think of course here to simulate the meaning of the topic, suppose some examples:

S1:1 2

S2:1 2 3

Obviously, here is the first line of order, after all, is to ask the distance as small as possible, that of course must first sort! It is easy to see that the S1 1 2 and S2 1 2 pairing is the best, min=0; is that just the same number in front of you? Of course not, give a counter-example:

S1:20 40

S2:10 30 45

S1 selected 40,S2 10 30 of course not the best, S1 choose 40,S2 10 45 is better than him, right! So, really do not know how to choose ...

--------------------------------------------------------------------------------------------------------------- --------------------

That's when you start to think:

S1:3

S2:1

At this time, n=m, the number is equal, of course, is directly paired! If S2 adds a 2, it becomes:

S1:3

S2:1 2

At this time do not choose 1, choose 2, the equivalent in the original state, increased a number, and then relatively new state of Min, found 3-1>3-2, so choose 3 2, abandoned 1;

At this time again to a fierce, up and down at once plus, into:

S1:3 4

S2:1 2 8

If the original based on the choice of 4 8,min=1+8-4=5, or discard the 8 (can not abandon 4, because S1 is all to use), it becomes the n=m, direct correspondence on the line, then min=4, of course, better than the previous choice!

--------------------------------------------------------------------------------------------------------------- -------------------------------

In fact, there is a bit of dynamic planning here, for a certain state, you know his optimal value, then in the next state (equivalent to the new two) you have to find the optimal solution of the next state through this state of optimization, so recursion down ...

Then continue to analyze: If the n=m, happens, then directly paired, said before;

So the point is, we want to analyze the situation of n<m, no matter how much m is greater than N, M is always larger than N, form such as:

s1:****

s2:*********

Because the optimal solution of the next state is determined by the optimal solution of the previous state, why not start with the initial state of the first?! Then we assume that the optimal solution of the initial state is the ultimate state:

s1:*

s2:**

First define a dp[i][j], which represents the number of the I (J>i) that is paired S1 with the number of S2 of the first J, at this time i=1,j=2; the next state? is to add a number on the top and bottom, or just add a number below (in fact, the situation is also included in the upper and lower, see the state of the state transfer equation to know, and now did not seek out, do not worry), the next state diagram is:

S1:*ai

s2:* (*) BJ

O is the newly added two number, which is the process of approaching the ultimate state, which is i=2,j=3, and that dp[2][3] equals how much? In fact, there are two possibilities: 1.ai is paired with Bi because they are closer to each other,

At this time obviously dp[2][3]=dp[1][2]+abs (AI-BJ); 2. Because the AI and bi gaps are too far apart, it is better to pair with the number in front of BJ, where Dp[2][3]=dp[2][2],dp[2][2] is the first two paired values, as we have said before, At this time i==j, you can directly draw the value! So here it comes! Dp[2][3]=min (Dp[1][2]+abs (AI-BJ), dp[2][2]), the abstract point is:

Dp[i+1][j+1] = min (Dp[i][j]+abs (AI-BJ), Dp[i+1][j])

Or: dp[i][j] = min (Dp[i-1][j-1]+abs (AI-BJ), dp[i][j-1])

In fact, the above analysis of the state transfer can have many kinds of, but it is a truth:

S1:**ai

s2:******* (*) BJ

s1:*

S2:*******bj

These are the same transfer equations!

So, we just need to get the initial value of Dp[i][j] (I==J), and then the recursive solution Dp[i][j] (i<j), the last dp[n][m] is the min we ask!

Here, the initial state has, the state transfer equation has, recursion direction has, finished, attached code!!!

Finally, it is found that the dynamic programming is a bit like the solution of nonlinear equations, first there is a initial guess, and then iterative solution, do not know that this is wrong ...

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cmath>5#include <cstring>6 7 using namespacestd;8 9 inta[505], b[505];Ten intf[505][505]; One A intMain () - { - intt, N, M; thescanf"%d", &t); - while(t--) - { - +scanf"%d%d", &n, &m); -Memset (F,0,sizeof(f)); + for(intI=1; i<=n; i++) Ascanf"%d", &a[i]); at for(intI=1; i<=m; i++) -scanf"%d", &b[i]); - -Sort (A +1, a+n+1); -Sort (b +1, b+m+1); - inf[1][1] = ABS (a[1]-b[1]); - for(intI=2; i<=n; i++) toF[i][i] = f[i-1][i-1] + ABS (a[i]-b[i]); + - for(intI=1; i<=n; i++) the for(intj=i+1; j<=m; J + +) *F[i][j] = min (f[i][j-1], f[i-1][j-1]+abs (a[i]-b[j])); $printf"%d\n", F[n][m]);Panax Notoginseng } - the return 0; +}

Finally recommend a blog to talk about dynamic planning!

Sicily 1828 Minimal (DP)