CodeForces 297D Color the Carpet (脑补题)

题意

一个h*w的矩阵上面涂k种颜色,并且每行相邻格子、每列相邻格子都有=或者!=的约束。要求构造一种涂色方案使得至少有3/4的条件满足。

思路

脑补神题……自己肯定想不出来T_T……


官方题解:

297D - Color the Carpet

This is my favorite problem in the contest :)

For k = 1 there is only one coloring so we just need to check the number of "E" constraints. When k ≥ 2, it turns out that using only 2color is sufficient to do so.

We will call the constraints that involves cells in different row "vertical constraints", and similar for "horizontal constraints".

First of all, we color the first row such that all horizontal constraints in row 1 are satisfied. We will color the remaining rows one by one.

To color row i, first we color it such that all horizontal constraints in row i are satisfied. Then consider the vertical constraints between row i and row i - 1. Count the number of satisfied and unsatisfied vertical constraints. If there are more unsatisfied constraints than satisfied constraints, flip the coloring of row i. Flipping a row means 2211212 → 1122121, for example.

If we flip the coloring of row i, all horizontal constraints in row i are still satisfied, but for the vertical constraints between row i and row i - 1, satisfied will becomes unsatisfied, unsatisfied will becomes satisfied. Therefore, one can always satisfy at least half the vertical constraints between row i and row i - 1.

Now for each row, all horizontal constraints are satisfied, at least half vertical constraints with the upper row are satisfied. It seems that we have got 75% satisfied in total. Unfortunately, the is exactly one more vertical constraints than horizontal constraints, which may make the fraction of satisfied constraints slightly less than 75%.

Luckily, we still have the all-satisfied first row. We can 'take' one satisfied horizontal constraints from it, and now we are guaranteed to have 75% satisfied constraints for row i. This also implies that the width of the table should be no less than the height of the table. If it is not in the case, rotate the table.


 

原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114109.html