[Frogs are excited] crossing the river

Source: Internet
Author: User

[Description]

There is a pig Bridge On The River. A frog wants to jump from one side of the river to the other side. There are some stones on the bridge, and frogs hate to step on these stones. Because the length of the bridge is a positive integer from the distance that frogs Skip at a time, we can regard the points that frogs may reach on the bridge as a series of points on the number axis: 0, 1 ,......, L (where L is the length of the bridge ). A point whose coordinates are 0 indicates the start point of the bridge, and a point whose coordinates are L indicates the end point of the bridge. The frog starts from the starting point of the bridge and keeps jumping to the ending point. The distance of a hop is any positive integer (including S and T) between S and T ). When a frog jumps to or skips a point whose coordinate is l, even if the frog has jumped out of the bridge.
The question shows the length of the bridge, the distance between frogs and the distance S, T, and the stone position on the bridge. Your task is to determine the number of stones that a frog must step on to cross the river.

[Input format]

The first line of the input file has a positive integer L (1 <= L <= 10 ^ 9), indicating the length of the bridge. The second row has three positive integers, S, T, and m, respectively, representing the minimum distance, maximum distance, and the number of stones on the bridge for a frog jump, 1 <= S <= T <= 10, 1 <= m <= 100. The third row has m different positive integers, indicating the position of the M stones on the number axis (data ensures that there are no stones at the start and end points of the bridge ). All adjacent integers are separated by a space.

[Output format]

The output file contains only one integer, indicating the number of stones that the frog must step on when crossing the river.

[Sample output]

10
2 3 5
2 3 5 6 7

[Example input]

2

[Analysis]

The dynamic equations are well written.

F [I] = min {f [I-j]} + F [I];

Initialization: F [I] = 1 with stones.

But l is very large, and m is very small, we consider compressing the distance between stones. But how much is this distance compressed. The problem I found on the Internet is compressed by S * t.

# Include <stdio. h> # include <stdlib. h> # define maxn 100010 # define maxint 00000010int stone [maxn], F [maxn]; int L, S, T, ANS, M, P; int CMP (const void * a, const void * B) {int c = * (int *) A, D = * (int *) B; If (C <D) return-1; if (C> d) return 1; return 0;} int main () {scanf ("% d", & L, & S, & T, & M); For (INT I = 1; I <= m; ++ I) scanf ("% d", & stone [I]); if (S = T) {for (INT I = 1; I <= m; ++ I) if (stone [I] % s = 0) ++ ans; printf ("% d \ n", ANS); Return 0;} stone [0] =-1; qsort (stone, m + 1, sizeof (INT), CMP ); stone [0] = 0; stone [M + 1] = L; P = S * t; for (INT I = 1; I <= m + 1; ++ I) {If (stone [I]-stone [I-1]> P) {int K = stone [I]-stone [I-1]-P; for (Int J = I; j <= m + 1; ++ J) Stone [J]-= K;} f [Stone [I] = 1 ;} L = stone [M + 1]; for (INT I = 1; I <L + T; ++ I) {int min = maxint; For (Int J = s; j <= T; ++ J) if (I-j> = 0) & (f [I-j] <min )) min = f [I-j]; F [I] + = min;} ans = maxint; For (INT I = L; I <L + T; ++ I) if (F [I] <ans) ans = f [I]; printf ("% d \ n", ANS); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.