Link: http://www.rqnoj.cn/problem/87
Idea: Dynamic Planning
Define f [I] [J] to indicate the minimum number of steps to reach the given stone on block I with J stones added.
Transfer equation: f [I] [J] = min {f [k] [J-TMP [POS [I]-pos [k] + 1] + TMP [POS [I]- pos [k]}, where 0 <= k <I and TMP [POS [I]-pos [k]-1 <= J
Pos [I] indicates the coordinate of the given stone in block I, and TMP [I] indicates the stone to be added across the distance I (equivalent to starting from coordinate 0 to coordinate I, note that there is no stone at coordinate I, and you need to put one block ). TMP [I] can be preprocessed. tmp [0] = 0, TMP [I] = min {TMP [I-j]} + 1, S <= j <= T and j <= I.
Last statistical answer, enumeration of the final settled given stone I, Used Stone J and the position of the final settled stone K (L-T + 1 <= k <= L and K> = POS [I] and J + TMP [k-pos [I] <= m ), ans = min {f [I] [J] + TMP [k-pos [I] + 1 }. If ans = inf is unable to reach the other side and the longest distance can be calculated, enumerate the given stone I that finally settled. If f [I] [J] <INF, ans = max {pos [I] + (m-j) * t }.
My implementation:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace STD; 5 # define maxl 100020 6 # define maxn 120 7 # define maxm 20020 8 # define INF 100020 9 int TMP [maxl], POS [maxn], F [maxn] [maxm]; 10 int L, n, m, S, T; 11 int ans; 12 inline void get_int (Int & RET) 13 {14 char ch; 15 bool flag = false; 16 For (; CH = getchar (), Ch <'0' | ch> '9';) 17 if (CH = '-') 18 flag = true; 19 For (ret = CH-'0'; CH = getchar (), ch> = '0' & Ch <= '9 '; ret = RET * 10 + CH-'0'); 20 flag & (ret =-RET); 21} 22 int main () 23 {24 get_int (L ); get_int (n); get_int (m); get_int (s); get_int (t); 25 int I, j, k; 26 for (I = 1; I <= N; ++ I) 27 get_int (Pos [I]); 28 memset (TMP, 0x3f, sizeof (TMP )); // pre-processing TMP [I] indicates the minimum number of stones placed from I. 29 TMP [0] = 0; 30 for (I = 1; I <= L; ++ I) 31 For (j = s; j <= T & I> = J; ++ J) 32 TMP [I] = min (TMP [I], TMP [I-j] + 1); 33 memset (F, 0x3f, sizeof (f); // 34 f [0] [0] = 0; 35 For (I = 1; I <= N; ++ I) // DP f [I] [J] indicates the minimum number of steps 36 for (j = 0; j <= m; ++ J) 37 for (k = 0; k <I; ++ K) 38 If (TMP [POS [I]-pos [k]-1 <= J) 39 f [I] [J] = min (F [I] [J], f [k] [J-TMP [POS [I]-pos [k] + 1] + TMP [POS [I]-pos [k]); 40 ans = inf; 41 for (I = 0; I <= N; ++ I) // statistical answer 42 for (j = 0; j <= m; + + J) 43 for (k = L; k> = L-T + 1 & K> = POS [I]; -- K) 44 If (J + TMP [k-pos [I] <= m) 45 ans = min (ANS, f [I] [J] + TMP [k-pos [I] + 1); 46 If (ANS = inf) // calculate the coordinate 47 {48 ans = 0; 49 for (I = 0; I <= N; ++ I) 50 for (j = 0; j <= m; ++ J) 51 if (F [I] [J] <inf) 52 ans = max (ANS, POS [I] + (m-j) * t); 53} 54 printf ("% d \ n", ANS); 55 return 0; 56}View code
Efficiency: