过河

题目描述

在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子,青蛙很讨厌踩在这些石子上。由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数轴上的一串整点:0,1,…,L0,1,,L(其中LL是桥的长度)。坐标为00的点表示桥的起点,坐标为LL的点表示桥的终点。青蛙从桥的起点开始,不停的向终点方向跳跃。一次跳跃的距离是SS到TT之间的任意正整数(包括S,TS,T)。当青蛙跳到或跳过坐标为LL的点时,就算青蛙已经跳出了独木桥。

题目给出独木桥的长度LL,青蛙跳跃的距离范围S,TS,T,桥上石子的位置。你的任务是确定青蛙要想过河,最少需要踩到的石子数。

输入输出格式

输入格式:

第一行有11个正整数L(1 le L le 10^9)L(1L109),表示独木桥的长度。

第二行有33个正整数S,T,MS,T,M,分别表示青蛙一次跳跃的最小距离,最大距离及桥上石子的个数,其中1 le S le T le 101ST10,1 le M le 1001M100。

第三行有MM个不同的正整数分别表示这MM个石子在数轴上的位置(数据保证桥的起点和终点处没有石子)。所有相邻的整数之间用一个空格隔开。

输出格式:

一个整数,表示青蛙过河最少需要踩到的石子数。

f[i]表示到第i个距离出最少踩几块石头

f[i]=min{f[i-j]+a[i]}   (s<=j<=t)

要进行缩点

from:Panda_hu

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<vector>
 7 #include<queue>
 8 #include<stack>
 9 using namespace std;
10 const int maxn=100007;
11 const int INF=0x7f7f7f7f;
12 int read(){
13   int x=0,f=1;char s=getchar();
14   while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
15   while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
16   return x*f;
17 }
18 int l,s,t,m,ans=INF;;
19 int a[maxn],pos[maxn],f[maxn];
20 bool is[maxn];
21 int main(){
22   memset(f,INF,sizeof(f));
23   l=read();s=read();t=read();m=read();
24   for(int i=1;i<=m;i++) a[i]=read();
25   if(s==t){
26       int ret=0;
27     for(int i=1;i<=m;i++){
28       if(a[i]%s==0) ret++;
29     }
30     printf("%d
",ret);
31     return 0;
32   } 
33   pos[0]=0;f[0]=0;
34   sort(a+1,a+m+1);
35   for(int i=1;i<=m;i++){
36     pos[i]=min(a[i]-a[i-1],90);
37     pos[i]+=pos[i-1];
38     is[pos[i]]=true;
39   }
40   pos[m+1]=min(l-a[m],90);
41   pos[m+1]+=pos[m];
42   for(int i=1;i<pos[m+1]+t;i++){
43     for(int j=s;j<=t;j++){
44       if(i-j<0) continue;
45       if(is[i]==true) f[i]=min(f[i],f[i-j]+1);
46       else f[i]=min(f[i],f[i-j]);
47     }
48   }
49   for(int i=pos[m+1];i<pos[m+1]+t;i++) ans=min(ans,f[i]);
50   printf("%d
",ans);
51   return 0;
52 } 
原文地址:https://www.cnblogs.com/lcan/p/9876739.html