Codeforces 985C (贪心)
传送门
题面:
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块木板组成,一个木桶的容水量为最短的木板的长度。先给你n*k块木板的长度,问你在两两木桶的容积不超过l的条件下,组成n个桶能获得的最大容积和是多少。
题目分析:
根据木桶原理我们可以得知,木桶的容积与长度很大的木板没有任何关系。因此我们先将所有木桶的长度排序。因为要组成n个木桶,(在最差的情况下,我们的策略是第1个木板跟第n*k个木板组合,第2个模板和第n*k-1个木板结合)。因此,首先我们先需要判断第n块木板和第一块木板的长度。倘若这两块木板的差值已经是大于l了,则表明必定无法构成n个桶,故输出0。
而倘若上述两块小于等于l,则此时需要采用贪心的策略。因为我们需要得到最大的容积,因此,我们需要尽可能贪心地将上界提高。因此我们需要在所有木板中找到第一个与第一块木板长度差大于l的木板,然后在计算容积的过程中,尽可能地让长度短的木板优先形成木桶,进而使得长度大的木板对结果有贡献,从而使得答案的结果最大。
代码:
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
ll a[maxn];
int main()
{ll n,k,l;cin>>n>>k>>l;for(int i=0;i<n*k;i++){cin>>a[i];}sort(a,a+n*k);if(a[n-1]-a[0]>l){puts("0");return 0;}ll rn;rn=lower_bound(a,a+n*k,a[0]+1+l)-a;//计算出第一个与第一块木板相差l的木板的编号ll cnt=rn-n;//计算出能够跳调多少块板ll ln=0;ll res=0;for(int i=0;i<n;i++){res+=a[ln];if(cnt>=k-1){//如果能跳过的板子正好可以组成一个木桶,则将左指针右移k-1cnt-=k-1,ln+=k-1;}else if(cnt){//否则直接将左指针移动cnt位,同时清零ln+=cnt;cnt=0;}ln++;}cout<<res<<endl;return 0;
}
转载于:https://www.cnblogs.com/Chen-Jr/p/11007284.html
Codeforces 985C (贪心)相关推荐
- 贪心 ---- Codeforces Global Round 8,B. Codeforces Subsequences[贪心,贪的乘法原理]
题目链接 给出字符串,统计子串(子串字母可以跳跃)是codeforces的数量. 本题要求,给出子串最少数量k,构造字符串s,要求字符串s包含的字母数量最少,输出这个最少的字符串s. 题目要求是至少有 ...
- CodeForces - 93B(贪心+vectorpairint,double +double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- Minimize the Permutation CodeForces - 1256(贪心)
题意: q次询问,每次询问给你长度为n的排列,然后你每次可以选择一个位置i和i+1的数字进行交换.但是每个位置只能交换一次,问你反转若干次后,这个排列最小是多少? 题目: You are given ...
- Minimizing Difference CodeForces - 1244E(贪心题)
题目 题意 官方题解: 百度翻译 思路 ac代码 题意 给出一列数,至多n个操作使其中的数+1或-1,要求得到最小的差值(最大值-最小值): You are given a sequence a1_{ ...
- Serval and Parenthesis Sequence CodeForces - 1153C 贪心
题意:给出一个由"(",")","?"三种字符构成的序列,让我们把其中的问号替换成左右括号,使得整个序列变成一个完整地括号序列,也就是括号匹 ...
- Codeforces #985C Liebig's Barrels
[问题描述] You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels ...
- CodeForces - 1089L 贪心
The kingdom of Lazyland is the home to nn idlers. These idlers are incredibly lazy and create many p ...
- Too Many Segments (easy version) CodeForces - 1249D1(贪心+差分)
题意 给多组线段,而每一个点的覆盖次数不超过K,每次可去除一个线段,问最少去多少线段以及线段的位置. The only difference between easy and hard version ...
- Codeforces 360E 贪心 最短路
题意及思路:https://blog.csdn.net/huanghongxun/article/details/49846927 在假设所有边都是最大值的情况下,如果第一个人能比第二个人先到,那就缩 ...
最新文章
- 川大网教计算机文化基础考试题,川大网教计算机文化基础第一次作业统一标准答案.doc...
- 中国反渗透膜产业竞争现状与投资战略决策报告2021-2027年版
- 世界四大重要检索系统简介
- ANSYS——模态提取方法简介
- 团队项目--设计类图
- 大数据之-Hadoop之HDFS_HDFS_副本数量设置---大数据之hadoop工作笔记0053
- 使用Dom4j进行XML解析
- 2015/7/24 (等待回调,结果是盘中回调,盘末拉升,错过了进仓机会吗?详情进入...
- 您未被授权查看该页 您不具备查看该目录或页面的权限,因为访问控制列表 (ACL) 对 Web 服务器上的该资源进行了配置
- HP惠普笔记本Microsoft ACPI-Compliant System未知设备的解决办法
- 关于流浪狗社会现状的调查报告
- php情书之笛卡尔的情书,笛卡尔情书的秘密——心形函数的作图
- MATLAB2014b画极坐标散点图
- 解决Linux命令行前出现base
- 第12周 项目4-输出从顶点u到v的所有简单路径
- 机器人工程→合适的规划←
- 王选计算机研究院,北京大学
- Java MD5加密解密
- Excel-用OFFSET和COUNTA实现动态增加下拉列表
- 光速入门消息队列Kafka
热门文章
- 编写python程序 兀 3_帮忙写一个简单的python程序π_π 要求就是图上的那样 其实挺简单的但我就是写不出来。拜托了,...
- uitextfield 键盘类型_iOS输入类型-文本字段(Text Fields) | 菜鸟教程
- symantec 操作 重叠vo_无关收购 谈谈赛门铁克的产品策略思路
- 悬剑武器库之5种工具学习(shiro检测插件、子域名、信息收集、暴力破解等)
- 《leetcode》reverse-integer
- 定位导致物化视图无法快速刷新的原因
- 数据挖掘之关联分析七(非频繁模式)
- 对数据科学家来说最重要的算法和统计模型
- C# 生成 MongoDB 中的 ObjectId
- WCF中的Dispose