## 洛谷P5017：摆渡车——题解

https://www.luogu.org/problem/P5017

\$f[i][j]=min(f[i][j],f[k][l]+sum(k+1,i,t[i]+j))\$（你得保证变量合法）

```#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=505;
const int M=105;
const ll INF=1e18;
int X=0,w=0;char ch=0;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
ll t[N],s[N],f[N][M];
ll sum(int l,int r,ll now){
return now*(r-l+1)-s[r]+s[l-1];
}
int main(){
sort(t+1,t+n+1);
for(int i=1;i<=n;i++)s[i]=s[i-1]+t[i];
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++)f[i][j]=sum(1,i,t[i]+j);
for(int j=1;j<i;j++){
for(int k=0;k<m;k++){
ll now=max(t[j]+k+m,t[i]);
f[i][now-t[i]]=min(f[i][now-t[i]],f[j][k]+sum(j+1,i,now));
}
}
}
ll ans=INF;
for(int i=0;i<m;i++)ans=min(ans,f[n][i]);
printf("%lld\n",ans);
return 0;
}```

+++++++++++++++++++++++++++++++++++++++++++

+本文作者：luyouqi233。 　　　　　　　　　　　　　　+

+欢迎访问我的博客：http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++