Hust OJ 1186 frog across the river (DP) _DP

Source: Internet
Author: User
Tags first row
Frog River Time limit:1000 MS Memory limit:65535 K Total submit:289 (all users) total accepted:109 (users) rating:special J Udge:no Description has a log bridge over the river, and a frog wants to jump from one side of the river to the other side along the log bridge. There are some stones on the bridge, and frogs hate stepping on these stones. Since the length of the bridge is a positive integer from the distance the frog skips, we can think of the point where the frog might arrive on the log, as a string of dots on the axis: 0,1,......,l (where L is the length of the bridge). A point with a coordinate of 0 indicates the starting point of the bridge, and the point with the coordinate L indicates the end of the bridge. The frog starts at the beginning of the bridge and jumps towards the end. The distance of a jump is any positive integer (including s,t) between S and T. When the frog jumps or skips the point where the coordinates are L, even the frog has jumped out of the log.


The title gives the length of the log bridge, the distance range of the frog jumping s,t, and the position of the stone. Your task is to make sure that the frog wants to cross the river, at least the number of stones needed to step. Input has multiple sets of test data.
For each set of test data, the first row is four positive integers l, s, T, N (1 <= L <= 10^5, 1 <= s <= t <= 10,1 <= n <= 100), respectively, indicating the length of the log bridge, the minimum distance a frog jumps at a time, the most Large distances, and the number of stones on the bridge. The second row has n different positive integers representing the position of the N-Stones on the axis (the data guarantees that the bridge's starting and ending points are free of stones). All adjacent integers are separated by a single space. Output one row per set of test data, including an integer, indicating the minimum number of stones the frog needs to step across the river. Sample Input 10 2 3 5
2 3 5 6 7 Sample Output

2

Mark out where the stones are, and then run the DP from the back.

State transition equation: dp[i] = min (dp[i],dp[i+j]) s<=j<=t; Find the minimum number of stones to step in before adding the current stone

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int MAXN = 100015;
const int INF = 0X3F3F3F;
int DP[MAXN];
int VIS[MAXN];
int l,s,t,n;

int main ()
{while
    (~scanf ("%d%d%d%d", &l,&s,&t,&n))
    {
        memset dp,0,sizeof (DP) );
        memset (Vis) (vis,0,sizeof);
        int index;
        for (int i=0;i<n;i++)
        {
            scanf ("%d", &index);
            Vis[index] = 1;
        }

        for (int i=l;i>=0;i--)
        {
            dp[i] = Inf;
            for (int j=s;j<=t;j++)
            {
                Dp[i] = min (dp[i],dp[i+j]);
            }
            Dp[i] + = Vis[i];
        }

        printf ("%d\n", Dp[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.