codeforce 985C Liebig's Barrels(贪心+思维)

Liebig‘s Barrels

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nk and l (1?≤?n,?k?≤?105, 1?≤?n·k?≤?105, 0?≤?l?≤?109).

The second line contains m?=?n·k space-separated integers a1,?a2,?...,?am (1?≤?ai?≤?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 |vx?-?vy|?≤?l for any 1?≤?x?≤?n and 1?≤?y?≤?n.

Examples

input

Copy

4 2 12 2 1 2 3 2 2 3

output

Copy

7

input

Copy

2 1 010 10

output

Copy

20

input

Copy

1 2 15 2

output

Copy

2

input

Copy

3 2 11 2 3 4 5 6

output

Copy

0

Note

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  l    你要做n个桶,每个桶需要k个木板,用木板拼好的桶相互之间体积的差距<=l,桶的体积大小就是最短的那根木板的长度大小。

第二行 共n*k个数,分别表示n*k个木板的长度。

分析:

先对边排个序

不存在的情况,就是a[n]-a[1]>l,那就是不存在,因为要是差距尽可能小,前n小的都分别作为n个桶的一块木板,那么这之中最大的差距就是a[n]-a[1],要是a[n]-a[1]都满足条件(<=l)了,那就满足条件了。

其次,要使体积和最大输出体积和,我毛想想觉得s=a[1]+……a[n],结果WA了,引起了我的深思。

因为:

eg:4    3    17

1   2   3   5   9   13  18  21  22  23  25 26

它可以这样组3组:

18  25  26

13   22  23

1     2     3

5     9   21

这样体积为1+5+13+18=37,不是简单地1 +2 +3 +5=11

所以我的思路:先要找到最大的满足条件的数,可以用二分找更快,在这组样例中,是18,它-a[1]<=l,

那么从最后开始去k-1个和18拼,s+=18,再下一个数13(25  26),再从最后找k-1个数(22  23),

再下一个数9,发现再k-1个数不够了,那就从头开始找了,(1   2   3)一组,在去(5   9  13)时,发现13

已经被取走,那就s+=5就可以了。

#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#define ll long long
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
ll a[100005];
bool cmp(ll a,ll b)
{
    return a<b;
}
int main()
{
    ll n,k,l;
    scanf("%I64d%I64d%I64d",&n,&k,&l);
    for(ll i=1;i<=n*k;i++)
    {
        scanf("%I64d",&a[i]);
    }
    sort(a+1,a+1+n*k,cmp);
    if(a[n]-a[1]>l)
    {
        printf("0");
    }
    else
    {
        ll s=0;
        ll p=-1;
        for(ll i=n*k;i>=1;i--)
        {
            if(a[i]-a[1]<=l)
            {
                p=i;//找到标准数,最大的满足条件的数
                break;
            }
        }
        s=0;
        int num=0;//记录从标准数向前取了多少
        int j=p;
        for(ll i=n*k;i-(k-1)>p;i=i-(k-1))//先从后往前取
        {
            s+=a[j--];
            num++;
        }
        for(ll i=1;i<p-num+1;i=i+k)//在从前往后取
        {
            s+=a[i];
        }
        printf("%I64d",s);
    }
    return 0;
}

codeforce 985C Liebig's Barrels(贪心+思维)

原文地址:https://www.cnblogs.com/caiyishuai/p/9080572.html

时间: 2024-08-01 07:57:25

codeforce 985C Liebig's Barrels(贪心+思维)的相关文章

codeforces 985C Liebig&#39;s Barrels

题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少,或者说明不可能做出这样的n个木桶. 思路: 贪心 要满足|v1-v2| <= d,那么就要满足最大的木桶容量和最小的木桶容量的差小于等于d. 所以先把木板长度排序,如果a[0] 到 a[0] + d这个范围内有大于等于n个木板,那么就存在合理的分配方案,因为可以把至少n个木板作为最短的木板. 然后就计算最大

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

hdu 4898 LCP+贪心思维

题意:将一个字符串切成k块,使得字典序最大的那块最小. ORZ  WJMZBMR,几行题解读了一天才懂. 快速比较两个子串的大小可以利用LCP(最长公共前缀),比较公共前缀的下一个字符的大小就够了. 利用这种思想,首先我们可以预处理所有子串的LCP(后缀数组+记录 O(2nlog(2n))+O(n*n),dp(O(4*n*n))) 然后将这些子串利用LCP按照字典序排序,开始二分答案. 二分的答案就是这K个块字典序的上限.假设以i作为起点,由于字典序上限已知,所以我们可以立刻求出i点最远能选到哪

Codeforces Round #546 (Div. 2) D 贪心 + 思维

https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里 题解 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u 假设前面有一个不能和u换位置的集合S,

hdu 4550 贪心 思维题 不错

http://acm.hdu.edu.cn/showproblem.php?pid=4550 想了挺久,然后各种分类 终于AC,如果是现场,对自己没信心的话,估计还是要WA,,,,,,然后搜题解,发现人家都认为是简单题,看来我还是太弱了,牡丹江没有做出来K看来还是自己贪心和思维有问题 d是一个Deque 最朴素的算法是,如果当前的数<=d.front(),那么插入队列的前面,否则插入队列后面,但是有零所以需要单独处理,还是自己多举例找规律 我的策略: 1.记录0的个数zero,最小非零的数的个数

ZOJ 3829 贪心 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题,自己智商不够,不敢搞,想着队友智商好,他们搞吧,但是没出来这题...... 以后任何时候,都自信点....该想的还是好好自己想,这类题感觉就是先去找性质,然后一点点找规律,如果必要的话,自己提出一点猜想,然后如果自己举不出来反例,就暂时认为是正确的 下午搞了一下午,发现还是悲剧,晚上参考了两个题解 http://blog.csdn.

hdu 4803 贪心/思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上键,最后下键补全,可是例子都过不了..... 题解參考http://www.cnblogs.com/xuesu/p/3967704.html http://www.cnblogs.com/Canon-CSU/p/3451784.html http://blog.csdn.net/keshuai199

贪心思维 专题记录 2017-7-21

A.UVa 10382 - Watering Grass 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数. 思路 :转化一下 将二维降成一维      d = sqrt(1.0*r*r-w*w/4.0) 接着就是区间覆盖问题了 #include <bits/stdc++.h> using namespace std; const int maxn = 10000+10;

Balanced Ternary String CodeForces - 1102D (贪心+思维)

You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain