#P1283. 平板涂色

平板涂色

Description

CE Digital has developed a product called the Automatic Painting Machine (APM). It can paint a board composed of non-overlapping rectangles of various sizes using predetermined colors.

To paint, the APM uses a set of brushes. Each brush paints a distinct color CiC_i. The APM picks up a brush of color CiC_i and paints all rectangles whose color is CiC_i and which satisfy the restriction below:

To prevent paint from seeping and mixing colors, a rectangle can be painted only after all rectangles that are immediately above it have been painted. For example, in the figure, rectangle FF can be painted only after CC and DD have been painted. Note that each rectangle must be painted to completion immediately; you cannot paint only part of it.

Write a program to find a painting plan that minimizes the number of times the APM picks up a brush. Note that if a brush is picked up more than once, each pickup counts toward the total.

Input Format

The first line contains the number of rectangles NN. The next NN lines each describe one rectangle. Each rectangle is given by 55 integers: the top-left yy-coordinate and xx-coordinate, the bottom-right yy-coordinate and xx-coordinate, and the predetermined color.

The top-left coordinate of the board is always (0,0)(0, 0).

Output Format

Output a single integer, the minimum number of brush pickups.

7
0 0 2 2 1
0 2 1 6 2
2 0 4 2 1
1 2 4 4 2
1 4 3 6 1
4 0 6 4 1
3 4 6 6 2

3

Hint

Constraints: 1Ci201 \le C_i \le 20, 0xi,yi990 \le x_i, y_i \le 99, 1N161 \le N \le 16.

Translated by ChatGPT 5