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 | 173 |
| 74 | 9663 | 관리자 | 2019.12.21 | 124 |
| 73 | 9345 | 관리자 | 2020.04.11 | 160 |
| 72 | 9019 | 관리자 | 2019.12.21 | 125 |
| 71 | 7569 | 관리자 | 2019.12.21 | 123 |
| 70 | 6987 | 관리자 | 2019.12.21 | 125 |
| 69 | 5842 | 관리자 | 2020.04.11 | 161 |
| 68 | 5625 | 관리자 | 2019.12.21 | 119 |
| 67 | 5419 | 관리자 | 2020.04.11 | 166 |
| 66 | 3392 | 관리자 | 2020.04.11 | 166 |
| 65 | 3019 | 관리자 | 2019.12.21 | 121 |
| 64 | 2933 | 관리자 | 2019.12.21 | 123 |
| 63 | 2931 | 관리자 | 2019.12.21 | 121 |
| 62 | 2836 | 관리자 | 2019.12.21 | 120 |
| 61 | 2667 | 관리자 | 2019.12.21 | 123 |
| 60 | 2636 | 관리자 | 2019.12.21 | 123 |
| 59 | 2629 | 관리자 | 2020.04.11 | 156 |
| 58 | 2615 | 관리자 | 2019.12.21 | 121 |
| 57 | 2610 | 관리자 | 2020.04.11 | 158 |
| 56 | 2606 | 관리자 | 2019.12.21 | 121 |
| 55 | 2585 | 관리자 | 2019.12.21 | 127 |
| 54 | 2578 | 관리자 | 2019.12.21 | 121 |
| 53 | 2573 | 관리자 | 2020.04.11 | 161 |
| 52 | 2557 | 관리자 | 2019.12.21 | 121 |
| 51 | 2512 | 관리자 | 2019.12.21 | 121 |
| 50 | 2504 | 관리자 | 2020.04.11 | 157 |
| 49 | 2481 | 관리자 | 2019.12.21 | 122 |
| 48 | 2478 | 관리자 | 2020.04.11 | 158 |
| 47 | 2473 | 관리자 | 2020.04.11 | 163 |
| 46 | 2470 | 관리자 | 2020.04.11 | 161 |