- 过河
- 2017-05-13 15:39:44 @
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=10000000;
inline const int small(int a,int b)
{
if(a<b)return a;
else return b;
}
inline const bool compare(int a,int b)
{
if(a<b)return true;
else return false;
}
int s,t;//跳跃范围
int l,m;//桥长 石头数 起点
int location[101],loc[101];//石位置 石头之间的距离
int f[100001];//记录最优前缀
bool stone[100001];//石头在否
void clean()//清理石头的距离,除去废距
{
location[0]=0;
int sum=0;
for(int i=1;i<=m;i++)//压缩
{
if(location[i]-location[i-1]>t)
{
sum+=location[i]-location[i-1]-t;
loc[i]=loc[i-1]+t;
}
else loc[i]=loc[i-1]+location[i]-location[i-1];
stone[loc[i]]=true;
}
l-=sum;
}
void dp()
{
for(int i=1;i<=l+t;i++)f[i]=inf;
f[0]=0;
for(int i=0;i<=loc[m];i++)
for(int j=s;j<=t;j++)
{
if(stone[i+j])f[i+j]=small(f[i+j],f[i]+1);
else f[i+j]=small(f[i+j],f[i]);
}
}
void ans()
{
int p=inf;
for(int i=loc[m];i<=loc[m]+t;i++)p=small(f[i],p);
cout<<p<<endl;
}
void special()
{
int p=0;
for(int i=1;i<=m;i++)
if(!location[i]%s)p++;
cout<<p<<endl;
}
int main()
{
freopen("过河的测试数据.txt","r",stdin);
memset(loc,0,sizeof(loc));
memset(stone,false,sizeof(stone));
scanf("%d%d%d%d",&l,&s,&t,&m);
for(int i=1;i<=m;i++)
scanf("%d",&location[i]);
if(s==t)
{
special();
return 0;
}
sort(location+1,location+m,compare);
clean();
dp();
ans();
return 0;
}
哪里错了!!!