hihocoder#1384 Genius ACM(倍增+二分)

描述
Advanced CPU Manufacturer (ACM) is one of the best CPU manufacturer in the world. Every day, they manufacture n CPU chips and sell them all over the world.

As you may know, each batch of CPU chips must pass a quality test by the QC department before they can be sold. The testing procedure is as follows:

1) Randomly pick m pairs of CPU chips from the batch of chips (If there are less than 2m CPU chips in the batch of chips, pick as many pairs as possible.)

2) For each pair, measure the Relative Performance Difference (RPD) between the two CPU chips. Let Di be the RPD of the i-th pair

3) Calculate the Sqared Performance Difference (SPD) of the batch according to the following formula:

SPD=∑Di2

If there are only 1 CPU in a batch, then the SPD of that batch is 0.

4) The batch of chips pass the test if and only if SPD≤k, where k is a preseted constant

Usually they send all the n CPU chips as a single batch to the QC department every day. As one of the best CPU manufacturer in the world, ACM never fail the test. However, with the continuous improvement of CPU performance, they find that they are at risk!

Of course they don’t want to take any risks. So they make a decision to divide the n chips into several batches to ensure all of them pass the test. What’s more, each batch should be a continuous subsequence of their productions, otherwise the QC department will notice that they are cheating. Quality tests need time and money, so they want to minimize the number of batches.

Given the absolute performance of the n chips P1 … Pn mesured by ACM in order of manufacture, your task is to determine the minimum number of batches to ensure that all chips pass the test. The RPD of two CPU chips equals to the difference of their absolute performance.

输入
The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains three integers n, m, k. The second line contains n integers, P1 … Pn.

T≤12
1≤n,m≤5×105
0≤k≤1018
0≤Pi≤220

输出
For each test case, print the answer in a single line.

样例输入
2
5 1 49
8 2 1 7 9
5 1 64
8 2 1 7 9
样例输出
2
1

首先要段数最小那么肯定是对于每个起点来说,区间长度越长越好。对于一段定长的区间,我们肯定是最大配最小来凑出一对是最好的。我们可以通过倍增确定\([pos,pos+2^{k}]\)是否合法,如果不合法,那么我们相当于固定左端点,可以在\([pos+2^{k-1},pos+2^{k}]\)二分右端点。在二分之前可以先把所有数排序,然后根据下标是否超过二分的右端点来进行选值。
时间复杂度大概是nlogn的。

发表评论

电子邮件地址不会被公开。 必填项已用*标注