NOIP 2005 frog across the river

Source: Internet
Author: User

Title Record: 2016-08-10 21:58:09

Title 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.

Input format  

The 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.

Output format  

The output includes only an integer that indicates the minimum number of stones a frog needs to step across the river.

Sample input

10
2 3 5
2 3 5) 6 7

Sample output

2

Code

1#include <iostream>2#include <algorithm>3#include <cstring>4 using namespacestd;5 intMain ()6 {7 intl,s,t,m;8 inta[100000],f[100000],sz[100000];9memset (SZ,0,sizeof(SZ));TenMemset (A,0,sizeof(a)); OneMemset (F,127,sizeof(f)); ACin>>l>>s>>t>>m; -  for(intI=1; i<=m;i++) -Cin>>A[i];  the if(s==t) - { -     intans=0; -      for(intI=1; i<=m;i++) +     { -         if(a[i]%s==0) ans++; +     } Acout<<ans; at     return 0; - } -Sort (a,a+m+1); -a[m+1]=l; -  for(intI=0; i<=m;i++)if(a[i+1]-a[i]> -) a[i+1]=a[i]+ (a[i+1]-a[i])% -; -  for(intI=1; i<=m;i++) sz[a[i]]=1; in  for(inti=s;i<=t;i++) {if(Sz[i]) f[i]=1;Elsef[i]=0;} -  for(intI=2*s;i<=a[m+1];i++) to    { +     for(intj=s;j<=t;j++) -    { the       if(J>i) Break; *F[i]=min (f[i-j],f[i]); $    }Panax Notoginseng    if(Sz[i]) f[i]++; - }  thecout<<f[a[m+1]]<<Endl; + //System ("pause"); A return 0; the}

Explanation: http://blog.csdn.net/zzp441524586/article/details/7654269

First case:

When the s=t, very simple, the frog stepping on the stone must be a multiple of s, then as long as the statistics of all the stones in the number of S is a multiple, the output can be.

Second case:

S<t, state compression.

NOIP 2005 frog across the river

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.