C. Liebig's Barrels

时间:2022-10-11 23:33:48

You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble nbarrels 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.

C. Liebig's Barrels

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 and1 ≤ 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堆数,每堆数的最小值相差不能大于limit ,

求出n堆数最小值的和

upper_bound 返回的是第一个大于的数,减去1就是小于等于的数了

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
long long a[maxn];
int n, k, limit;
int main() {
scanf("%d%d%d", &n, &k, &limit);
for (int i = ; i < n * k ; i++)
scanf("%lld", &a[i]);
sort(a, a + n * k );
int temp = upper_bound(a, a + n * k, a[] + limit) - a;
long long ans = ;
int sum = n * k;
if (temp >= n) {
int temp1=temp;
while(sum > temp && sum - temp >= k - ) {
sum -= k - ;
ans += a[--temp1];
}
for (int i = ; i * k < temp1 ; i++)
ans += a[i * k];
}
printf("%lld\n", ans);
return ;
}

C. Liebig's Barrels的更多相关文章

  1. Codeforce Div-2 985 C&period; Liebig&&num;39&semi;s Barrels

    http://codeforces.com/contest/985/problem/C C. Liebig's Barrels time limit per test 2 seconds memory ...

  2. codeforce 985C Liebig&&num;39&semi;s Barrels(贪心&plus;思维)

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

  3. CF985C Liebig&&num;39&semi;s Barrels 贪心 第二十

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

  4. codeforces 985C Liebig&&num;39&semi;s Barrels

    题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少,或者 ...

  5. Liebig&&num;39&semi;s Barrels CodeForces - 985C &lpar;贪心&rpar;

    链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...

  6. codeforces 985C Liebig&&num;39&semi;s Barrels(贪心&rpar;

    题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...

  7. Educational Codeforces Round 44 &lpar;Rated for Div&period; 2&rpar;

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

  8. 【codeforces 768F】 Barrels and boxes

    http://codeforces.com/problemset/problem/768/F (题目链接) 题意 A,B两种物品可以装到栈中,每个栈只能存放一种物品,容量没有限制.现在讲所有栈排成一列 ...

  9. 【codeforces 768F】Barrels and boxes

    [题目链接]:http://codeforces.com/problemset/problem/768/F [题意] 让你把f个food和w个wine装在若干个栈里面; 每个栈只能装food或者是wi ...

随机推荐

  1. hibernate的基本xml文件配置

    需要导入基本的包hibernate下的bin下的required和同bin下optional里的c3p0包下的所有jar文件,当然要导入mysql的驱动包了.下面需要注意的是hibernate的版本就 ...

  2. ASP&period;NET MVC 利用IRouteHandler&comma; IHttpHandler实现图片防盗链

    你曾经注意过在你服务器请求日志中多了很多对图片资源的请求吗?这可能是有人在他们的网站中盗链了你的图片所致,这会占用你的服务器带宽.下面这种方法可以告诉你如何在ASP.NET MVC中实现一个自定义Ro ...

  3. UIView的生命周期

    一. 大体流程: (loadView/nib)文件来加载view到内存-->viewDidLoad函数进一步初始化这些view-->内存不足时, 调用viewDidUnload函数释放vi ...

  4. IP工具类——IpAddress&period;java

    根据IP地址获取详细的地域信息,也可通过 http://whois.pconline.com.cn/ 获取地址信息. 源码如下:(点击下载  IpAddress.java) import java.i ...

  5. Activity和Fragment生命周期变化

    情形一:启动应用加载Activity和Fragment Activity::onCreate Fragment::onAttach Fragment::onCreate Fragment::onCre ...

  6. 关于bootstrap--表单(按钮&lt&semi;button&gt&semi;效果、大小、禁用)

    1.各种标签实现按钮效果: <button class="btn btn-default" type="button">button标签按钮< ...

  7. day06&lowbar;JDBC学习笔记

    ============================================================ 一.JDBC概述 为什么要使用JDBC? JDBC:Java DataBase ...

  8. win10安装spacemacs

    参考: https://www.cnblogs.com/e190/p/10404927.html https://blog.csdn.net/u011729865/article/details/54 ...

  9. 原生JS制作简易Tabs组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. PHP Warning&colon; mysqli&lowbar;connect&lpar;&rpar;&colon; The server requested authentication method unknown to the client &lbrack;caching&lowbar;sha2&lowbar;password&rsqb; in &sol;usr&sol;local&sol;php&sol;CreateDB&period;php on line 5

    原因:php还不支持mysql8.0最新的密码加密方式 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ' ...