CodeForces985C-Liebig's Barrels
描述
题解
二分加贪心。先确保前 ii 桶可以分配为相邻的 kk 个,并且保证 a[i∗k+j]−a[1]<=la[i∗k+j]−a[1]<=l,这样就能保证所有的差不大于 ll,如果不能保证这个条件,说明此时已经无法分配相邻的 kk 个了,而需要将剩下的没有组装的桶先分配一个满足条件的最大的,然后剩下的再分给这些没有组装完成的桶(当然这部分不用代码写出来)。
代码
AC:
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int MAXN = 1e5 + 10;
int n, k, l;
int a[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> l;
int tmp = n * k;
for (int i=1;i<=tmp;i++) cin>>a[i];
sort(a+1,a+tmp+1);
if (a[n]-a[1]>l) cout<<"0"<<endl;
else
{
long long ans=0;
for (int i=1,j=n-1;i<=n;i++,j--)
{
if (a[i*k+j]-a[1]<=l) ans+=a[(i-1)*k+1];
else
{
int p=upper_bound(a+(i-1)*k+1,a+tmp+1,a[1]+l)-a-1;
ans+=a[(i-1)*k+1];
while(j--) ans+=a[p--];
break;
}
}
cout<<ans<<endl;
}
return 0;
}
CodeForces985C-Liebig's Barrels的更多相关文章
- 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 ...
- codeforce 985C Liebig's Barrels(贪心+思维)
Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF985C Liebig's Barrels 贪心 第二十
Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- C. Liebig's Barrels
You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble nbarrels consisti ...
- codeforces 985C Liebig's Barrels
题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少,或者 ...
- Liebig's Barrels CodeForces - 985C (贪心)
链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...
- codeforces 985C Liebig's Barrels(贪心)
题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...
- Educational Codeforces Round 44 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
- 【codeforces 768F】 Barrels and boxes
http://codeforces.com/problemset/problem/768/F (题目链接) 题意 A,B两种物品可以装到栈中,每个栈只能存放一种物品,容量没有限制.现在讲所有栈排成一列 ...
- 【codeforces 768F】Barrels and boxes
[题目链接]:http://codeforces.com/problemset/problem/768/F [题意] 让你把f个food和w个wine装在若干个栈里面; 每个栈只能装food或者是wi ...
随机推荐
- JS 暴虐算法查找
@dd|ad|fds|d@dd|ad|fds|d@dd|ad|fds|d@ var e = []; window.onload = function () { var ...
- .NET Core3.0 EF 连接 MySql
一:创建项目 添加 csproj (或者直接NuGet 引用) <ItemGroup> <PackageReference Include="Microsoft.Entit ...
- window,sts安装python
1.先在python官网下载最新的python安装,安装的时候勾上所有选项 官网:https://www.python.org/downloads/ 2.在sts里安装pyDev插件,我当时直接sea ...
- C#查看已下载文件大小和扩展名
FileInfo fi = new FileInfo(fullfile);//fullfile文件路径 ong Size = fi.Length;//查看已下载文件的大小 C# 获取文件名及扩展名 s ...
- LeetCode51 N皇后——经典dfs+回溯(三段式解法)
代码如下: class Solution { public: // record[row] 该行对应的列 vector<vector<string> > ans; // 结果集 ...
- devicemapper存储驱动下镜像的存储
docker配置devicemapper存储驱动 #查看当前使用的存储驱动,默认为overlay docker info | grep -i storage #停止dockersystemctl st ...
- C# - SPC(Statistical Process Control)系统 - 6西格玛数据决策和Chart模块的开发与实现
Statistical Process Control 简介 统计过程控制(Statistical Process Control)是一种借助数理统计方法的过程控制工具.它对生产过程进行分析评价,根据 ...
- vue中router跳转本页刷新
问题: 导航栏的地址发生改变但是页面却不刷新 (用vue-router路由到当前页面,页面是不进行刷新的)解决: 1.); 2.location.reload() ...
- Docker从门外到入门使用
取材 第一本Docker书.原作者:James Turnbull 安装 这里只说明Windows环境的安装(Windows7以上) 使用Docker Toolbox工具即可:http://mirror ...
- Stream系列(九)Collector方法使用
toMap toList toCollection joining 视频讲解:https://www.bilibili.com/video/av77800638/ EmployeeTestCase.j ...