codeforce 985C Liebig's Barrels(贪心+思维)

Liebig's Barrels
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ 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
Note

In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].

In the second example you can form the following barrels: [10], [10].

In the third example you can form the following barrels: [2, 5].

In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.

 

题意:输入  n  k  l    你要做n个桶,每个桶需要k个木板,用木板拼好的桶相互之间体积的差距<=l,桶的体积大小就是最短的那根木板的长度大小。

第二行 共n*k个数,分别表示n*k个木板的长度。

 

分析:

先对边排个序

不存在的情况,就是a[n]-a[1]>l,那就是不存在,因为要是差距尽可能小,前n小的都分别作为n个桶的一块木板,那么这之中最大的差距就是a[n]-a[1],要是a[n]-a[1]都满足条件(<=l)了,那就满足条件了。

其次,要使体积和最大输出体积和,我毛想想觉得s=a[1]+……a[n],结果WA了,引起了我的深思。

因为:

eg:4    3    17

1   2   3   5   9   13  18  21  22  23  25 26

它可以这样组3组:

18  25  26

13   22  23

1     2     3

5     9   21

这样体积为1+5+13+18=37,不是简单地1 +2 +3 +5=11

 

所以我的思路:先要找到最大的满足条件的数,可以用二分找更快,在这组样例中,是18,它-a[1]<=l,

那么从最后开始去k-1个和18拼,s+=18,再下一个数13(25  26),再从最后找k-1个数(22  23),

再下一个数9,发现再k-1个数不够了,那就从头开始找了,(1   2   3)一组,在去(5   9  13)时,发现13

已经被取走,那就s+=5就可以了。

 

#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
ll a[100005];
bool cmp(ll a,ll b)
{
    return a<b;
}
int main()
{
    ll n,k,l;
    scanf("%I64d%I64d%I64d",&n,&k,&l);
    for(ll i=1;i<=n*k;i++)
    {
        scanf("%I64d",&a[i]);
    }
    sort(a+1,a+1+n*k,cmp);
    if(a[n]-a[1]>l)
    {
        printf("0");
    }
    else
    {
        ll s=0;
        ll p=-1;
        for(ll i=n*k;i>=1;i--)
        {
            if(a[i]-a[1]<=l)
            {
                p=i;//找到标准数,最大的满足条件的数
                break;
            }
        }
        s=0;
        int num=0;//记录从标准数向前取了多少
        int j=p;
        for(ll i=n*k;i-(k-1)>p;i=i-(k-1))//先从后往前取
        {
            s+=a[j--];
            num++;
        }
        for(ll i=1;i<p-num+1;i=i+k)//在从前往后取
        {
            s+=a[i];
        }
        printf("%I64d",s);
    }
    return 0;
}

 

 

 

 

 

 

转载于:https://www.cnblogs.com/caiyishuai/p/9080572.html

### Codeforces 题目难度分布及评级标准 Codeforces 的题目难度范围广泛,涵盖了从新手到专家级别的各种挑战[^1]。该平台通过细致的分层机制来区分不同难度等级的问题,使得每位参赛者都能找到适合自己水平的任务。 #### 评分体系概述 Codeforces 使用基于积分制的评价系统,其中每道题都有一个对应的分数(rating),用于表示其相对难易程度。较低的 rating 表明这是一道较为简单的题目;而较高的 rating 则意味着更高的复杂性和解决难度。通常情况下: - **简单题**:Rating 小于等于 1200 分 - **中等偏难题**:Rating 范围大约在 1600 至 2000 分之间 - **困难题**:Rating 大于等于 2400 分 这些数值并不是固定的界限,而是根据社区反馈以及比赛实际情况调整的结果。 #### Divisions 和 Contest Types 为了更好地适应不同程度的学习者和技术爱好者的需求,Codeforces 提供了多种类型的竞赛活动,比如 Division 1, Division 2, Division 3 及 Division 4 等不同类型的比赛[^2]。每个 division 对应着不同的最低准入门槛——即参与者应该具备的基础能力或经验水平。例如,在更高级别的比赛中会遇到更多高难度的问题。 #### 技术领域覆盖 平台上发布的题目不仅限于单一的技术方向,还涉及到多个计算机科学的重要分支,如贪心算法、动态规划、图论等领域。这种多样性有助于全面锻炼编程技巧并促进跨学科思维的发展。 ```python # 示例 Python 代码片段展示如何获取某场比赛的信息 import requests def get_contest_info(contest_id): url = f"https://codeforces.com/api/contest.standings?contestId={contest_id}&from=1&count=1" response = requests.get(url).json() if 'result' not in response or 'problems' not in response['result']: return None problems = response['result']['problems'] for problem in problems: print(f"{problem['index']}: {problem['name']} - Rating: {problem.get('rating', 'N/A')}") get_contest_info(1669) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值