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 |
15 | 14430 | 관리자 | 2019.12.21 | 6 |
14 | 1182 | 관리자 | 2019.12.21 | 8 |
13 | 11660 | 관리자 | 2020.04.11 | 46 |
12 | 1149 | 관리자 | 2019.12.21 | 8 |
11 | 10999 | 관리자 | 2020.04.11 | 47 |
10 | 10937 | 관리자 | 2020.04.11 | 41 |
» | 10919 | 관리자 | 2020.04.11 | 42 |
8 | 10800 | 관리자 | 2019.12.21 | 8 |
7 | 10799 | 관리자 | 2019.12.21 | 9 |
6 | 10534 | 관리자 | 2020.04.11 | 44 |
5 | 10164 | 관리자 | 2019.12.21 | 6 |
4 | 10159 | 관리자 | 2019.12.21 | 6 |
3 | 10157 | 관리자 | 2019.12.21 | 8 |
2 | 10070 | 관리자 | 2020.04.11 | 44 |
1 | 1005 | 관리자 | 2019.12.21 | 8 |