A15193 | Chopping Carrots (Hard Version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is the hard version of the problem. The only difference between the versions is the constraints on $n$ , $k$ , $a_i$ , and the sum of $n$ 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 $a_1, a_2, \ldots, a_n$ of length $n$ , and an integer $k$ .
The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ 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, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$ . Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$.
Note the unusual memory limit.
You are given an array of integers $a_1, a_2, \ldots, a_n$ of length $n$ , and an integer $k$ .
The cost of an array of integers $p_1, p_2, \ldots, p_n$ of length $n$ 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, $\lfloor \frac{x}{y} \rfloor$ denotes the integer part of the division of $x$ by $y$ . Find the minimum cost of an array $p$ such that $1 \le p_i \le k$ for all $1 \le i \le n$.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 100$ ) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ( $1 \le n, k \le 10^5$ ).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
The first line of each test case contains two integers $n$ and $k$ ( $1 \le n, k \le 10^5$ ).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$ .
输出格式
For each test case, print a single integer — the minimum possible cost of an array $p$ satisfying the condition above.
输入输出样例
输入 #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]$ . The resulting array of values of $\lfloor \frac{a_i}{p_i} \rfloor$ is $[4, 5, 6, 4, 5]$ . The cost of $p$ is $\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]$ , which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$ .
In the third test case, the only possible array is $p = [1, 1, 1]$ .
In the second test case, one of the optimal arrays is $p = [12, 12, 12, 12, 12]$ , which results in all $\lfloor \frac{a_i}{p_i} \rfloor$ being $0$ .
In the third test case, the only possible array is $p = [1, 1, 1]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted