题目描述:传送门
题解:
这题是一道贪心题,想法非常神奇。。。
详见原题解:传送门
代码如下:
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10000005;
int n,m,l,s1[maxn],s2[maxn],top1,top2;
long long f1[maxn],f2[maxn],ans;
inline int read(){
int x=0; char ch=getchar();
while (ch<'0'||ch>'9') ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x;
}
int main(){
n=read(),m=read(),l=read();
for (int i=1;i<=n;i++) {
int x=read(); if (x<=l/2) s1[++top1]=x; else s2[++top2]=l-x;
}
for (int i=1;i<=top2/2;i++) swap(s2[i],s2[top2-i+1]);
for (int i=1;i<=top1;i++) if (i<=m) f1[i]=1ll*s1[i]; else f1[i]=f1[i-m]+s1[i];
for (int i=1;i<=top2;i++) if (i<=m) f2[i]=1ll*s2[i]; else f2[i]=f2[i-m]+s2[i];
ans=2*(f1[top1]+f2[top2]);
for (int i=top1-m;i<=top1;i++)
ans=min(ans,2*(f1[i]+f2[max(top2-m+top1-i,0)])+l);
printf("%lld\n",ans);
return 0;
}