codeforces 985C Liebig's Barrels

Keywords: less

Title:

There are n * k boards, each bucket is composed of K boards, and the capacity of each bucket is defined as the length of the shortest board.

The capacity of any two wooden barrels v1, v2 meets the requirement of | v1-v2 | < D.

Ask the maximum sum of N buckets, or explain that it is impossible to make such n buckets.

Ideas:

greedy

In order to meet the requirement of | V1-V2 | < D, the difference between the maximum and the minimum bucket capacity should be less than or equal to d.

Therefore, the length of boards should be sorted first. If there are more than or equal to n boards in the range of a[0] to a[0] + d, then there is a reasonable allocation scheme, because at least n boards can be regarded as the shortest boards.

Then calculate the maximum sum. If there are just n boards in the range a[0] to a[0] + d, then the maximum sum is the sum of the N board lengths;

If it is greater than n, then we should consider to make the minimum board length of each barrel as large as possible, that is, let each minimum board choose the number behind the array as much as possible.

Since one board can control k - 1 board, the minimum length of the next bucket can start from a[k], so that the minimum can be as large as possible.

If there are sum boards in the range a[0] to a[0] + d, then the redundant boards are res = sum - n.

The number of intervals is c = res / (k-1), let r = res% (k-1),

When r = 0, then there are c complete intervals. The length of the first c buckets is 0*k, 1*k, 2 * k... (c-1) * k, and the subscript of the capacity of the last n - c buckets is from c * k to sum-1;

When r! = 0, there are c complete intervals and an incomplete interval. The capacity of the first c + 1 barrel is 0 * k, 1 * k, 2 * K.. c * k, and the subscript of the capacity of the last n - c - 1 barrel is from c * k + r + 1 to sum - 1.

code:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int N = 1e5 + 10;
 6 long long a[N];
 7 int main()
 8 {
 9     int n,k;
10     long long l;
11     scanf("%d%d%lld",&n,&k,&l);
12     for (int i = 0;i < n * k;i++) scanf("%lld",&a[i]);
13     sort(a,a+n*k);
14     //printf("%lld\n",a[0] + l);
15     int pos = upper_bound(a,a+n*k,a[0] + l) - a;
16     pos--;
17     //printf("%d\n",pos);
18     if (pos < n - 1) puts("0");
19     else
20     {
21         long long ans = 0;
22         int sum = pos + 1;
23         if (sum == n)
24         {
25             for (int i = 0;i < n;i++) ans += a[i];
26         }
27         else
28         {
29             if (k == 1)
30             {
31                 for (int i = 0;i < n;i++) ans += a[i];
32             }
33             else
34             {
35                 int c = (sum - n) / (k - 1);
36                 int r = (sum - n) % (k - 1);
37                 if (r)
38                 {
39                     for (int i = 0;i <= c;i++)
40                     {
41                         ans += a[i*k];
42                     }
43                     n -= c + 1;
44                     for (int i = k * c + r + 1;i <= pos;i++)
45                     {
46                         if (n == 0) break;
47                         ans += a[i];
48                         n--;
49                     }
50                 }
51                 else
52                 {
53                     for (int i = 0;i < c;i++)
54                     {
55                         ans += a[i*k];
56                     }
57                     n -= c;
58                     for (int i = k * c;i <= pos;i++)
59                     {
60                         if (n == 0) break;
61                         ans += a[i];
62                         n--;
63                     }
64                 }
65             }
66         }
67         printf("%lld\n",ans);
68     }
69     return 0;
70 }

Posted by Jeroen Oosterlaar on Mon, 20 Apr 2020 10:00:54 -0700