[codevs1105] [COJ0183] [NOIP2005] cross the river
Question 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.
Input
Enter the first line with a positive integer L (1<=l<=109), which represents the length of the bridge. The second line has three positive integer s,t,m, which indicates the frog's minimum distance, maximum distance, and the 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.
Output
The output includes only an integer that indicates the minimum number of stones a frog needs to step across the river.
Input example
Ten 2 3 5 2 3 5 6 7
Output example
2
Data size and conventions
For 30% of data,l<=10000;
For all the data, l<=109.
Exercises
L compare hours, can direct DP: set F (i) to indicate the minimum number of stones that have been trampled at position I. The positive solution is the same as it does, just to find that many nodes do not need to be considered, so we can ignore them. Violence find S, T respectively take 1~10 when the maximum non-table number, found only 71, then when the adjacent two stones between the distance of more than 71 o'clock, we can turn this distance to 71 modulo plus twice times 71, the last violent DP a bit better. Note the case where S = T is determined.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cctype > #include <algorithm>using namespace Std;int read () {int x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if ( c = = '-') f =-1; c = GetChar (); }while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 27280int L, S, T, N, A[maxn], F[maxn];bool has[maxn];void up (int& a, int b) {if (a < 0) A = b;else a = Min (A, b); return;} int main () {L = read (); S = Read (); T = Read (); n = read (); for (int i = 1; I <= n; i++) A[i] = Read (), sort (A + 1, a + n + 1), if (S = = T) {int cnt = 0;for (int i = 1; I &l t;= N; i++) if (a[i]% S = = 0) cnt++;return printf ("%d\n", CNT), 0;} int p = 0;for (int i = 1; I <= n; i++) if (A[i]-a[i-1] <=) p + = A[i]-a[i-1], has[p] = 1;else p + = (A[i]-a[i-1] )% + 142, has[p] = 1;memset (f,-1, sizeof (f)), f[0] = 0;for (int i = 0; I <= p + 1; i++) if (F[i] >= 0) for (int j = S J <= T; J + +) {int tmp = (i + j <= p + 1)? i + j:p + 1;up (f[tmp], f[i] + has[tmp]);} printf ("%d\n", f[p+1]); return 0;}
[codevs1105] [COJ0183] [NOIP2005] cross the river