CF1821D.Black Cells

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are playing with a really long strip consisting of 101810^{18} white cells, numbered from left to right as 00, 11, 22 and so on. You are controlling a special pointer that is initially in cell 00. 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 xx to cell x+1x + 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 kk cells, but there is a restriction: you are given nn segments [li,ri][l_i, r_i] — you can color cells only inside these segments, i. e. you can color the cell xx if and only if li≤x≤ril_i \le x \le r_i for some ii.

What is the minimum number of moves you need to make in order to color at least kk cells black?

你正在操作一条非常长的纸带,上面有 101810^{18} 个白色格子,从左到右依次编号为 00、11、22,依此类推。你控制一个特殊的指针,初始时位于第 00 号格子。此外,你还有一个可按住的“Shift”键。

每一步操作中,你可以执行以下三种动作之一:

  • 将指针向右移动(从格子 xx 移动到格子 x+1x + 1);
  • 按下并按住“Shift”键;
  • 松开“Shift”键:在松开“Shift”的瞬间,所有在“Shift”被按下期间访问过的格子将被染成黑色。

(当然,若当前已按住“Shift”,则不能再按;同理,若尚未按下“Shift”,则不能松开。)

你的目标是至少将 kk 个格子染成黑色,但存在一个限制条件:题目给出 nn 个区间 [li,ri][l_i, r_i],你只能在这些区间内部染色,即:仅当存在某个 ii 使得 li≤x≤ril_i \le x \le r_i 时,才允许将格子 xx 染黑。

为达成目标,最少需要多少步操作?

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 1≤k≤1091 \le k \le 10^9) — the number of segments and the desired number of black cells, respectively.

The second line contains nn integers l1,l2,…,lnl_1, l_2, \dots, l_n (1≤l1<l2<⋯<ln≤1091 \le l_1 \lt l_2 \lt \dots \lt l_n \le 10^9), where lil_i is the left border of the ii-th segment.

The third line contains nn integers r1,r2,…,rnr_1, r_2, \dots, r_n (1≤ri≤1091 \le r_i \le 10^9; li≤ri<li+1−1l_i \le r_i \lt l_{i + 1} - 1), where rir_i is the right border of the ii-th segment.

Additional constraints on the input:

  • every cell belongs to at most one segment;
  • the sum of nn doesn't exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;1≤k≤1091 \le k \le 10^9)—— 分别表示线段的数量和目标黑色格子数量。

每个测试用例的第二行包含 nn 个整数 l1,l2,…,lnl_1, l_2, \dots, l_n(1≤l1<l2<⋯<ln≤1091 \le l_1 \lt l_2 \lt \dots \lt l_n \le 10^9),其中 lil_i 表示第 ii 条线段的左端点。

每个测试用例的第三行包含 nn 个整数 r1,r2,…,rnr_1, r_2, \dots, r_n(1≤ri≤1091 \le r_i \le 10^9;li≤ri<li+1−1l_i \le r_i \lt l_{i + 1} - 1),其中 rir_i 表示第 ii 条线段的右端点。

输入的额外约束条件:

  • 每个格子至多属于一条线段;
  • 所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print the minimum number of moves to color at least kk cells black, or −1-1 if it's impossible.

对于每个测试用例,输出将至少 kk 个格子染成黑色所需的最少操作次数;如果无法实现,则输出 −1-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:

  1. Move right: pointer is moving into cell 11;
  2. Press Shift;
  3. Release Shift: cell 11 is colored black;
  4. Move right: pointer is moving into cell 22;
  5. Move right: pointer is moving into cell 33;
  6. Press Shift;
  7. Move right: pointer is moving into cell 44;
  8. Release Shift: cells 33 and 44 are colored in black.

We've colored 33 cells in 88 moves.

In the second test case, we can color at most 88 cells, while we need 2020 cell to color.

In the third test case, one of the optimal sequences of operations is the following:

  1. Move right: pointer is moving into cell 11;
  2. Move right: pointer is moving into cell 22;
  3. Move right: pointer is moving into cell 33;
  4. Press Shift;
  5. Move right: pointer is moving into cell 44;
  6. Move right: pointer is moving into cell 55;
  7. Release Shift: cells 33, 44 and 55 are colored in black.

We've colored 33 cells in 77 moves.

在第一个测试用例中,一种最优的操作序列如下:

  1. 向右移动:指针移入第 11 个单元格;
  2. 按下 Shift 键;
  3. 松开 Shift 键:第 11 个单元格被涂黑;
  4. 向右移动:指针移入第 22 个单元格;
  5. 向右移动:指针移入第 33 个单元格;
  6. 按下 Shift 键;
  7. 向右移动:指针移入第 44 个单元格;
  8. 松开 Shift 键:第 33 和第 44 个单元格被涂黑。

我们共用 88 步操作涂黑了 33 个单元格。

在第二个测试用例中,最多只能涂黑 88 个单元格,而我们需要涂黑 2020 个单元格。

在第三个测试用例中,一种最优的操作序列如下:

  1. 向右移动:指针移入第 11 个单元格;
  2. 向右移动:指针移入第 22 个单元格;
  3. 向右移动:指针移入第 33 个单元格;
  4. 按下 Shift 键;
  5. 向右移动:指针移入第 44 个单元格;
  6. 向右移动:指针移入第 55 个单元格;
  7. 松开 Shift 键:第 33、44 和 55 个单元格被涂黑。

我们共用 77 步操作涂黑了 33 个单元格。

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

首页