Educational Codeforces Round 44 (Rated for Div. 2) C. Liebig's Barrels

C. Liebig's Barrels

You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.

Let volume vj of barrel j be equal to the length of the minimal stave in it.

You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.

Input

The first line contains three space-separated integers nk and l (1 ≤ n, k ≤ 1051 ≤ n·k ≤ 1050 ≤ l ≤ 109).

The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.

Output

Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Examples
input
Copy
4 2 1
2 2 1 2 3 2 2 3
output
Copy
7
input
Copy
2 1 0
10 10
output
Copy
20
input
Copy
1 2 1
5 2
output
Copy
2
input
Copy
3 2 1
1 2 3 4 5 6
output
Copy
0

题意:现在给你n*k个数,要你构造成n组,每组有k个数,将每组中的最小的数字相加,即为所求解,现在问你最大的解是多少,同时要保证每组最小值之差不超过l。

思路:贪心即可,我们必然会选取n个数,在保证这n个数任意之差不超过l的前提下,选出最大和。

1.首先排个序,那么我们所有可选数字的范围:[a[1],a[1]+l],很明显,我们要选取的n个数字,就在这个范围中,如果这个范围数字个数小于n,即不可能有答案。

2.假设当前可选的数字范围最右端的下标是pos,即可选的数字个数是pos,剩下的个数num=n*k-pos,这num个数是待匹配的,此时从pos开始,向左逐个选取,当做每组的最小值,每选一个,pos--,num-k一次,当不够减时,也就说明不能连续选取了,右边的已经不够匹配了。

3.此时开始每隔k个间隔选取一次,当从开始到现在选取够n-1个数字时,可以停下了,因为n*k个数中最小的一个a[1]必然要入选,在最后sum+=a[1]即可,当然如果选不够n-1个,直接判定不存在选取方案。

ps:有一个特判,当pos>=n-k+1时,此时是最简单,所有方案中的最优解,即:a[1],a[1+k],a[1+2*k]...,求和输出即可,想还是很好想,就是bug太多,代码实现有点麻烦。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define exp 1e-8
#define mst(a,k) memset(a,k,sizeof(a))
#define pi acos(-1.0)
using namespace std;
ll n,k,l,a[100010];
int main()
{
    while(~scanf("%lld%lld%lld",&n,&k,&l))
    {
        ll n1=n;
        n=n*k;
        for(ll i=1;i<=n;i++)scanf("%lld",&a[i]);
        sort(a+1,a+n+1);
        ll sum=0,pos=-1,num;
        for(ll i=n;i>=1;i--)   //找到能选取的大位置
        {
            if(a[i]-a[1]<=l)
            {
                pos=i;
                break;
            }
        }
        if(pos<n1){printf("0\n");continue;}   //如果可选的不够n,必然不成立
        if(pos>=n-k+1)   //如果可选的足够多,直接输出最优解
        {
            for(ll i=1;i<=n;i+=k)
            {
                sum+=a[i];
            }
            printf("%lld\n",sum);
            continue;
        }
        num=n-pos;   //待匹配的个数
        sum+=a[1];
        n1--;
        for(ll i=pos;i>1;i--)   //开始从右向左连续选取
        {
            if(n1==0)break;
            if(num>=k)
            {
                num-=(k-1);
                sum+=a[i];
                n1--;
            }
            else
            {
                pos=i;
                break;
            }

        }
        pos=pos-k+num+1;
        for(ll i=pos;i>1;i-=k)    //开始间隔选取
        {
            if(n1==0)break;
            sum+=a[i];
            n1--;
        }
        if(n1)printf("0\n");   //如果选的数字不够,必然不成立
        else printf("%lld\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值