You are building advanced chips for machines. Making the chips is easy, but the power supply turns out to be an issue since the available batteries have varied power outputs.
Consider the problem of nn machines, each with two chips, where each chip is powered by kk batteries. Surprisingly, it does not matter how much power each chip gets, but a machine works best when its two chips have power outputs as close as possible. The power output of a chip is simply the smallest power output of its kk batteries.
You have a stockpile of 2nk2nk batteries that you want to assign to the chips. It might not be possible to allocate the batteries so that in every machine both chips have equal power outputs, but you want to allocate them so that the differences are as small as possible. To be precise, you want to tell your customers that in all machines the difference of power outputs of the two chips is at most dd, and you want to make dd as small as possible. To do this you must determine an optimal allocation of the batteries to the machines.
Consider Sample Input 1. There are 2 machines, each requiring 3 batteries per chip, and a supply of batteries with power outputs 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. You can, for instance, assign the batteries with power outputs 1, 3, 5 to one chip, those with power 2, 4, 12 to the other chip of the same machine, those with power 6, 8, 9 to the third chip, and those with power 7, 10, 11 to the fourth. The power outputs of the chips are 1, 2, 6, and 7, respectively, and the difference between power outputs is 1 in both machines. Note that there are many other ways to achieve this result.
The input consists of a single test case. A test case consists of two lines. The first line contains two positive integers: the number of machines nn and the number of batteries per chip kk (2nk≤1062nk≤106). The second line contains 2nk2nk integers pipi specifying the power outputs of the batteries (1≤pi≤109 1≤pi≤109).
Display the smallest number dd such that you can allocate the batteries so that the difference of power outputs of the two chips in each machine is at most dd.
有n台机器,每台机器里有两块芯片,每块芯片需要k个电池,每个电池有一个能量值,我们定义每块芯片的能量为该芯片对应的k个电池中能量的最小值,另外我们希望每台机器中的两块芯片的能量之差尽量小,我们定义每台机器中的能量之差为。现在要求输出。数据范围
首先,我们对2nk个电池按照能量由小到大排序
现在,我们的思考方向是这样的:每次我们挑出两个电池分别作为第i台机器中的两个芯片中能量最小的电池,我们重复n次,挑出所有机器对应的这两块电池,那么最终答案就是。那么怎么每次挑出这样的两块电池就成了问题的关键。
我们观察,可以发现问题存在这样的性质:
1、排序后的第一块电池一定会在第一次被挑走。这是因为第一块电池能量最小,无论放到哪台机器的那块芯片中,都自然是能量最小的那个。
2、每次挑的一定是排序后相邻的两块电池。这是因为相邻的电池一定比不相邻电池之间的能量之差要小,能挑相邻的何必要挑不相邻的。
依照这样的规律,考虑从前向后直接贪心,会发现当我们当前在考虑相邻两块电池能否作为时,是无法直接决定的,因为我们不知道为机器i取这两个电池是不是比取后面的某相邻两个电池的策略要更优。这样,我们自然会想到使用二分。
我们每次二分一个x,判断x是否合法的策略如下:
我们用cnt记已经为多少台机器的两块芯片取了两个能量最小的电池,初始时cnt=0。
我们从前到后枚举相邻的两个电池,如果,那么他们可以作为某台机器的两块电池,我们令cnt=cnt+1。
维护了这些值,那么判断的具体标准是什么呢?观察可以发现,前两块电池中至少取一对(性质1);在前2k+2块电池中,我们至少取两对;在前4k+2块电池中,我们至少取三对...这个原因也很简单,我们简单说明:如果前2k+2个电池中只取了一对电池并放在了机器i里作为芯片对应的最小能量电池,那么剩下的2k个电池中只有2k-2个也可以放在机器i里(机器i只有2k-2个位置了),这样会剩下相邻的两个电池,这对电池假设最终被放在了机器j当中,如果j中的两块芯片对应的两块能量最小的电池是,因为是在第2k+2个电池后选取的,所以有,这与是分别是两块芯片中能量最小的这一事实是矛盾的。所以说,是一定要取的,如果取不动(即),说明当前枚举的x太小了。
这样,判断标准就明确了,我们在枚举到第2 / 2k+2 / 4k+2 / ...块电池时,必须有cnt大于等于1 / 2 / 3 /...,全部满足时x合法,否则x非法。
考虑到可能会被查重,这里只提供思路,不提供代码。