Reference: https://blog.csdn.net/zqh_wz/article/details/52953516
Miao ah
, see the problem of sections, because the train can only go in batches (easy to prove = =) let f[i] be to i The cars so far have gone back and forth, and the transfer is obviously
\[ f[i]=min{max(f[j]+ij-1,a[i])+ij-1+2*s} \]
Thenproblem explanationThe value of this j is monotonic, so it is enough to increase the maintenance (but it will not prove monotonic)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1000005;
int n,s;
long long f[N],a[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read(),s=read();
for(int i=1;i<=n;i++)
a[i]=read(),f[i]=1e18;
sort(a+1,a+1+n);
for(int i=2;i<=n;i++)
a[i]=max(a[i],a[i-1]+1);
int j=0;
for(int i=1;i<=n;i++,j--)
while(j<i&&max(f[j]+i-j-1,a[i])+i-j-1+2*s<f[i])
f[i]=max(f[j]+i-j-1,a[i])+i-j-1+2*s,j++;
printf("%lld\n",f[n]);
return 0;
}