title link:Sicily 1419
problem-Solving ideas:(a slightly different dynamic programming topic)
At first saw the topic immediately think of a solution to the motion, with Dp[i][j] that the first I arrived at the J Point, but this approach has a problem-the deduction of the next point of time need to use the last point of the data (because the slower the delivery of milk will take more time), so that the time complexity will reach O (n^3 ), must be timed out, so we can see, to solve this problem, to resolve two problems:
1) First to search all the data possibilities; 2) The final total time can be obtained
These two questions, thought for a long time, found themselves silly ...
1) in order to minimize the total time, then as long as through that point will inevitably put down the milk, so (assuming that the current sent to the i+1 home) I must be from the first layer from the i+1 to the nearest one or the other end of a certain, so there is no n case Ah, only the L-layer below, there is a recent one;
2) The final total time, since the current time will have an impact on the subsequent time, so (assuming that the distance from the current point to the next point is D, the remaining X points) the last total time will increase d*x; (well, that's enough)
Final Solution:
First, all the floors (including L) are ordered and then used DP[I][J] to represent the shortest time between interval I ~ j (point I to Point J), but not enough, because the end point is different, the effect on the subsequent movement is different, so we need two DP array to record (Dp[0] and dp[1], 0 represents the end point under L, 1 is the opposite), and then the state transition equation is:
Dp[0][i][j]=min (dp[0][i+1][j]+ (A[i+1]-a[i])(n-j+i), dp[1][i+1][j]+ (A[j]-a[i])(n-j+i));
Dp[1][i][j]=min (dp[1][i][j-1]+ (a[j]-a[j-1])(n-j+i), dp[0][i][j-1]+ (A[j]-a[i])(n-j+i));
of which, I < index, J > index (indexes for L)
The code is as follows:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3fusing namespace Std;intn,l,a[1005],dp[2][1005][1005];intMain () {intTIndex; scanf"%d", &t); while(t--) {scanf ("%d %d", &n,&l); for(intI=0; i<n;i++) scanf ("%d", &a[i]); A[n++]=l,Index=0;Sort(A,a+n); while(Index<n) {if(a[Index]==L) Break;Index++; } memset (Dp,inf,sizeof (DP)); dp[0][Index][Index]=dp[1][Index][Index]=0; for(intI=Index; i>=0; i--) { for(intj=Index; j<n;j++) {if(i<Index) dp[0][i][j]=min (dp[0][i+1][j]+ (a[i+1]-a[i])*(N-j+i), dp[1][i+1][j]+ (A[j]-a[i])*(N-j+i));if(j>Index) dp[1][i][j]=min (dp[1][i][j-1]+ (a[j]-a[j-1])*(N-j+i), dp[0][i][j-1]+ (A[j]-a[i])*(N-j+i)); } }printf("%d\ n", Min (dp[0][0][n-1],dp[1][0][n-1])); }return 0;}
Summary:
1, a bit difficult problem, heavy in the state of consideration;
2, dynamic regulation is not enough familiar, harm more practice.
Sicily 1419 (Dynamic planning)