CodeForces-985C Liebig's Barrels
CodeForces-985C Liebig's Barrels
Description
You have m?=?n·k wooden staves. The i-th stave has length \(a_i\). 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 \(v_j\) 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. \(|v_x?-?v_y|?≤?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 \((1?≤?n,?k?≤?10^5, 1?≤?n·k?≤?10^5, 0?≤?l?≤?10^9).\)
The second line contains m?=?n·k space-separated integers$ a_1,?a_2,?...,?a_m$ (1?≤?$a_i $?≤?109) — 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 $|v_x?-?v_y|?≤?l $for any 1?≤?x?≤?n and 1?≤?y?≤?n.
Examples
Input
4 2 1 2 2 1 2 3 2 2 3Output
7Input
2 1 0 10 10Output
20Input
1 2 1 5 2Output
2Input
3 2 1 1 2 3 4 5 6Output
0Note
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個木板,每個桶的容積由長度最短的木板決定,即為長度最短的木板的長度。n個桶要滿足任意兩個桶的容積差的絕對值不超過\(l\)。如果不可能輸出0,可能輸出最大的木桶容積總和
題解
這個題重點在于如何貪心是正確的,首先我們比較容易想到先對木板長度從小到大排序,從前往后掃描一遍,找到第一個比\(a_1+l\)大的\(a_{split}\),它包括它后面的木板都必須和前面的木板結(jié)合構(gòu)成木桶才能滿足條件,顯然當(dāng)\(a_{n}-a_1>l\)時不可能滿足條件,此時輸出0,其他時候均可滿足條件。
我們只需考慮怎樣讓容積最大即可。顯然\(a_i\)后面的木板長度大小都無所謂因為它們決定不了容積,那我們就考慮讓后面的木板選k-1個,前面的最靠近\(a_{split}\)的選1個來構(gòu)成木桶,這樣最大程度的讓前面大長度的木板決定容積,當(dāng)后面不夠k-1個的時候終止選擇。
然后我們從\(a_1\)到最后一個沒被選擇的木板循環(huán),選擇相鄰的k個作為一個木桶,這樣能保證小木板盡量與小木板結(jié)合從而使容積最大,最后不夠k個時即相當(dāng)于選擇了后面不夠k-1的木板和前面不夠k個木板結(jié)合成木桶,這樣即保證了容積最大。
AC代碼:
#include <bits/stdc++.h> #define maxn 100050 using namespace std; long long a[maxn]; int main() {int n, k, l;scanf("%d%d%d", &n, &k, &l);for (int i = 1; i <= n * k; i++) {scanf("%d", &a[i]);}sort(a + 1, a + n * k + 1);int minv = a[1];int split;for (split = 1; split <= n * k; split++) {if (a[split] > minv + l) {break;}}int cnt1 = split - 1, cnt2 = n * k - split + 1;int begin1 = 1;long long sum = 0;while (cnt2) {if (cnt2 < k - 1) {break;}else if (cnt2 >= k - 1) {sum += a[cnt1];cnt2 = cnt2 - k + 1;cnt1--;}if (cnt1 <= 0) break;}if (cnt1 <= 0 && cnt2 > 0) {cout << 0 << endl;return 0;}else {for (int i = 1; i <= cnt1; i += k) {sum += a[i];}cout << sum << endl;}return 0; }轉(zhuǎn)載于:https://www.cnblogs.com/artoriax/p/10357332.html
總結(jié)
以上是生活随笔為你收集整理的CodeForces-985C Liebig's Barrels的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 14-jQuery补充
- 下一篇: 新年記