Editorial for TLE '17 Contest 1 P5 - Cake
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The first subtask is troll.
To solve the second subtask, it is enough to check the conditions directly. To determine if there is cake at , two conditions must be met:
- Row contains cake at positions between and . This cell will contain cake if . Otherwise, this cell will not contain cake.
- Column contains cake at positions between and . This cell will contain cake if . Otherwise, this cell will not contain cake.
If exactly one of these conditions is met, then the cell is unclear. Otherwise, it is clear.
Next, we want to check if the cake is connected. It is enough to generate the cake, perform a BFS/DFS graph search, and then check if all cells of the cake are visited.
Time Complexity:
There are various ways to solve subtask 3. The intended solution was to use 3 segment trees. Consider all of the rows of the cake.
- The first segment tree will store the first row to have cake in a specific column. For the column, call this value .
- The second segment tree will store the last row to have cake in a specific column. For the column, call this value .
- The third segment tree will store the number of rows that have cake in a specific column. For the column, call this value .
Now, for each column , we check if the segment trees agree with and . First of all, the amount of cake must agree. That is, . Also, the starting and ending positions must agree too. That is, and .
If any of these conditions is false, there must be an unclear cell in this column. To find an unclear cell, check each of the cells in this column to find it. We already have a way to determine if there is cake at .
If there are no unclear cells, it is time to check if the cake is connected. Notice that if a row can be visited, all of the cake in that row can be visited. If it is always possible to move between two adjacent rows, then the cake is connected (otherwise, it is unconnected).
There are only 2 cases where adjacent rows are unconnected.
Time Complexity:
Note: It might be possible to solve this problem with a time complexity of .
Comments