codeforces 657C - Bear and Contribution [想法题]

--------------------------------------------------------------------------------------------------------

 1 #include <cstdio>
2 #include <cstring>
3 #include <cmath>
4 #include <algorithm>
5 #include <queue>
6 using namespace std;
7 const int N = 2e5 + 10;
8 const long long inf = 1e9;
9 int a[N], dis[N * 5];
10 long long sum[5];
11 long long ans = 1e18;
12 int n, k, b, c, cnt;
13 priority_queue <long long> q[5];
14 int main()
15 {
16     scanf("%d%d%d%d", &n, &k, &b, &c);
17     b = min(b, c * 5);
18     for(int i = 1; i <= n; ++i)
19     {
20         scanf("%d", &a[i]);
21         for(int j = 0; j < 5; ++j)
22             dis[cnt++] = a[i] + j;
23     }
24     sort(a + 1, a + 1 + n);
25     sort(dis, dis + cnt);
26     cnt = unique(dis, dis + cnt) - dis;
27     int now = 1;
28     for(int i = 0; i < cnt; ++i)
29     {
30         int x = dis[i], kind =(inf + x) % 5;
31         long long cost;
32         while(now <= n && a[now] <= x)
33         {
34             for(int j = 0; j < 5; ++j)
35             {
36                 cost = (inf + j - a[now]) / 5 * b + (inf + j - a[now]) % 5 * c;
37                 q[j].push(cost);
38                 sum[j] += cost;
39                 if((int)q[j].size() > k)
40                 {
41                     sum[j] -= q[j].top();
42                     q[j].pop();
43                 }
44             }
45             ++now;
46         }
47         long long y = (inf + kind - x) / 5 * b * k;
48         if((int)q[kind].size() == k)
49             ans = min(ans, sum[kind] - y);
50     }
51     printf("%lld\n", ans);
52     return 0;
53 }

posted @ 2016-04-11 20:32 sagitta 阅读(...) 评论(...) 编辑 收藏