All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Oh, no! Chef’s in trouble. He’s got himself stuck in a cave (we don’t know how) and is looking for a way out. The bigger problem is that he needs to get his tractor out of the cave (don't ask why Chef owns a tractor!). He currently faces a large block of height N cells and length N cells, and needs to get his tractor across this block. The block is made up of vertical columns of soil, each of which is one cell long. Each column has a continuous vertical gap, with the ith column having its gap from the lith cell to the hith cell (starting from the bottom, 0-indexing). That is, in the ith column, there is no soil from the lith cell to the hith cell (both inclusive). Chef can build additional gaps by clearing some cells of soil. His tractor has height H, and therefore, he needs to build a horizontal corridor of height H passing through all the columns. That is, some consecutive H rows must have no soil. Please see the figures in the example and explanation sections for more details.
Chef is able to clear one cell of soil by spending one unit of energy. Chef is smart, and will figure out a way to build the horizontal corridor while spending the minimum possible amount of energy. To estimate how many of his tasty dishes he will still be able to cook for you tonight, find out what is the minimum possible energy he needs to spend.
First line of input contains one integer T - number of test cases. T test cases follow.
Each test case starts with two integers N and H – size of the cave and height of the tractor, respectively. In each of the next N lines are two integers li and hi, respectively indicating lowest and highest number of cell for the gap in the ith column.
One integer – minimum energy required.
- 1 ≤ T ≤ 103
- 1 ≤ N ≤ 106
- 1 ≤ sum of N over all test cases ≤ 106
- 1 ≤ H ≤ N
- 0 ≤ li ≤ hi < N
Subtask 1 (25 points): T ≤ 100, N ≤ 100
Subtask 2 (75 points): No additional constraints
Input: 2 4 3 1 2 1 2 1 2 1 2 5 2 2 3 1 2 2 3 1 2 2 3 Output: 4 2
In the second case, the figure describes the initial map, where white cells denote empty cells and brown cells denote soil cells.
When we removed soil in two cells as the following figure, then we can make a corridor of height 2, adn this is the optimal way to make a corridor.
|Tags||aug15, dynamic-programming, easy, prefix-sum, subarray, witalij_hq|
|Time Limit:||2 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.