Liebig's Barrels CodeForces - 985C (贪心)
大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和
假设最短板长$m$, 显然最后桶的体积都在$[m,m+l]$范围内, 就是说需保证每个桶都至少有一块板在$[m,m+l]$范围.
考虑贪心逐步调整, 假设$m+l$足够多的话, 可以先尽量连续得选最短的边做桶, 最后将$m+l$分给剩余桶即为最大容积.
如果不够多的话, 就要选出$m+l-1$分给剩余桶, 还不够就选择$m+l-2$, 直到所有桶都分到为止.
离散化后预处理一下前缀和可以达到复杂度$O(nlogk+nlogn)$
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll; const int N = 4e5+10, INF = 0x3f3f3f3f;
int n, k, l;
int a[N], b[N], cnt[N], s[N]; //用a记录离散化后的值, b是离散化辅助数组
//cnt记录每个离散化后的值的出现次数, s统计cnt的前缀和 ll solve(int n, int x) {
//当前最大的为x, 还要制作n个桶的最大容积和
int t = (s[x-1]+k-1)/k;
if (t+cnt[x]>=n) {
//x数量足够, 直接贪心分配
ll ans = 0;
REP(i,1,n) {
if (a[(i-1)*k+1]<x) ans += b[a[(i-1)*k+1]];
else ans += b[x];
}
return ans;
}
//x不够的话, 先尽量把x分完, 求出第一个比x小的数, 递归计算
auto p = lower_bound(a+1,a+1+n*k,x);
return solve(n-cnt[x],*(--p))+(ll)cnt[x]*b[x];
} int main() {
scanf("%d%d%d", &n, &k, &l);
REP(i,1,n*k) scanf("%d", a+i);
sort(a+1,a+1+n*k);
ll mx = a[1]+l;
if (a[n]>mx) return puts("0"),0;
REP(i,1,n*k) b[i]=a[i];
*b = unique(b+1,b+1+n*k)-b-1;
REP(i,1,n*k) a[i]=lower_bound(b+1,b+1+*b,a[i])-b;
REP(i,1,n*k) ++cnt[a[i]];
REP(i,1,*b) s[i]=s[i-1]+cnt[i];
int p = upper_bound(b+1,b+1+*b,mx)-b-1;
printf("%lld\n", solve(n,p));
}
看了下别人题解, 发现没必要这么麻烦, 上述分析已经知道, 最优结构一定是先连续选一段最小的, 再将$[m,m+l]$中剩余的逐个分给剩余桶
假设连续部分做了x个桶, 剩余部分y个桶, 就有$xk+y<=s[m+l],x+y=n$
解出x的最大值, 就可以直接得到最优解的结构了
Liebig's Barrels CodeForces - 985C (贪心)的更多相关文章
- 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 ...
- 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 ...
- codeforces 985C Liebig's Barrels(贪心)
题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...
- codeforces 985C Liebig's Barrels
题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少,或者 ...
- 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 - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
随机推荐
- 基于webview的Hybrid app和React Native及html5
基于webview的Hybrid app和React Native及html5 React Native 结合了 Web 应用和 Native 应用的优势,可以使用 JavaScript 来开发 iO ...
- php new stdClass array 实例代码
php new stdClass array 实例代码 $searchResults = array ();// //$obj = array ("rs"=>array(), ...
- MySQL性能分析和优化
1. EXPLAIN 优化你的 SELECT 查询 2. 当只要一行数据时使用 LIMIT 1 3. 为搜索字段建索引 like %最好放右边 4. 尽可能的使用 NOT NULL 5. 在Join表 ...
- Linux中Postfix虚拟用户及虚拟域(六)
Postfix基于虚拟用户虚拟域的邮件架构 上图是一个几乎完整的邮件系统架构图,这里基于Mysql数据库进行用户认证,不管是Postfix.Dovecot.webmail都需要去Mysql数据库中进行 ...
- MySQL备份与恢复-innobackupex
:上一片myloder搞崩溃,为什么百度的博文都是抄袭一模一样的,哎烦! 这一片文章我们来介绍物理备份工具xtracebackup! 首先是安装可以percona官网下载安装,下载rpm包直接yum安 ...
- P2158/bzoj2190 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 欧拉函数 计算下三角的点数再*2+1 观察斜率,自行体会 #include<iostream> #include<cstdio> #in ...
- 20165310 Java实验四 《Android程序设计》
20165310 实验四 <Android程序设计> 第24章:初识Android 任务一:改写res目录中的内容,Hello World后要显示自己的学号,自己学号前后一名同学的学号 首 ...
- 不到 200 行代码,教你如何用 Keras 搭建生成对抗网络(GAN)【转】
本文转载自:https://www.leiphone.com/news/201703/Y5vnDSV9uIJIQzQm.html 生成对抗网络(Generative Adversarial Netwo ...
- No compatible targets were found Do you wish to a add new Android Virtual Device ?
运行一个Android小程序时提示: No compatible targets were found Do you wish to a add new Android Virtual Device ...
- P3466 [POI2008]KLO-Building blocks
目录 题目 思路 错误 代码 题目 luogu csdn好像限制了展开博客次数,真的好xx 思路 显然一段区间内的值一定是他的中位数 少一点比多一点好 然后就可以枚举区间了 区间答案为 val[mid ...