#BS0000447. Hàng rào kỳ lạ (Weird Fence)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hàng rào kỳ lạ (Weird Fence)
Weird Fence
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
For the Sultan's World Weird Fence festival, poles stand at distinct points of a coordinate plane. Each pole is red or blue and has one hook.
An unlimited number of chains is available, all with the same integer length . A chain has a red end and a blue end; each end must attach to a pole of the corresponding color. A chain can connect two poles if its length is at least their straight-line distance:
A connected pair forms a fence. Each pole may be used in at most one fence. Fences may intersect.
Find the minimum integer length permitting at least fences, or determine that it is impossible.
Input
The first line contains integers , the pole count and required fence count.
Each of the next lines contains integers and either red or blue, specifying the location and color of pole .
Output
Print the minimum integer chain length, or Impossible if fewer than fences can be built for every length.
Subtasks
- Subtask 1 (20 points): ; ; for every ; each color is
redorblue; all pole locations are distinct. - Subtask 2 (30 points): ; ; for every ; each color is
redorblue; all pole locations are distinct. - Subtask 3 (50 points): ; ; for every ; each color is
redorblue; all pole locations are distinct.
Examples
Input 1
6 2
-3 5 blue
-3 3 red
1 5 blue
2 0 red
4 6 blue
4 -1 red
Output 1
6
Explanation 1
Pair with at distance 2 and with at distance . Both fit length 6. With length at most 5, only the red pole has a blue neighbor; the other red poles are farther than 5 from every blue pole. Its single hook prevents two fences.
Input 2
6 4
-3 5 blue
-3 3 red
1 5 blue
2 0 red
4 6 blue
4 -1 red
Output 2
Impossible
Explanation 2
There are three poles of each color. Four fences require four of each color, so no chain length suffices.
