CF985C Liebig's Barrels 贪心 第二十
2 seconds
256 megabytes
standard input
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.
The first line contains three space-separated integers n, k 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.
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.
4 2 1
2 2 1 2 3 2 2 3
7
2 1 0
10 10
20
1 2 1
5 2
2
3 2 1
1 2 3 4 5 6
0
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 贪心 第二十的更多相关文章
- codeforce 985C Liebig's Barrels(贪心+思维)
Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 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 ...
- Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码
Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码 文/玄魂 目录 Kali Linux Web 渗透测试视频教—第二十课-利用 ...
- Kali Linux Web 渗透测试— 第二十课-metasploit.meterpreter
Kali Linux Web 渗透测试— 第二十课-metasploit.meterpreter 原文链接:http://www.xuanhun521.com/Blog/7fc11b7a-b6cb-4 ...
- NeHe OpenGL教程 第二十九课:Blt函数
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第二十八课:贝塞尔曲面
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第二十六课:反射
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第二十五课:变形
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- NeHe OpenGL教程 第二十四课:扩展
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
随机推荐
- web图形验证码逻辑
逻辑:前端生成一个UUID以URL方式发送给后端,后端准备Redis数据库缓存数据,后端拿到UUID后,调用captcha.generate_captcha()生成图片和图片的标签,Redis数据库保 ...
- 6.源码分析---和dubbo相比SOFARPC是如何实现负载均衡的?
官方目前建议使用的负载均衡包括以下几种: random(随机算法) localPref(本地优先算法) roundRobin(轮询算法) consistentHash(一致性hash算法) 所以我们接 ...
- Leetcode的SQL题解:185. 部门工资前三高的员工
题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...
- react解析: render的FiberRoot(三)
react解析: render的FiberRoot(三) 感谢 yck: 剖析 React 源码解析,本篇文章是在读完他的文章的基础上,将他的文章进行拆解和加工,加入我自己的一下理解和例子,便于大家理 ...
- 最简单的DWR例子
什么是DWR? DWR是一个Open Source的 java项目.DWR可以让JavaScript调用运行在Web服务器里面的JAVA程序.简单一点或者专业一点就是Easy AJAX for JAV ...
- Opengl_入门学习分享和记录_00
2019.7.4 本着对游戏创作的热情,本人初步了解了一部分的unity引擎的使用,也学习了一点C#可是越学习unity我就反而对引擎内部感兴趣(不知道有没有一样的朋友=,=). 接着了解到了open ...
- 浅谈osi模型 三次握手 四次挥手 ddos攻击原理
C/S B/S 架构 C:client 端 B:browser 浏览器 S:server 端 C/S架构,基于客户端与服务端之间的通信 例如:QQ,抖音,快手,微信,支付宝等等 优点:个性化设置,响应 ...
- 微信小程序如何动态增删class类名达到切换tabel栏的效果
微信小程序和vue还是有点差别的,要想实现通过动态切换class来达到切换css的效果,请看代码: //wxml页面: <view class="tab"> <v ...
- Knative 基本功能深入剖析:Knative Serving 之服务路由管理
导读:本文主要围绕 Knative Service 域名展开,介绍了 Knative Service 的路由管理.文章首先介绍了如何修改默认主域名,紧接着深入一层介绍了如何添加自定义域名以及如何根据 ...
- 解决Mac下VSCode打开zsh乱码
1.乱码问题 iTerm2终端使用Zsh,并且配置Zsh主题,该主题主题需要安装字体来支持箭头效果,在iTerm2中设置这个字体,但是VSCode里这个箭头还是显示乱码. iTerm2展示如下: VS ...