CF1706D2.Chopping Carrots (Hard Version)

提高+/省选-

通过率:0%

时间限制:4.00s

内存限制:64MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference between the versions is the constraints on nn, kk, aia_i, and the sum of nn over all test cases. You can make hacks only if both versions of the problem are solved.

Note the unusual memory limit.

You are given an array of integers a1,a2,…,ana_1, a_2, \ldots, a_n of length nn, and an integer kk.

The cost of an array of integers p1,p2,…,pnp_1, p_2, \ldots, p_n of length nn is $$\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).$$

Here, ⌊xy⌋\lfloor \frac{x}{y} \rfloor denotes the integer part of the division of xx by yy. Find the minimum cost of an array pp such that 1≤pi≤k1 \le p_i \le k for all 1≤i≤n1 \le i \le n.

这是该问题的困难版本。两个版本的唯一区别在于对 nn、kk、aia_i 以及所有测试用例中 nn 的总和的约束条件。仅当两个版本的问题均被解决时,才允许进行 Hack。

注意:本题具有非同寻常的内存限制。

给定一个长度为 nn 的整数数组 a1,a2,…,ana_1, a_2, \ldots, a_n,以及一个整数 kk。

长度为 nn 的整数数组 p1,p2,…,pnp_1, p_2, \ldots, p_n 的代价定义为

max⁡1≤i≤n(⌊aipi⌋)−min⁡1≤i≤n(⌊aipi⌋).\max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).

其中,⌊xy⌋\lfloor \frac{x}{y} \rfloor 表示 xx 除以 yy 的商的整数部分(即向下取整)。请找出满足对所有 1≤i≤n1 \le i \le n 均有 1≤pi≤k1 \le p_i \le k 的数组 pp 的最小可能代价。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n,k≤1051 \le n, k \le 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤a1≤a2≤…≤an≤1051 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 表示测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n,k≤1051 \le n, k \le 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤a1≤a2≤…≤an≤1051 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5)。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, print a single integer — the minimum possible cost of an array pp satisfying the condition above.

对于每个测试用例,输出一个整数——满足上述条件的数组 pp 的最小可能代价。

输入输出样例

  • 输入#1

    7
    5 2
    4 5 6 8 11
    5 12
    4 5 6 8 11
    3 1
    2 9 15
    7 3
    2 3 5 5 6 9 10
    6 56
    54 286 527 1436 2450 2681
    3 95
    16 340 2241
    2 2
    1 3

    输出#1

    2
    0
    13
    1
    4
    7
    0

说明/提示

In the first test case, the optimal array is p=[1,1,1,2,2]p = [1, 1, 1, 2, 2]. The resulting array of values of ⌊aipi⌋\lfloor \frac{a_i}{p_i} \rfloor is [4,5,6,4,5][4, 5, 6, 4, 5]. The cost of pp is max⁡1≤i≤n(⌊aipi⌋)−min⁡1≤i≤n(⌊aipi⌋)=6−4=2\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2. We can show that there is no array (satisfying the condition from the statement) with a smaller cost.

In the second test case, one of the optimal arrays is p=[12,12,12,12,12]p = [12, 12, 12, 12, 12], which results in all ⌊aipi⌋\lfloor \frac{a_i}{p_i} \rfloor being 00.

In the third test case, the only possible array is p=[1,1,1]p = [1, 1, 1].

在第一个测试用例中,最优数组为 p=[1,1,1,2,2]p = [1, 1, 1, 2, 2]。对应得到的 ⌊aipi⌋\lfloor \frac{a_i}{p_i} \rfloor 数组为 [4,5,6,4,5][4, 5, 6, 4, 5]。数组 pp 的代价为 max⁡1≤i≤n(⌊aipi⌋)−min⁡1≤i≤n(⌊aipi⌋)=6−4=2\max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2。可以证明:不存在满足题目条件且代价更小的数组。

在第二个测试用例中,一个最优数组为 p=[12,12,12,12,12]p = [12, 12, 12, 12, 12],此时所有 ⌊aipi⌋\lfloor \frac{a_i}{p_i} \rfloor 均为 00。

在第三个测试用例中,唯一可能的数组为 p=[1,1,1]p = [1, 1, 1]。

输入解题思路,AI测评打分。不知道怎么写?

首页