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
  1. 4 2 1
    2 2 1 2 3 2 2 3
output

Copy
  1. 7
input

Copy
  1. 2 1 0
    10 10
output

Copy
  1. 20
input

Copy
  1. 1 2 1
    5 2
output

Copy
  1. 2
input

Copy
  1. 3 2 1
    1 2 3 4 5 6
output

Copy
  1. 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    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就可以了。

  1. #include <iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<deque>
  8. #include<vector>
  9. #define ll long long
  10. #define inf 0x3f3f3f3f
  11. #define mod 1000000007;
  12. using namespace std;
  13. ll a[];
  14. bool cmp(ll a,ll b)
  15. {
  16. return a<b;
  17. }
  18. int main()
  19. {
  20. ll n,k,l;
  21. scanf("%I64d%I64d%I64d",&n,&k,&l);
  22. for(ll i=;i<=n*k;i++)
  23. {
  24. scanf("%I64d",&a[i]);
  25. }
  26. sort(a+,a++n*k,cmp);
  27. if(a[n]-a[]>l)
  28. {
  29. printf("");
  30. }
  31. else
  32. {
  33. ll s=;
  34. ll p=-;
  35. for(ll i=n*k;i>=;i--)
  36. {
  37. if(a[i]-a[]<=l)
  38. {
  39. p=i;//找到标准数,最大的满足条件的数
  40. break;
  41. }
  42. }
  43. s=;
  44. int num=;//记录从标准数向前取了多少
  45. int j=p;
  46. for(ll i=n*k;i-(k-)>p;i=i-(k-))//先从后往前取
  47. {
  48. s+=a[j--];
  49. num++;
  50. }
  51. for(ll i=;i<p-num+;i=i+k)//在从前往后取
  52. {
  53. s+=a[i];
  54. }
  55. printf("%I64d",s);
  56. }
  57. return ;
  58. }

codeforce 985C Liebig's Barrels(贪心+思维)的更多相关文章

  1. CF985C Liebig's Barrels 贪心 第二十

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

  2. codeforces 985C Liebig's Barrels(贪心)

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

  3. codeforces 985C Liebig's Barrels

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

  4. Codeforce Div-2 985 C. Liebig's Barrels

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

  5. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  6. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  7. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  8. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  9. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

随机推荐

  1. scala学习手记20 - 方法返回类型推断

    除了推演变量的类型,scala也会推演方法的返回类型.不过这里有一处需要注意:方法返回类型的推演依赖于方法的定义方式.如果用等号"="定义方法,scala就会推演方法返回类型:否则 ...

  2. Samba的安装配置使用

    ////////////////////////////////Samba//////////////////////////////////////////// 小常识:在同一局域网中,两个系统的工 ...

  3. ES使用中遇到的多种坑,以及解决方案

    1.查询不到导致404报错 在使用get或者search进行查询获取文档的时候,如果没有结果会抛出404的异常. 我们当然不希望抛出异常,这时候就要使用ignore这个参数来忽略报错,ignore可以 ...

  4. mysql 出现Host 'localhost' is not allowed to connect to this MySQL server 错误

    MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server 修改mysql的root密码后,出现Host 'loc ...

  5. spring mvc: 静态资源/文件配置

    静态文件不用再放web-info 下面了,放在webapp/ 下面就行了(静态文件放web-inf下你在jsp都无法引用~  注意一下所有js.css包括报表文件~ 配置文件等等等~  不要放在web ...

  6. mysql中开启慢查询日志

    开启慢查询日志,需要在配置文件my.ini中配置. long_query_time = 1 #设置慢查询时间,配置是下划线log-slow-queries = d:\mysql5\logs\mysql ...

  7. Android开发调试中遇到的Waiting for HOME解决方案

    显示问题如图: 基本解决思路:在AVD里面单独启动运行一次模拟器,然后尝试运行程序,基本会解决这个问题.

  8. ZOJ 3211 Dream City(线性DP)

    Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...

  9. C++轮子队 敏捷冲刺

    团队Github地址:https://github.com/Pryriat/2048.git 敏捷开发——第1天 Alpha阶段第1次Scrum Meeting 敏捷开发起始时间 2018/10/27 ...

  10. NSPredicate(正则表达式)

    1. 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.通常被用来检索.替换那些符合某个模式的文本. 2. iOS中正则使用 有三种(NSPredicate, rangeOfStri ...