C++
#include <cstdio>
#include <climits>
#include <algorithm>
using namespace std;
int N,K,L,pos[10000010];
long long dt[10000010],result;
int L_2_idx,nL;
int main()
{
scanf("%d %d %d",&N,&K,&L);
#if 1
for(int i=0;i<N;i++){
scanf("%d",&pos[i]);
}
int i;
for(i=0;i<N;i++){
if(pos[i]>L/2)
break;
dt[i]=pos[i]*2;
if(i>=K)
dt[i]+=dt[i-K];
}
nL=i-1;
for(int j=N-1;j>=i;j--){
dt[j]=(L-pos[j])*2;
if(N-j>K)
dt[j]+=dt[j+K];
}
#else
for(int i=0;i<N;i++){
int x;
scanf("%d",&x);
if(x > L / 2)
{
dt[i] = (L - x) * 2;
}
else
{
dt[nL++] = x * 2;
}
}
nL--;
for(int i = K; i <=nL; ++i)
dt[i] += dt[i - K];
for(int i = N - 1 - K; i > nL; --i)
dt[i] += dt[i+K];
#endif
result = 0;
if(nL>=0){
//printf("%lld",dt[nL+1]);
//return 0;
result=dt[nL];
}
if(nL+1 < N)
{
//printf("%lld",dt[nL]);
//return 0;
result+=dt[nL + 1];
}
for(int i=0;i<=K;i++){
long long l = (nL-i<0) ? 0 : dt[nL-i];
long long r = (nL+(K-i)+1>=N) ? 0 : dt[nL+(K-i)+1];
result=min(result,l+r+L);
}
printf("%lld",result);
return 0;
}
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
공지 | 안내사항 | 관리자 | 2019.12.21 | 164 |
74 | 9663 | 관리자 | 2019.12.21 | 115 |
73 | 9345 | 관리자 | 2020.04.11 | 151 |
72 | 9019 | 관리자 | 2019.12.21 | 114 |
71 | 7569 | 관리자 | 2019.12.21 | 114 |
70 | 6987 | 관리자 | 2019.12.21 | 114 |
69 | 5842 | 관리자 | 2020.04.11 | 152 |
68 | 5625 | 관리자 | 2019.12.21 | 110 |
67 | 5419 | 관리자 | 2020.04.11 | 157 |
66 | 3392 | 관리자 | 2020.04.11 | 157 |
65 | 3019 | 관리자 | 2019.12.21 | 112 |
64 | 2933 | 관리자 | 2019.12.21 | 113 |
63 | 2931 | 관리자 | 2019.12.21 | 112 |
62 | 2836 | 관리자 | 2019.12.21 | 111 |
61 | 2667 | 관리자 | 2019.12.21 | 114 |
60 | 2636 | 관리자 | 2019.12.21 | 114 |
59 | 2629 | 관리자 | 2020.04.11 | 147 |
58 | 2615 | 관리자 | 2019.12.21 | 112 |
57 | 2610 | 관리자 | 2020.04.11 | 147 |
56 | 2606 | 관리자 | 2019.12.21 | 112 |
55 | 2585 | 관리자 | 2019.12.21 | 114 |
54 | 2578 | 관리자 | 2019.12.21 | 112 |
53 | 2573 | 관리자 | 2020.04.11 | 152 |
52 | 2557 | 관리자 | 2019.12.21 | 112 |
51 | 2512 | 관리자 | 2019.12.21 | 112 |
50 | 2504 | 관리자 | 2020.04.11 | 148 |
49 | 2481 | 관리자 | 2019.12.21 | 113 |
48 | 2478 | 관리자 | 2020.04.11 | 149 |
47 | 2473 | 관리자 | 2020.04.11 | 154 |
46 | 2470 | 관리자 | 2020.04.11 | 152 |