CF1821D.Black Cells
普及+/提高
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are playing with a really long strip consisting of 1018 white cells, numbered from left to right as 0, 1, 2 and so on. You are controlling a special pointer that is initially in cell 0. Also, you have a "Shift" button you can press and hold.
In one move, you can do one of three actions:
- move the pointer to the right (from cell x to cell x+1);
- press and hold the "Shift" button;
- release the "Shift" button: the moment you release "Shift", all cells that were visited while "Shift" was pressed are colored in black.
(Of course, you can't press Shift if you already hold it. Similarly, you can't release Shift if you haven't pressed it.)
Your goal is to color at least k cells, but there is a restriction: you are given n segments [li,ri] — you can color cells only inside these segments, i. e. you can color the cell x if and only if li≤x≤ri for some i.
What is the minimum number of moves you need to make in order to color at least k cells black?
你正在操作一条非常长的纸带,上面有 1018 个白色格子,从左到右依次编号为 0、1、2,依此类推。你控制一个特殊的指针,初始时位于第 0 号格子。此外,你还有一个可按住的“Shift”键。
每一步操作中,你可以执行以下三种动作之一:
- 将指针向右移动(从格子 x 移动到格子 x+1);
- 按下并按住“Shift”键;
- 松开“Shift”键:在松开“Shift”的瞬间,所有在“Shift”被按下期间访问过的格子将被染成黑色。
(当然,若当前已按住“Shift”,则不能再按;同理,若尚未按下“Shift”,则不能松开。)
你的目标是至少将 k 个格子染成黑色,但存在一个限制条件:题目给出 n 个区间 [li,ri],你只能在这些区间内部染色,即:仅当存在某个 i 使得 li≤x≤ri 时,才允许将格子 x 染黑。
为达成目标,最少需要多少步操作?
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n and k (1≤n≤2⋅105; 1≤k≤109) — the number of segments and the desired number of black cells, respectively.
The second line contains n integers l1,l2,…,ln (1≤l1<l2<⋯<ln≤109), where li is the left border of the i-th segment.
The third line contains n integers r1,r2,…,rn (1≤ri≤109; li≤ri<li+1−1), where ri is the right border of the i-th segment.
Additional constraints on the input:
- every cell belongs to at most one segment;
- the sum of n doesn't exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
每个测试用例的第一行包含两个整数 n 和 k(1≤n≤2⋅105;1≤k≤109)—— 分别表示线段的数量和目标黑色格子数量。
每个测试用例的第二行包含 n 个整数 l1,l2,…,ln(1≤l1<l2<⋯<ln≤109),其中 li 表示第 i 条线段的左端点。
每个测试用例的第三行包含 n 个整数 r1,r2,…,rn(1≤ri≤109;li≤ri<li+1−1),其中 ri 表示第 i 条线段的右端点。
输入的额外约束条件:
- 每个格子至多属于一条线段;
- 所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, print the minimum number of moves to color at least k cells black, or −1 if it's impossible.
对于每个测试用例,输出将至少 k 个格子染成黑色所需的最少操作次数;如果无法实现,则输出 −1。
输入输出样例
输入#1
4 2 3 1 3 1 4 4 20 10 13 16 19 11 14 17 20 2 3 1 3 1 10 2 4 99 999999999 100 1000000000
输出#1
8 -1 7 1000000004
说明/提示
In the first test case, one of the optimal sequences of operations is the following:
- Move right: pointer is moving into cell 1;
- Press Shift;
- Release Shift: cell 1 is colored black;
- Move right: pointer is moving into cell 2;
- Move right: pointer is moving into cell 3;
- Press Shift;
- Move right: pointer is moving into cell 4;
- Release Shift: cells 3 and 4 are colored in black.
We've colored 3 cells in 8 moves.
In the second test case, we can color at most 8 cells, while we need 20 cell to color.
In the third test case, one of the optimal sequences of operations is the following:
- Move right: pointer is moving into cell 1;
- Move right: pointer is moving into cell 2;
- Move right: pointer is moving into cell 3;
- Press Shift;
- Move right: pointer is moving into cell 4;
- Move right: pointer is moving into cell 5;
- Release Shift: cells 3, 4 and 5 are colored in black.
We've colored 3 cells in 7 moves.
在第一个测试用例中,一种最优的操作序列如下:
- 向右移动:指针移入第 1 个单元格;
- 按下 Shift 键;
- 松开 Shift 键:第 1 个单元格被涂黑;
- 向右移动:指针移入第 2 个单元格;
- 向右移动:指针移入第 3 个单元格;
- 按下 Shift 键;
- 向右移动:指针移入第 4 个单元格;
- 松开 Shift 键:第 3 和第 4 个单元格被涂黑。
我们共用 8 步操作涂黑了 3 个单元格。
在第二个测试用例中,最多只能涂黑 8 个单元格,而我们需要涂黑 20 个单元格。
在第三个测试用例中,一种最优的操作序列如下:
- 向右移动:指针移入第 1 个单元格;
- 向右移动:指针移入第 2 个单元格;
- 向右移动:指针移入第 3 个单元格;
- 按下 Shift 键;
- 向右移动:指针移入第 4 个单元格;
- 向右移动:指针移入第 5 个单元格;
- 松开 Shift 键:第 3、4 和 5 个单元格被涂黑。
我们共用 7 步操作涂黑了 3 个单元格。
输入解题思路,AI测评打分。不知道怎么写?