# [NOIp2018] 摆渡车

f i f_i 表示在 i i 时刻发车的最小等待时间

f i = min ⁡ 0 ≤ j ≤ i − m { f j + ( c n t i − c n t j ) i − ( s u m i − s u m j ) } f_i=\min\limits_{0\leq j\leq i-m} \{f_j+(cnt_i-cnt_j)i-(sum_i-sum_j)\}

f j + s u m j = i × c n t j − c i × i + s i + f i f_j+sum_j=i\times cnt_j-c_i\times i+s_i+f_i

#include <bits/stdc++.h>
using namespace std;

# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)

typedef long long ll;

const int N=4e6+5;
const int mod=1e9;

x=0;int f=1;
char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
x*=f;
}

int n,m,top;
int a[N];
int cnt[N],sum[N];
int f[N];
int ans=INT_MAX;

int fz(int x,int y){
return f[x]+sum[x]-f[y]-sum[y];
}

int fm(int x,int y){
return cnt[x]-cnt[y];
}

int main()
{
Rep(i,1,n)top=max(top,a[i]);
Rep(i,1,top+m)sum[i]+=sum[i-1],cnt[i]+=cnt[i-1];
Rep(i,0,m-1)f[i]=cnt[i]*i-sum[i];
q[++tail]=0;
Rep(i,m,top+m){
q[++tail]=i-m+1;
}
Rep(i,top,top+m)ans=min(ans,f[i]);
printf("%d\n",ans);
return 0;
}

11-17 4023
08-30 39

11-21 1169
11-22 32
11-11 804
11-10 1972
11-17 414
07-10 171