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 ...
随机推荐
- eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined.
eclipse里error报错Target runtime com.genuitec.runtime.generic.jee60 is not defined. eclipse里error报错解决办法 ...
- Ubuntu系统下在github中新增库的方法
上一篇介绍了Ubuntu16.04系统下安装git的方法.本博客介绍怎么在github上怎么新建库. 如图 root@ranxf:/home/ranxf/learnGit/ranran_jiekou# ...
- 如何加固linux NFS 服务安全的方法
NFS(Network File System)是 FreeBSD 支持的一种文件系统,它允许网络中的计算机之间通过 TCP/IP 网络共享资源.不正确的配置和使用 NFS,会带来安全问题. 概述 N ...
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
- SNMP学习笔记之SNMPWALK 安装与使用详解
0x00 简介 snmpwalk是SNMP的一个工具,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户.通过snmpwalk也可以查看支 ...
- Removing bad blocks from the USB drive with fsck
An easy way to repair a flash drive, or any drive really, is to use the fsck tool. This tool is grea ...
- mysql主备部署[高可用]
配置方案 master:192.168.99.61 service-id:61 slave:192.168.99.62 service-id:62同步账号:sync 同步密码:sync 主:192 ...
- tensorflow reduction_indices理解
在tensorflow的使用中,经常会使用tf.reduce_mean,tf.reduce_sum等函数,在函数中,有一个reduction_indices参数,表示函数的处理维度,直接上图,一目了然 ...
- winform中splitter的用法
1.将winform分为三列 整个窗体分成可以自动调节的三列的做法:1.往窗体上丢两个panel控件其中:panelLeft——设置Dock属性为“left”,并调节好其宽度panelRight——设 ...
- Atcoder Educational DP Contest
前面简单一点的题直接过吧. A 暴力DP B 怎么还是暴力DP C 还是暴力DP D 直接背包 E 这个背包不太一样了,这里有一个技巧,就是因为价值很小,所以直接对价值背包,求出来达到某一个权值最小的 ...