原题:
C. Liebig’s Barrels
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 n, k and l (1 ≤ n, k ≤ 10^5, 1 ≤ n·k ≤ 10^5, 0 ≤ l ≤ 10^9).

The second line contains m = n·k space-separated integers a1, a2, …, am (1 ≤ ai ≤ 10^9) — 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
4 2 1
2 2 1 2 3 2 2 3
output
7
input
2 1 0
10 10
output
20
input
1 2 1
5 2
output
2
inputCopy
3 2 1
1 2 3 4 5 6
output
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个板子,每个板子长度为ai,现在让你把这n×k个板子分成n组,每组k个,每组当中取长度最小的值求和,且要求在这些组当中,任意取两组,两个组中各自的最小值相差不超过l,现在问你这个和最大是多少?

代码:

#include<iostream>
#include<limits>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
typedef long long ll;int n, k, l;
ll a[100001];int main()
{ios::sync_with_stdio(false);while (cin >> n >> k >> l){int ter = n*k;for (int i = 1; i <= ter; i++)cin >> a[i];sort(a + 1, a + 1 + ter);int ind = ter;for (int i = 2; i <= ter; i++){if (a[i] - a[1]>l){ind = i - 1;break;}}ll ans = 0;if (n == 1){cout << a[1] << endl;continue;}if (ind<n){cout << 0 << endl;continue;}if (k == 1){for (int i = 1; i <= ind; i++)ans += a[i];cout << ans << endl;continue;}int res = ter - ind, pre = ind, flag = 1;for (int i = ind; i >= 1;){if (res>0){if (res >= (k - 1)){res -= (k - 1);ans += a[i];i--;pre = i;}else{while ((pre - i + 1) + res<k)i--;ans += a[i];flag = 0;res = 0;}}else{if (flag != -1){if (flag!=0)i++;flag = -1;}i -= k;if (i<1)break;ans += a[i];}}cout << ans << endl;}return 0;
}

思路:

要求所有组中最小值的和最大,听起来像是二分,贪心可以解决。那就是要让每组当中的最小值从最大的值开始选取即可。

首先从小到大排序,找到最后一个小于最小值加上l的下标ind,从ind向前依次取值作为每一组当中的最小长度,同时判断在下标ind后面是否有元素,如果有,尽量选取k个,与前面选取到的最小长度组成一组。

如果ind后面没有元素了,那么就一次从后向前组成一组即可。

例如
4 2 2
1 2 2 3 3 4 5 6

上面序列是排好序的,最小值是1,加上l=2以后是3。

那么ind值等于5

从ind向前依次选取最小元素与ind后面的值组成一组
[3,4]
[3.5]
[2,6]

4,5,6都被选抽走后,依次从后往前组成组即可

[1,2]

结果为9

cf Educational Codeforces Round 44 C. Liebig's Barrels相关推荐

  1. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 'A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  2. coderforce Educational Codeforces Round 44 (Rated for Div. 2) C(赛后补题)

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

  3. cf Educational Codeforces Round 133 C. Robot in a Hallway

    原题: There is a grid, consisting of 2 rows and m columns. The rows are numbered from 1 to 2 from top ...

  4. cf Educational Codeforces Round 85 (Rated for Div. 2)B. Middle Class

    题目链接:https://codeforces.com/contest/1334/problem/B B. Middle Class time limit per test2 seconds memo ...

  5. CF Educational Codeforces Round 57划水记

    因为是unrated于是就叫划水记了,而且本场也就用了1h左右. A.B:划水去了,没做 C:大水题,根据初三课本中圆的知识,可以把角度化成弧长,而这是正多边形,所以又可以化成边数,于是假设读入为a, ...

  6. cf Educational Codeforces Round 49 D. Mouse Hunt

    原题: Medicine faculty of Berland State University has just finished their admission campaign. As usua ...

  7. cf Educational Codeforces Round 54 C. Meme Problem

    原题: C. Meme Problem time limit per test1 second memory limit per test256 megabytes inputstandard inp ...

  8. cf Educational Codeforces Round 134 E. Prefix Function Queries

    原题: You are given a string s, consisting of lowercase Latin letters. You are asked q queries about i ...

  9. cf Educational Codeforces Round 77 E. Tournament

    原题: E. Tournament time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

最新文章

  1. 深入浅出WPF——附加事件(Attached Event)
  2. ubuntu重启+sublime快捷键
  3. NoneBot2插件——打印系统状态
  4. SecureCRT:保存输出日志的方法
  5. Loader之二:CursorLoader基本实例
  6. 虚拟机安装Solaris10
  7. wordpress安装及使用
  8. DB2 sql复制error sqlcode2038
  9. weblogic详解
  10. 利用FrameLayout连接一组view实现流畅的左右滑动
  11. 【源码更新】活动报名登记预约问卷表单系统微信小程序支持导入导出自定义表单填报字段
  12. QCC3020/QCC3040 TWS耳机按键切换EQ,APP切换EQ功能实现
  13. rapidminer基础使用
  14. 微信小程序毕业设计 驾校考试小程序毕设开题报告
  15. qt 5.12.1 下载安装详细教程
  16. Total Phase Data Center介绍
  17. Linux操作文档——分析和排查系统故障(日志)
  18. [矩阵的QR分解系列四] QR(正交三角)分解
  19. 2022kali安装docker教程
  20. aspx repeater 用法_详解ASP.NET数据绑定操作中Repeater控件的用法

热门文章

  1. 小米13怎么设置每年自动生日提醒?
  2. 手机那点事儿——华为与苹果的差距
  3. VSCode快速初始化html文件
  4. PPT导出长图与SWF的输出
  5. “全民皆赚”是什么?
  6. LM2576HV调节负压输出拉低输入电压
  7. 几大网络电话网站用户体验评测
  8. 郁闷,车胎被人放了气
  9. OpenGL《圣诞节主题三位场景动画》
  10. Google Chrome 浏览器无法运行 Applet 的解决方法