#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
long long a,b,i,j,t,p,q,m,n,s,x[160001],y[160001];
main()
{
scanf("%lld%lld",&a,&b);
for(i=0;i<a;i++)
{
scanf("%lld",&x[i]);
}
std::sort(x,x+a);
for(i=0;i<a-1;i++)
{
p=q=0;
if(i-1>=0)
{
if(x[i]-x[i-1]<b)p=b-(x[i]-x[i-1]);
}
if(i+2<a)
{
if(x[i+2]-x[i+1]<b)q=b-(x[i+2]-x[i+1]);
}
if(p+q>=x[i+1]-x[i])s+=x[i+1]-x[i];
else if(b>=x[i+1]-x[i])
{
s+=x[i+1]-x[i];
}
else if(2*b<=x[i+1]-x[i])s+=p+q;
else
{
t=b*2-(x[i+1]-x[i]);
m=x[i]+b-t;
n=x[i]+b;
p=p+x[i];
q=x[i+1]-q;
if(p<m&&n<q)
{
s+=(p-x[i]+x[i+1]-q+t);
}
else if(n<=p||q<=m)
{
s+=(p-x[i]+x[i+1]-q);
}
else if(m<=p&&q<=n)
{
s+=x[i+1]-x[i];
}
else if(m<=p&&p<=n)
{
s+=(n-x[i]+x[i+1]-q);
}
else if(m<=q&&q<=n)
{
s+=(p-x[i]+x[i+1]-m);
}
}
}
if(x[1]-x[0]<b)
{
s+=b-(x[1]-x[0]);
}
if(x[a-1]-x[a-2]<b)
{
s+=b-(x[a-1]-x[a-2]);
}
if(a==1)printf("0");
else printf("%lld",s);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3584 KB |
Output is correct |
2 |
Correct |
0 ms |
3584 KB |
Output is correct |
3 |
Correct |
0 ms |
3584 KB |
Output is correct |
4 |
Correct |
0 ms |
3584 KB |
Output is correct |
5 |
Correct |
0 ms |
3584 KB |
Output is correct |
6 |
Correct |
0 ms |
3584 KB |
Output is correct |
7 |
Correct |
0 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3584 KB |
Output is correct |
2 |
Correct |
0 ms |
3584 KB |
Output is correct |
3 |
Correct |
0 ms |
3584 KB |
Output is correct |
4 |
Correct |
0 ms |
3584 KB |
Output is correct |
5 |
Correct |
0 ms |
3584 KB |
Output is correct |
6 |
Correct |
0 ms |
3584 KB |
Output is correct |
7 |
Correct |
0 ms |
3584 KB |
Output is correct |
8 |
Correct |
0 ms |
3584 KB |
Output is correct |
9 |
Correct |
0 ms |
3584 KB |
Output is correct |
10 |
Correct |
0 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3584 KB |
Output is correct |
2 |
Correct |
23 ms |
3584 KB |
Output is correct |
3 |
Correct |
19 ms |
3584 KB |
Output is correct |
4 |
Correct |
40 ms |
3584 KB |
Output is correct |
5 |
Correct |
42 ms |
3584 KB |
Output is correct |
6 |
Correct |
33 ms |
3584 KB |
Output is correct |
7 |
Correct |
19 ms |
3584 KB |
Output is correct |
8 |
Correct |
25 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
3584 KB |
Output is correct |
2 |
Correct |
20 ms |
3584 KB |
Output is correct |
3 |
Correct |
48 ms |
3584 KB |
Output is correct |
4 |
Correct |
42 ms |
3584 KB |
Output is correct |
5 |
Correct |
19 ms |
3584 KB |
Output is correct |
6 |
Correct |
16 ms |
3584 KB |
Output is correct |
7 |
Correct |
30 ms |
3584 KB |
Output is correct |
8 |
Correct |
20 ms |
3584 KB |
Output is correct |
9 |
Correct |
25 ms |
3584 KB |
Output is correct |
10 |
Correct |
21 ms |
3584 KB |
Output is correct |
11 |
Correct |
50 ms |
3584 KB |
Output is correct |
12 |
Correct |
35 ms |
3584 KB |
Output is correct |
13 |
Correct |
38 ms |
3584 KB |
Output is correct |
14 |
Correct |
0 ms |
3584 KB |
Output is correct |
15 |
Correct |
37 ms |
3584 KB |
Output is correct |