4368: [IOI2015]boxes纪念品盒

4368: [IOI2015]boxes纪念品盒

链接

分析
  链接

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4   
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9   
10 const int N = 1e7+10;
11 int s1[N],s2[N],p1,p2;
12 LL f1[N],f2[N]; //-
13 
14 int main() {
15     int n = read(),k = read(),L = read();
16     for (int i=1; i<=n; ++i) {
17         int x = read();
18         if (x <= L/2) s1[++p1] = x;
19         else s2[++p2] = L - x;
20     }
21     for (int i=1,lim=p2/2; i<=lim; ++i) swap(s2[i],s2[p2-i+1]);
22     for (int i=1; i<=p1; ++i) {
23         if (i <= k) f1[i] = s1[i];
24         else f1[i] = f1[i-k] + s1[i];
25     }
26     for (int i=1; i<=p2; ++i) {
27         if (i <= k) f2[i] = s2[i];
28         else f2[i] = f2[i-k] + s2[i];
29     }
30     LL ans = 2 * (f1[p1] + f2[p2]);
31     for (int i=p1-k; i<=p1; ++i) {
32         ans = min(ans,2*(f1[i]+f2[max(p2-k+p1-i,0)])+L);
33     }
34     cout << ans;
35     return 0;
36 }

 

posted @ 2018-07-05 17:30  MJT12044  阅读(394)  评论(0)    收藏  举报