Codeforces-985C - Liebig's Barrels - 贪心

29 篇文章 1 订阅

题解链接

https://www.lucien.ink/archives/234/


题目链接

http://codeforces.com/contest/985/problem/C


题目

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.

Input

The first line contains three space-separated integers n, k and l (1n,k105,1n·k105,0l109) ( 1   ≤   n ,   k   ≤   10 5 , 1   ≤   n · k   ≤   10 5 , 0   ≤   l   ≤   10 9 ) .

The second line contains m = n·k space-separated integers a1,a2,...,am a 1 ,   a 2 ,   . . . ,   a m (1ai109) ( 1   ≤   a i   ≤   10 9 ) — lengths of staves.

Output

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 |vxvy|l | v x   −   v y |   ≤   l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.


题意

  需要造n个桶,每个桶需要k个木板,任意两个桶之间容积(一个桶中最短的木板的高度)的差距不能超过l,然后给你n * k个木板,第 i i 个木板的长度为ai,问能否造出符合条件的n个桶,如果能的话问这n个桶的容积之和最大是多少,如果不能的话就输出0


思路

  首先对所有木板sort一下,由于短板效应,我们至少可以让前n个木板恰好为n个桶的容积,判断一下 ana1l a n − a 1 ≤ l 是否成立,不成立的话一定是 0 0 ,因为前n个的木板的差距一定是最小的。

  我们发现,如果我们让每连续的k个木板形成一个桶的话,那么这些桶的容积将会是a1ak+1a2k+1,中间一些较小的木板都会被更短但是又必须得取的木板给包含,这样一定是最优的。

  首先我们找到一个最大的upper,使得 auppera1l  (uppern·k) a u p p e r − a 1 ≤ l     ( u p p e r ≤ n · k ) 成立,这样在 [1,upper] [ 1 , u p p e r ] 的范围内以任意n个木板的高度为桶的容积都是合法的。假设此时我们选了 x x 个连续的k来造桶,还需要再造y个桶才能造够n个桶,则有:

x·k+yupper x · k + y ≤ u p p e r
x+y=n x + y = n

  移项、代入、化简:

xuppernk1 x ≤ u p p e r − n k − 1

  也就是说:得到 x x 后,y也就相应出来了,对于剩下的 upperx·k u p p e r − x · k 个木板,显然取后 y1 y − 1 个和第 x·k+1 x · k + 1 个木板作为剩下的 y y <script type="math/tex" id="MathJax-Element-22">y</script>个桶的容积是最优的。


实现

#include <bits/stdc++.h>
typedef long long ll;
const int maxn = int(1e5) + 7;
ll n, k, l, m, a[maxn], ans, upper;
int main() {
    scanf("%lld%lld%lld", &n, &k, &l);
    m = n * k;
    for (ll i = 1; i <= m; i++) scanf("%lld", a + i);
    std::sort(a + 1, a + 1 + m);
    if (a[n] - a[1] > l) return 0 * puts("0");
    if (k == 1) for (ll i = 1; i <= n; i++) ans += a[i];
    else {
        while (a[upper] - a[1] <= l && upper <= m) upper++;
        upper--;
        ll x = (upper - n) / (k - 1);
        for (ll i = 1, cur = 1; i <= x; i++, cur += k) ans += a[cur];
        for (ll i = 1; i < n - x; i++) ans += a[upper--];
        ans += a[x * k + 1];
    }
    printf("%lld\n", ans);
    return 0;
}
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值