Pontoon Bridge
Time limit1sMemory limit256 MB
Find the smallest connected set of water squares that touches both banks of a river given as water intervals per row.
- Level
Medium7 of 10
- Topics
- Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Some years ago the Hooligan tribe went to war. Their army moves machine guns and tanks, so crossing the many rivers of the region is the hardest part of every march. Hooligan cartographers redraw the land so that every boundary between land and water runs along the sides of the unit squares of a square grid. On such a map every river flows straight down, and every coordinate of the left bank is smaller than every coordinate of the right bank.
The inventor Postolomlatos builds mobile bridges out of pontoons. One pontoon covers exactly one unit square of water. Two pontoons hold together only when they share a whole side, and a pontoon reaches a bank only when it shares a whole side with a square of land. Chief Mlask the Great wants every river crossed with as few pontoons as possible.
The map of one river is unit strips high. The strips are numbered to from top to bottom. In strip the water covers the horizontal coordinates from to , so the water squares of that strip are , where is the square of strip between the vertical lines and . In strip every square with coordinate smaller than belongs to the left bank, and every square with coordinate at least belongs to the right bank. Nothing exists above strip or below strip .
A set of water squares is a bridge when all three conditions hold.
- Any two squares of are joined by a chain of squares of in which consecutive squares share a whole side.
- Some square of shares a whole side with a square of the left bank.
- Some square of shares a whole side with a square of the right bank.
The square that touches the left bank and the square that touches the right bank may lie in different strips. Compute the smallest number of squares a bridge can have.
Input
The first line contains the number of test cases . The first line of each test case contains one positive integer , the height of the map. Each of the next lines describes one horizontal unit strip, from top to bottom, and contains two space separated integers and : the left and the right coordinate of the river bank in that strip. Always , and every is smaller than every .
Output
For each test case print exactly one line:
K prechodu reky je treba X pontonu.
Replace with the smallest number of pontoons that build a bridge from one bank of the river to the other. Print the sentence exactly as shown, in Czech and without diacritics. The K at the start of the line is the Czech preposition, not the height of the map.