CF1821D.Black Cells
普及/提高-
通过率:0%
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?
输入格式
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 .
输出格式
For each test case, print the minimum number of moves to color at least k cells black, or −1 if it's impossible.
输入输出样例
输入#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.