Editorial for COCI '10 Contest 3 #6 Mono


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let us denote by M the rectangle with minimal area that completely contains the provided polygon. We intersect M with the least possible number of vertical lines such that one of them passes through each polygon vertex. The lines divide the polygon into rectangles, so its area can be computed as the sum of areas of individual rectangles.

Note that the area of a single rectangle can be computed using the inclusion-exclusion principle. The area equals the difference of areas above the lower and upper edge.

Furthermore, the sum of their areas can be obtained by traversing the polygon anticlockwise. Whenever we traverse a horizontal edge, we add the area above it to the sum, with the sign depending on the direction of traversal. Since the uppermost edge will be traversed from right to left, we will add areas in that direction with the negative sign.

Let us select a cell from the table as a potential upper left corner of the rectangle M. Next we determine a letter on one of the cells contained in the polygon specified by M. The polygon is monoliteral only if all its cells contain that same letter, i.e. if the number of incidences of that letter is equal to its area.

The number of incidences of a letter within a polygon can be calculated in an analogous way to calculating its area. The only difference is that instead of adding areas, we add the number of incidences of the required letter. A matrix can be precomputed containing data enabling us to answer such queries in constant time.

Finally, the total number of monoliteral polygons is obtained by running the described algorithm for each potential upper left cell in the table. The time complexity is \mathcal O(RCV).


Comments

There are no comments at the moment.