problem Description
There is a single-plank bridge on the river, and a frog wants to jump from one side of the river to the other along the plank. There were some stones on the bridge, and the frogs hated stepping on the stones. Since the length of the bridge and the distance that the frog skips all are positive integers, we can think of the point where the frog may arrive on the single plank as a series of the whole hour on the axis: 0,1,......,l (where L is the length of the bridge). A point with a coordinate of 0 represents the starting point of the bridge, and the point with the L coordinates represents the end point of the bridge. The frog jumps from the beginning of the bridge to the final direction. The distance of a single hop is any positive integer (including s,t) between S and T. When a frog jumps or jumps over a point with a coordinate of l, even if the frog has jumped out of the plank.
The topic gives the length of the bridge, the distance of the frog jumps s,t, and the position of the stone on it. Your task is to determine the minimum number of stones a frog will need to step across the river.
For 30% of data, L <= 10000;
For all data, L <= 10^9.
Inputthe first line of the input has a positive integer l (1 <= L <= 10^9), which represents the length of the single-plank bridge. The second line has three positive integer s,t,m, which indicates the frog's minimum distance, maximum distance, and number of stones on the bridge, of which 1 <= S <= T <= 10,1 <= M <= 100. The third line has m different positive integers representing the position of the M-Stone on the axis (the data ensures that there are no stones at the beginning and end of the bridge). All adjacent integers are separated by a space. OutputThe output includes only an integer that indicates the minimum number of stones a frog needs to step across the river. Sample Input
102 3 52 3 5 6 7
Sample Output
2
Originally this problem can be said to be a simple DP problem, the state transfer equation is easy to find:f [i] = min (f [i-j]) + stone [i] S <= J <=t && I >= J
However, the difficulty of this problem is the length of the plank Bridge L Up to the highest attainable 10^9. If you use the usual method for Loop from 1 recursion to L + T , it will inevitably time out.
At this point we need to compress the path with discrete thought.
We will find:
① f [i]only with F [(i-j) min]~ F [(I-J) max] ( S <= J <= T && i >= J) about.
② The number m of the stone is much smaller than L. In other words, there is a large gap between the two stones , and this time f [i] in this area the recursive values are constant.
due to the above ① and ② We know in the recursion F [Step] , we need at mostF [Step-t]~ F [step-s] the value between the two. At this time we just have to launch a step1 position for the first time, making:
F [ step1-t]~ F [step1-s] And F [step-t]~f [step-s] each value corresponds to equality. (If S≠t, F [Step1-s] Each value is also equal to )
The rest of the Step1 -to-step does not have to be pushed. Here we reach the optimization of the left and right, the path length from step optimization to step1.
Now we only need to find the largest then you can put L Compressed to Step1*m.
If the p*x+ (p+1) *y=q(with jump distance p and p+1 can jump to any position Q), then there must be a solution in q≥p* (P-1) .
Because the topic gives an interval is 1≤s≤t≤10, so when the distance between the adjacent two stones is not less than 8*9=72, then the distance can be reached, we can think of the distance between them is 72. In this way, we will reduce the scope of the original topic L to 100*72=7200, the dynamic programming algorithm can be completely affordable.
Special, when s=t, then the equation of's inconstancy solution, andF [step1-t]~F [Step1-s]each value in between is not intended to wait, butF [Step1-t]~ F [step1-s] with the F [step-t]~ F [step-s] each value corresponds to equal. So for this situation we cannot simply compress the distance between two stones into 72. But also add the remainder by dividing by 72 .
Compression strategy: then for every two stones between the distance Xi.
① when xi<2*72, do not compress.
② when xi≥2*72, Compress xi=72-(Xi)%72;
The largest total size:100*72=14400.
< Span style= "Color:rgb (54,46,43); Line-height:26px ">
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; int f[3000005]; int stone[105]; BOOL vis[3000005]; int main () {int l,s,t,m;int x[105]; scanf ("%d%d%d%d", &l,&s,&t,&m); int i,j,ans=0; for (i=1;i<=m;i++) scanf ("%d", &stone[i]), if (s==t) {for (i=1;i<=m;i++) if (stone[i]%s==0) ans++;p rintf ("%d\n ", ans); return 0;} stone[0]=0; Sort (stone+1,stone+m+1); Stone[m+1]=l; for (i=1;i<=m;i++) x[i]=stone[i+1]-stone[i]; for (I=1;i<=m;++i) {if ((stone[i+1]-stone[i)) >72) {if (x[i]>72) stone[i+1]=stone[i]+ (stone[i+1]-s Tone[i])%72+72;elsestone[i+1]=stone[i]+ (stone[i+1]-stone[i])%72; } L=stone[m+1]; for (i=1;i<=m;++i) vis[stone[i]]=1; f[0]=0; for (i=1;i<=l+t;++i) {f[i]=m; for (J=S;J<=T;++J) if (i>=j && f[i]>f[i-j]+vis[i]) f[i]=f[i-j]+vi S[i]; } ans=m; for (I=l;i<=l+t;++i) if (Ans>f[i]) ans=f[i]; printf ("%d\n", ans); return 0; }
C Language Dynamic Programming (7) ___ River (Vijos P1002)