CF985C Liebig's Barrels 贪心 第二十

时间:2023-02-08 19:58:12
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.

CF985C 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 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个木板的长度。

开始自己的思路是对的,但是中间的具体细节写错了,wa了好几发

然后看的别人的博客才写出来的

http://www.mamicode.com/info-detail-2309549.html

分析:

先对边排个序

不存在的情况,就是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 <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn];
int main(){
std::ios::sync_with_stdio(false);
ll n, k, m;
while( cin >> n >> k >> m ) {
for( ll i = ; i <= n*k; i ++ ) {
cin >> a[i];
}
sort( a + , a + n*k + );
ll num = -;
for( ll i = n*k; i >= n; i -- ) {
if( a[i] - a[] <= m ) {
num = i;
break;
}
}
if( num == - ) {
cout << << endl;
continue;
}
ll sum = , j = num, t = ;
for( ll i = n*k ; i - ( k- ) > num; i = i - ( k- ) ) {
sum += a[j--];
t ++;
}
for( ll i = ; i < num - t + ; i += k ) {
sum += a[i];
}
cout << sum << endl;
}
return ;
}

CF985C Liebig's Barrels 贪心 第二十的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码

    Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码 文/玄魂 目录 Kali Linux Web 渗透测试视频教—第二十课-利用 ...

  4. Kali Linux Web 渗透测试— 第二十课-metasploit&period;meterpreter

    Kali Linux Web 渗透测试— 第二十课-metasploit.meterpreter 原文链接:http://www.xuanhun521.com/Blog/7fc11b7a-b6cb-4 ...

  5. NeHe OpenGL教程 第二十九课:Blt函数

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  6. NeHe OpenGL教程 第二十八课:贝塞尔曲面

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  7. NeHe OpenGL教程 第二十六课:反射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. NeHe OpenGL教程 第二十五课:变形

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  9. NeHe OpenGL教程 第二十四课:扩展

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

随机推荐

  1. Oracle数据库该如何着手优化一个SQL

    这是个终极问题,因为优化本身的复杂性实在是难以总结的,很多时候优化的方法并不是用到了什么高深莫测的技术,而只是一个思想意识层面的差异,而这些都很可能连带导致性能表现上的巨大差异. 所以有时候我们应该先 ...

  2. Android 谈谈封装那些事 --BaseActivity 和 BaseFragment&lpar;二&rpar;

      1.前言 昨天谈了BaseActivity的封装,Android谈谈封装那些事--BaseActivity和BaseFragment(一)有很多小伙伴提了很多建议,比如: 通用标题栏可以自定义Vi ...

  3. 引用项目外dll时不显示注释的解决方案

    在引用项目外的dll时,显示类库中的注释可按以下步骤: 方法或变量用summary添加注释,如:         /// <summary>发送post请求         /// &lt ...

  4. linux下安装jdk&plus;tomcat&plus;eclipse&plus;mysql

    我的环境:主机是win7的,虚拟机是VWare  Workstation 6.0   ,linux系统为Red Hat Enterprise  Linux 5 64位 各软件版本:jdk是jdk-6u ...

  5. 程序进入 EXPORT App&lowbar;Fault&lowbar;ISR的原因及措施:

    最近再UCOSIII+LPC1768上移植modbus,在定时器初始化部分竟然跑飞进入 EXPORT  App_Fault_ISR,查资料.逛论坛.问大牛都没有解决,最后发现竟然是犹豫一个低级失误引起 ...

  6. sql中的or的用法说明

    数据库里a  c 0  10  20  31  41  51  6这样执行完这语句之后查出来有几条语句? 答案是3条 而我想要的是a为0或1并且c为3的数据显示 应该只有1条的 所以应该这样写 Sql ...

  7. iOS-事务相关

    事务管理 事务(Transaction):1.构成单一逻辑工作单元的操作集合DBMS中的用户程序DBMS外的可执行程序对数据库的读/写操作序列2.读从数据库中读取数据,首先从磁盘中读到内存(Buffe ...

  8. css08盒子模型

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

  9. sql参数化查询避免注入漏洞的原因探析

    网上其他同学的都说是重用执行计划,将用户输入的作为文本查询,到底如何实现,我用下面三行代码来解析一下. DECLARE @test NVARCHAR() SET @test=' or 1='1 SEL ...

  10. for in 使用

    // JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写. var json = { "id": 1, "n ...